-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver_win.py
95 lines (69 loc) · 3.12 KB
/
server_win.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pkg_resources, time, os
command = "pip install "
for lib in ['flask', 'g4f', 'flask_cors', 'pygetwindow', 'keyboard', 'pywin32']:
try:
dist = pkg_resources.get_distribution(lib)
command = command + lib + " "
except pkg_resources.DistributionNotFound:
command = command + lib + " "
command = command + " --upgrade"
os.system(command)
os.system('cls')
import flask, win32gui, keyboard, pygetwindow as gw
from flask import Flask, request, jsonify
from flask_cors import CORS
from g4f.client import Client
app = Flask(__name__)
CORS(app, origins='*', allow_headers='*')
client = Client()
@app.route('/api', methods=['POST'])
def api():
data = request.json
fulltask = data.get('fulltask', '')
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": f"I will next provide you with a question in a language. Strictly follow its instructions for replying. Do NOT reply with elaborate answers or things not requested. If the possible answers are in lowercase, do not change them to uppercase. Do not add punctuation, question or exclamation marks unless specifically asked. Reply in the SAME language as the question (Spanish) with short answers, no punctuation. Here is the question: {fulltask}"}]
)
answer = response.choices[0].message.content
except Exception as e:
print(e)
return jsonify({'error': str(e)}), 500
return jsonify({'answer': answer})
@app.route('/type', methods=['POST'])
def type():
data = request.json
fixedString = data.get('fixedString', '')
try:
def send_text(text):
time.sleep(3)
keyboard.write(text, delay=0.1)
time.sleep(0.3)
keyboard.press_and_release('enter')
time.sleep(0.3)
def get_chrome_window():
chrome_windows = [window for window in gw.getAllTitles() if "discord" or "chrome" in window.lower()]
# you can replace "chrome" with brave/edge/opera/chromium/etc
if chrome_windows:
chrome_window_title = chrome_windows[0]
print(f"Found Chrome window: {chrome_window_title}")
hwnd = win32gui.FindWindow(None, chrome_window_title)
if hwnd:
# Disabled these options because they were giving errors, try at your own risk
#win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) # Restore the window if minimized
#win32gui.SetForegroundWindow(hwnd) # Activate the window
return True
else:
print("Failed to find window handle.")
return False
else:
print("Chrome window not found")
return False
get_chrome_window()
send_text(fixedString)
return jsonify({'Answer typed': fixedString})
except Exception as e:
print(e)
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)