forked from taranjeet/unofficial-chatgpt-api
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathserver.py
155 lines (128 loc) · 4.1 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""Make some requests to OpenAI's chatbot"""
import time
import os
import flask
from flask_cors import CORS
import requests
import json
from flask import g
from playwright.sync_api import sync_playwright
APP = flask.Flask(__name__)
CORS(APP)
PLAY = sync_playwright().start()
BROWSER = PLAY.chromium.launch_persistent_context(
user_data_dir="/tmp/playwright",
headless=False,
)
PAGE = BROWSER.new_page()
GPT_PREFIX = ""
# For short, concise responses:
# GPT_PREFIX = "Reply with less than 15 words: "
### RESEMBLE AI CONFIGURATION ###
### VISIT https://resemble.ai/ TO GET YOUR OWN TOKEN ###
RESEMBLE_TOKEN = ""
RESEMBLE_PROJECT = ""
RESEMBLE_VOICE = ""
RESEMBLE_ENDPOINT = ""
def get_input_box():
"""Get the child textarea of `PromptTextarea__TextareaWrapper`"""
return PAGE.query_selector("textarea")
def is_logged_in():
# See if we have a textarea with data-id="root"
return get_input_box() is not None
def is_loading_response() -> bool:
"""See if the send button is diabled, if it does, we're not loading"""
return not PAGE.query_selector("textarea ~ button").is_enabled()
def send_message(message):
# Send the message
box = get_input_box()
box.click()
box.fill(message)
box.press("Enter")
def get_last_message():
"""Get the latest message"""
while is_loading_response():
time.sleep(0.25)
page_elements = PAGE.query_selector_all("div[class*='request-:']")
last_element = page_elements.pop()
return last_element.inner_text()
def regenerate_response():
"""Clicks on the Try again button.
Returns None if there is no button"""
try_again_button = PAGE.query_selector("button:has-text('Try again')")
if try_again_button is not None:
try_again_button.click()
return try_again_button
def get_reset_button():
"""Returns the reset thread button (it is an a tag not a button)"""
return PAGE.query_selector("a:has-text('Reset thread')")
@APP.route("/chat", methods=["GET"]) #TODO: make this a POST
def chat():
message = flask.request.args.get("q")
print("Sending message: ", message)
send_message(message)
response = get_last_message()
print("Response: ", response)
return response
@APP.route("/voice", methods=["GET"]) #TODO: make this a POST
def voice():
message = flask.request.args.get("q")
message = GPT_PREFIX + message
print("Sending message: ", message)
send_message(message)
response = get_last_message()
print("Response: ", response)
url = RESEMBLE_ENDPOINT
payload = json.dumps({
"voice_uuid": RESEMBLE_VOICE,
"project_uuid": RESEMBLE_PROJECT,
"data": f"<speak>{response}</speak>"
})
headers = {
'x-access-token': RESEMBLE_TOKEN,
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
resemble_response = response.json()
audio = resemble_response["audio_content"]
return audio
# create a route for regenerating the response
@APP.route("/regenerate", methods=["POST"])
def regenerate():
print("Regenerating response")
if regenerate_response() is None:
return "No response to regenerate"
response = get_last_message()
print("Response: ", response)
return response
@APP.route("/reset", methods=["POST"])
def reset():
print("Resetting chat")
get_reset_button().click()
return "Chat thread reset"
@APP.route("/restart", methods=["POST"])
def restart():
global PAGE,BROWSER,PLAY
PAGE.close()
BROWSER.close()
PLAY.stop()
time.sleep(0.25)
PLAY = sync_playwright().start()
BROWSER = PLAY.chromium.launch_persistent_context(
user_data_dir="/tmp/playwright",
headless=False,
)
PAGE = BROWSER.new_page()
PAGE.goto("https://chat.openai.com/")
return "API restart!"
def start_browser():
PAGE.goto("https://chat.openai.com/")
if not is_logged_in():
print("Please log in to OpenAI Chat")
print("Press enter when you're done")
input()
else:
print("Logged in")
APP.run(port=5001, threaded=False)
if __name__ == "__main__":
start_browser()