-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
192 lines (147 loc) · 5.49 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
import os
import sys
import wave
import time
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO, emit
from chatter import Chatter, setup_logger
from thinker import Thinker
import openai
from pydub import AudioSegment
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload_audio', methods=['POST'])
def upload_audio():
print('upload audio called')
file = request.files.get('audio_data', None)
if file:
filename = f'user_audio/{time.time()}.wav'
file.save(filename)
transcription = transcribe(filename)
print(transcription)
socketio.emit('receive_transcription', transcription) #, room=room) No idea why GPT-4 added room lmao
return jsonify({"status": "success", "message": "File uploaded and saved."}), 200
else:
print('upload audio failed')
return jsonify({"status": "error", "message": "File not received."}), 400
def transcribe(filename):
start_time = time.time()
audio_file = open(filename, "rb")
ntries = 4
for i in range(ntries):
try:
transcript = openai.Audio.transcribe("whisper-1", audio_file)#, prompt=self._collate_textmap())
break
except openai.error.RateLimitError:
print(f"Hit rate limit on try #{i}")
time.sleep(2**i)
gen = gen_func(*args, **kwargs)
except openai.error.APIConnectionError:
print(f"API connection error on try #{i}")
gen = gen_func(*args, **kwargs)
time.sleep(2**i)
except Exception as e:
# Handle the error (e.g., log it, sleep and retry, etc.)
print(f"Unknown error occurred: {e}")
time.sleep(2**i)
end_time = time.time()
elapsed_time = end_time - start_time
content = transcript.text
logger.info(f"Finished transcription of {filename} in: {elapsed_time:.2f} seconds")
print(transcript)
return content
@socketio.on('upload_text')
def upload_text(text):
thread = chat.thinker.receive(text)
# thread.join()
@socketio.on('send_text')
def handle_send_text(text, chatter=None):
thread = chat.thinker.receive(text)
gen = my_generator()
tmp = ""
for word in gen:
if '``' in word and not tmp:
tmp = word
continue
if tmp:
tmp += word
if '\n' in word:
word = tmp
tmp = ""
else:
continue
emit('receive_word', word)
socketio.sleep(0.05) # Adjust this value to control the speed of the word-by-word display
emit('processing_done')
# join thread created for running any of the tools
# thread.join()
def generator_wrapper(gen_func):
def wrapper(*args, **kwargs):
gen = gen_func(*args, **kwargs)
has_error = True
t = 0
ntries = 4
for i in range(ntries):
try:
# Try to get the first value from the generator
first_value = next(gen)
break
except openai.error.RateLimitError:
print(f"Hit rate limit on try #{i}")
time.sleep(2**i)
gen = gen_func(*args, **kwargs)
except openai.error.APIConnectionError:
print(f"API connection error on try #{i}")
gen = gen_func(*args, **kwargs)
time.sleep(2**i)
#generally caused by overlength context window: should clear out thinker.utterances
# (or maybe I should do this pre-emptively...)
except openai.error.InvalidRequestError as e:
print(f"Invalid Request Error: {e}")
return None
except Exception as e:
# Handle the error (e.g., log it, sleep and retry, etc.)
print(f"Unknown error occurred: {e}")
time.sleep(2**i)
yield first_value
yield from gen
return wrapper
@generator_wrapper
def my_generator():
return chat.thinker._process(webapp=True)
def save_audio_to_file(audio_data, filename=None):
if not filename:
filename = f'{time.time()}.wav'
file_path = os.path.join('audio_files', filename)
with wave.open(file_path, 'wb') as wave_file:
wave_file.setnchannels(1)
wave_file.setsampwidth(2) # 2 bytes (16 bits) per sample
wave_file.setframerate(16000) # 16 kHz sampling rate
wave_file.writeframes(audio_data)
if __name__ == '__main__':
# Define the directory names
audio_dir = 'user_audio'
logs_dir = 'logs'
# Create the directories if they don't exist
if not os.path.exists(audio_dir):
os.makedirs(audio_dir)
if not os.path.exists(logs_dir):
os.makedirs(logs_dir)
logger = setup_logger('app', log_file='logs/app.log')
tlogger = setup_logger('thinker', log_file='logs/thinker.log')
thinker = Thinker({}, tlogger)
# thinker = Thinker({"model":"gpt-4-0314"}, tlogger)
chat = Chatter(listener=None, thinker=thinker, speaker=None)
if not os.path.exists('audio_files'):
os.makedirs('audio_files')
if not os.path.exists('logs'):
os.makedirs('logs')
try:
socketio.run(app, debug=True)
except KeyboardInterrupt:
print('Web server exited from terminal')
# chat.save()