-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
360 lines (307 loc) · 13 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import os
import sys
import time
import toml
import json
import uuid
import queue
import atexit
import base64
import pygame
import tempfile
import operator
import contextlib
import soundfile as sf
import sounddevice as sd
from time import sleep
from TTS.api import TTS
from functools import reduce
import speech_recognition as sr
from pynput import keyboard as pk
from playwright.sync_api import sync_playwright
config = toml.load("config.toml")
json_config = json.load(open("config.json"))
REC_START_FX = os.path.join('assets', config["paths"]["startfx"])
REC_END_FX = os.path.join('assets', config["paths"]["endfx"])
COOKIES_PATH = config["paths"]["cookies"]
VOICE_VOLUME = config["settings"]["volume"]
KEYS_COMBO = config["settings"]["key_combo"]
LANGUAGE = config["settings"]["language"]
BASE_PROMPT = json_config[LANGUAGE][0]
TRIGGER_WORDS = json_config[LANGUAGE][1]
SPEECH_LANG = json_config[LANGUAGE][2]
TTS_MODEL = json_config[LANGUAGE][3]
class ChatGPT:
"""
A ChatGPT interface that uses Playwright to run a browser,
and interacts with that browser to communicate with ChatGPT in
order to provide an open API to ChatGPT.
From github.com/mmabrouk/chatgpt-wrapper
"""
stream_div_id = "chatgpt-wrapper-conversation-stream-data"
eof_div_id = "chatgpt-wrapper-conversation-stream-data-eof"
session_div_id = "chatgpt-wrapper-session-data"
def __init__(self, headless: bool = True, browser="firefox", timeout=60):
self.play = sync_playwright().start()
try:
playbrowser = getattr(self.play, browser)
except Exception:
print(f"Browser {browser} is invalid, falling back on firefox")
playbrowser = self.play.firefox
self.browser = playbrowser.launch_persistent_context(
user_data_dir=os.path.join(os.path.dirname(__file__), COOKIES_PATH),
headless=headless,
)
if len(self.browser.pages) > 0:
self.page = self.browser.pages[0]
else:
self.page = self.browser.new_page()
self._start_browser()
self.parent_message_id = str(uuid.uuid4())
self.conversation_id = None
self.session = None
self.timeout = timeout
atexit.register(self._cleanup)
def _start_browser(self):
self.page.goto("https://chat.openai.com/")
def _cleanup(self):
self.browser.close()
self.play.stop()
def refresh_session(self):
self.page.evaluate(
"""
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://chat.openai.com/api/auth/session');
xhr.onload = () => {
if(xhr.status == 200) {
var mydiv = document.createElement('DIV');
mydiv.id = "SESSION_DIV_ID"
mydiv.innerHTML = xhr.responseText;
document.body.appendChild(mydiv);
}
};
xhr.send();
""".replace(
"SESSION_DIV_ID", self.session_div_id
)
)
while True:
session_datas = self.page.query_selector_all(f"div#{self.session_div_id}")
if len(session_datas) > 0:
break
sleep(0.2)
session_data = json.loads(session_datas[0].inner_text())
self.session = session_data
self.page.evaluate(f"document.getElementById('{self.session_div_id}').remove()")
def _cleanup_divs(self):
self.page.evaluate(f"document.getElementById('{self.stream_div_id}').remove()")
self.page.evaluate(f"document.getElementById('{self.eof_div_id}').remove()")
def ask_stream(self, prompt: str):
if self.session is None:
self.refresh_session()
new_message_id = str(uuid.uuid4())
if "accessToken" not in self.session:
yield (
"Your ChatGPT session is not usable.\n"
"* Run this program with the `install` parameter and log in to ChatGPT.\n"
"* If you think you are already logged in, try running the `session` command."
)
return
request = {
"messages": [
{
"id": new_message_id,
"role": "user",
"content": {"content_type": "text", "parts": [prompt]},
}
],
"model": "text-davinci-002-render",
"conversation_id": self.conversation_id,
"parent_message_id": self.parent_message_id,
"action": "next",
}
code = (
"""
const stream_div = document.createElement('DIV');
stream_div.id = "STREAM_DIV_ID";
document.body.appendChild(stream_div);
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://chat.openai.com/backend-api/conversation');
xhr.setRequestHeader('Accept', 'text/event-stream');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer BEARER_TOKEN');
xhr.responseType = 'stream';
xhr.onreadystatechange = function() {
var newEvent;
if(xhr.readyState == 3 || xhr.readyState == 4) {
const newData = xhr.response.substr(xhr.seenBytes);
try {
const newEvents = newData.split(/\\n\\n/).reverse();
newEvents.shift();
if(newEvents[0] == "data: [DONE]") {
newEvents.shift();
}
if(newEvents.length > 0) {
newEvent = newEvents[0].substring(6);
// using XHR for eventstream sucks and occasionally ive seen incomplete
// json objects come through JSON.parse will throw if that happens, and
// that should just skip until we get a full response.
JSON.parse(newEvent);
}
} catch (err) {
console.log(err);
newEvent = undefined;
}
if(newEvent !== undefined) {
stream_div.innerHTML = btoa(newEvent);
xhr.seenBytes = xhr.responseText.length;
}
}
if(xhr.readyState == 4) {
const eof_div = document.createElement('DIV');
eof_div.id = "EOF_DIV_ID";
document.body.appendChild(eof_div);
}
};
xhr.send(JSON.stringify(REQUEST_JSON));
""".replace(
"BEARER_TOKEN", self.session["accessToken"]
)
.replace("REQUEST_JSON", json.dumps(request))
.replace("STREAM_DIV_ID", self.stream_div_id)
.replace("EOF_DIV_ID", self.eof_div_id)
)
self.page.evaluate(code)
last_event_msg = ""
start_time = time.time()
while True:
eof_datas = self.page.query_selector_all(f"div#{self.eof_div_id}")
conversation_datas = self.page.query_selector_all(
f"div#{self.stream_div_id}"
)
if len(conversation_datas) == 0:
continue
full_event_message = None
try:
event_raw = base64.b64decode(conversation_datas[0].inner_html())
if len(event_raw) > 0:
event = json.loads(event_raw)
if event is not None:
self.parent_message_id = event["message"]["id"]
self.conversation_id = event["conversation_id"]
full_event_message = "\n".join(
event["message"]["content"]["parts"]
)
except Exception:
yield (
"Failed to read response from ChatGPT. Tips:\n"
" * Try again. ChatGPT can be flaky.\n"
" * Use the `session` command to refresh your session, and then try again.\n"
" * Restart the program in the `install` mode and make sure you are logged in."
)
break
if full_event_message is not None:
chunk = full_event_message[len(last_event_msg):]
last_event_msg = full_event_message
yield chunk
# if we saw the eof signal, this was the last event we
# should process and we are done
if len(eof_datas) > 0 or (((time.time() - start_time) > self.timeout) and full_event_message is None):
break
sleep(0.2)
self._cleanup_divs()
def ask(self, message: str) -> str:
"""
Send a message to chatGPT and return the response.
Args:
message (str): The message to send.
Returns:
str: The response received from OpenAI.
"""
response = list(self.ask_stream(message))
return (
reduce(operator.add, response)
if len(response) > 0
else "Unusable response produced, maybe login session expired. Try 'pkill firefox' and 'chatgpt install'"
)
def new_conversation(self):
self.parent_message_id = str(uuid.uuid4())
self.conversation_id = None
def on_press(key):
if key == pk.Key.alt_l:
global recording
recording = True
def on_release(key):
if key == pk.Key.alt_l:
global recording
recording = False
def callback(indata, frames, time, status):
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
if __name__ == "__main__":
if not os.path.exists(os.path.join(os.path.dirname(__file__), "cookies")):
chatgpt = ChatGPT(headless=False, timeout=90)
print("Running ChatGPT for the first time. Please log in to OpenAI.com and then press CTRL+C to continue...")
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
chatgpt._cleanup()
break
print("Setting up ChatGPT, Speech Recognition and Text-To-Speech...")
with open(os.devnull, "w") as f, contextlib.redirect_stdout(f):
q = queue.Queue()
bot = ChatGPT()
r = sr.Recognizer()
tts = TTS(model_name=TTS_MODEL, progress_bar=False)
listener = pk.Listener(on_press=on_press, on_release=on_release)
listener.start()
pygame.init()
ssound = pygame.mixer.Sound(REC_START_FX)
esound = pygame.mixer.Sound(REC_END_FX)
device_info = sd.query_devices(None, 'input')
samplerate = int(device_info['default_samplerate'])
_ = bot.ask(BASE_PROMPT.replace("TRIGGER WORDS", TRIGGER_WORDS))
recording = False
try:
print("\nWelcome to ChatGPT! Press ALT to start talking, and release to stop.")
print("Press CTRL+C to exit and wait for the program to finish.\n")
while True:
if recording:
with tempfile.NamedTemporaryFile(suffix='.wav') as youtf:
with sf.SoundFile(youtf.name, mode='w', samplerate=samplerate, channels=1, subtype=None) as file:
with sd.InputStream(samplerate=samplerate, device=None, channels=1, callback=callback):
ssound.play()
ssound.set_volume(VOICE_VOLUME*0.1)
pygame.time.wait(int(ssound.get_length() * 1000))
#print("Recording started...")
while True:
file.write(q.get())
if not recording:
esound.play()
esound.set_volume(VOICE_VOLUME*0.1)
pygame.time.wait(int(esound.get_length() * 1000))
#print("Recording stopped...")
break
with sr.AudioFile(youtf.name) as source:
audio = r.record(source)
try:
text = r.recognize_google(audio, show_all=True, language=SPEECH_LANG)
final_pred = text['alternative'][0]['transcript']
print(f"\nYou: {final_pred}")
response = bot.ask(final_pred).replace('\n', ' ')
with tempfile.NamedTemporaryFile(suffix='.wav') as bottf:
with open(os.devnull, "w") as f, contextlib.redirect_stdout(f):
tts.tts_to_file(text=response, file_path=bottf.name)
print(f"Bot: {response}")
vresponse = pygame.mixer.Sound(bottf.name)
vresponse.play()
vresponse.set_volume(VOICE_VOLUME)
pygame.time.wait(int(vresponse.get_length() * 1000))
except:
print("Bot: Sorry, I didn't catch that.")
except KeyboardInterrupt:
print('Bot: Goodbye!')
listener.stop()
pygame.quit()