-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.py
398 lines (319 loc) · 13.9 KB
/
start.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import os
import json
import asyncio
import sounddevice as sd
import numpy as np
import base64
from dotenv import load_dotenv
import traceback
import sys
import aiohttp
from aiohttp import WSMsgType
import queue
from concurrent.futures import ThreadPoolExecutor
# Load environment variables
load_dotenv(override=True)
# Audio settings
CHANNELS = 1
RATE = 24000
CHUNK = 1024
SILENCE_THRESHOLD = -30 # in dB
MIN_SILENCE_DURATION = 0.5 # in seconds
VOLUME = 1.0
# WebSocket URL
WS_URL = "wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01"
# Global variables
is_recording = False
is_model_talking = False
audio_output_stream = None
ws = None
audio_output_queue = queue.Queue()
loop = None
executor = ThreadPoolExecutor(max_workers=1)
total_cost = 0
silence_counter = 0
current_recording = []
audio_output_buffer = np.array([], dtype=np.float32)
audio_event_queue = asyncio.Queue()
audio_event_task = None
# Utility functions
def debug_log(message):
if os.getenv("DEBUG") and os.getenv("DEBUG").lower() != "false":
print(f"DEBUG: {message}", file=sys.stderr)
def error_log(message):
print(f"ERROR: {message}", file=sys.stderr)
def select_input_device():
devices = sd.query_devices()
input_devices = [d for d in devices if d['max_input_channels'] > 0]
print("Available input devices:")
for i, device in enumerate(input_devices):
print(f"{i}: {device['name']}")
while True:
try:
selection = int(input(f"Select input device (0-{len(input_devices)-1}): "))
if 0 <= selection < len(input_devices):
return input_devices[selection]['index']
else:
print("Invalid selection. Please try again.")
except ValueError:
print("Invalid input. Please enter a number.")
def calculate_db(audio_chunk, dtype='int16'):
if dtype == 'int16':
float_data = audio_chunk.astype(np.float32) / 32768.0
rms = np.sqrt(np.mean(float_data**2))
epsilon = 1e-10
db = 20 * np.log10(max(rms, epsilon))
else:
rms = np.sqrt(np.mean(audio_chunk**4))
db = 20 * np.log10(rms) if rms > 0 else -np.inf
return db
# Audio callbacks
def audio_input_callback(indata, frames, time, status):
global is_recording, silence_counter, current_recording, loop, audio_output_buffer, audio_output_stream, audio_output_queue
if status:
debug_log(f"Audio callback status: {status}")
try:
db_level = calculate_db(indata)
if db_level > SILENCE_THRESHOLD:
if not is_recording:
debug_log("Speech detected, starting recording...")
is_recording = True
current_recording = [indata.copy()]
if not audio_output_queue.empty():
while not audio_output_queue.empty():
audio_output_queue.get_nowait()
if is_model_talking:
loop.call_soon_threadsafe(audio_event_queue.put_nowait, {"type": "response.cancel"})
else:
current_recording.append(indata.copy())
silence_counter = 0
audio_event = {
"type": "input_audio_buffer.append",
"audio": base64.b64encode(indata).decode('utf-8')
}
loop.call_soon_threadsafe(audio_event_queue.put_nowait, audio_event)
else:
if is_recording:
silence_counter += 1
if silence_counter >= int(MIN_SILENCE_DURATION * RATE / CHUNK):
debug_log("Silence detected, stopping recording...")
is_recording = False
silence_counter = 0
loop.call_soon_threadsafe(audio_event_queue.put_nowait, {"type": "input_audio_buffer.commit"})
loop.call_soon_threadsafe(audio_event_queue.put_nowait, {"type": "response.create"})
current_recording = []
else:
current_recording.append(indata.copy())
audio_event = {
"type": "input_audio_buffer.append",
"audio": base64.b64encode(indata).decode('utf-8')
}
loop.call_soon_threadsafe(audio_event_queue.put_nowait, audio_event)
except Exception as e:
debug_log(f"Error in audio callback: {e}")
debug_log(traceback.format_exc())
def audio_output_callback(outdata, frames, time, status):
global VOLUME, audio_output_buffer
if status:
debug_log(f"Audio output callback status: {status}")
try:
if len(audio_output_buffer) < len(outdata):
while len(audio_output_buffer) < len(outdata):
if audio_output_queue.empty():
break
chunk = audio_output_queue.get_nowait()
audio_output_buffer = np.concatenate((audio_output_buffer, chunk.flatten()))
if len(audio_output_buffer) > 0:
samples_to_play = min(len(audio_output_buffer), len(outdata))
outdata[:samples_to_play, 0] = audio_output_buffer[:samples_to_play] * VOLUME
audio_output_buffer = audio_output_buffer[samples_to_play:]
if samples_to_play < len(outdata):
outdata[samples_to_play:, 0] = 0
else:
outdata[:] = 0
if len(audio_output_buffer) > 0:
debug_log(
f"Played {len(outdata)} samples, buffer size: {len(audio_output_buffer)}, queue size: {audio_output_queue.qsize()}")
except Exception as e:
debug_log(f"Error in audio output callback: {e}")
debug_log(traceback.format_exc())
outdata[:] = 0
# WebSocket and event handling
async def send_audio_event(audio_event):
global ws
if ws and not ws.closed:
debug_log(f"ws -> {audio_event['type']}")
await ws.send_str(json.dumps(audio_event))
async def handle_server_events():
global is_model_talking, ws, audio_output_stream, total_cost
while True:
try:
message = await asyncio.wait_for(ws.receive(), timeout=30.0)
if message.type == aiohttp.WSMsgType.TEXT:
event = json.loads(message.data)
debug_log("ws <- " + event["type"])
if event["type"] == "response.audio.delta":
handle_audio_delta(event)
elif event["type"] == "response.done":
handle_response_done(event)
elif event["type"] == "response.text.delta":
print(event["delta"], end="", flush=True)
elif event["type"] == "response.audio_transcript.delta":
print(f"{event['delta']}", end="", flush=True)
elif event["type"] == "error":
print(f"Error: {event['error']['message']}")
elif event["type"] == "session.created":
debug_log(f"Session created: {event['session']}")
elif event["type"] == "session.updated":
debug_log(f"Session updated: {event['session']}")
loop.call_soon_threadsafe(audio_event_queue.put_nowait, {"type": "response.create"})
print("Connected!\n")
elif event["type"] == "conversation.item.input_audio_transcription.completed":
print(f"> {event['transcript']}\n", end="", flush=True)
elif event["type"] == "conversation.item.input_audio_transcription.falied":
print(f"> ??? {event['error']['message']}", flush=True)
else:
debug_log(f"Unhandled event type: {event['type']}")
elif message.type == aiohttp.WSMsgType.CLOSED:
print("WebSocket connection closed")
break
elif message.type == aiohttp.WSMsgType.ERROR:
print(f"WebSocket error: {message.data}")
break
except asyncio.TimeoutError:
debug_log("No message received from server in 30 seconds")
except aiohttp.ClientConnectionError:
error_log("WebSocket connection closed")
break
except Exception as e:
error_log(f"Error in handle_server_events: {e}")
debug_log(traceback.format_exc())
def handle_audio_delta(event):
global is_model_talking, audio_output_stream
if not is_model_talking:
is_model_talking = True
debug_log("Model is talking...")
if audio_output_stream is None or not audio_output_stream.active:
audio_output_stream = sd.OutputStream(
samplerate=RATE, channels=CHANNELS, callback=audio_output_callback,
blocksize=CHUNK)
audio_output_stream.start()
debug_log("Received audio chunk")
audio_data = base64.b64decode(event["delta"])
audio_array = np.frombuffer(audio_data, dtype=np.int16)
audio_array = audio_array.astype(np.float32) / 32768.0
audio_array = audio_array.reshape(-1, 1)
debug_log(
f"Audio chunk size: {audio_array.shape}, min: {audio_array.min()}, max: {audio_array.max()}")
audio_output_queue.put(audio_array)
debug_log(f"Added chunk to queue, new size: {audio_output_queue.qsize()}")
def handle_response_done(event):
global is_model_talking, total_cost
is_model_talking = False
debug_log("Model finished talking")
input_tokens = event["response"]["usage"]["input_tokens"]
output_tokens = event["response"]["usage"]["output_tokens"]
price = (input_tokens * 100 + output_tokens * 200) / 1000000
total_cost += price
print(
f"\n - Usage: input_tokens: {input_tokens}, output_tokens: {output_tokens}, price: ${price:.2f}, total_cost: ${total_cost:.2f}\n\n")
async def process_audio_events():
while True:
event = await audio_event_queue.get()
await send_audio_event(event)
audio_event_queue.task_done()
# Main function and setup
async def main():
global is_recording, is_model_talking, ws, audio_output_stream, loop, audio_event_task
loop = asyncio.get_running_loop()
input_device = select_input_device()
print(f"Selected input device: {sd.query_devices(input_device)['name']}")
headers = {
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
"OpenAI-Beta": "realtime=v1"
}
try:
while True:
try:
debug_log("Attempting to connect to WebSocket")
print("Connecting...")
async with aiohttp.ClientSession() as session:
async with session.ws_connect(WS_URL, headers=headers) as websocket:
ws = websocket
debug_log("WebSocket connected")
await setup_session()
server_task = asyncio.create_task(handle_server_events())
audio_event_task = asyncio.create_task(process_audio_events())
debug_log("Starting audio stream")
with sd.InputStream(samplerate=RATE, channels=CHANNELS, callback=audio_input_callback, blocksize=CHUNK, device=input_device, dtype='int16'):
debug_log("Audio stream started")
await asyncio.gather(server_task, audio_event_task)
await wait_for_audio_completion()
except aiohttp.ClientResponseError as e:
debug_log(f"HTTP error during WebSocket connection: {e.status} {e.message}")
await asyncio.sleep(1)
except aiohttp.WSServerHandshakeError as e:
debug_log(f"WebSocket handshake error: {e.status} {e.message}")
await asyncio.sleep(1)
except aiohttp.ClientConnectionError as e:
debug_log(f"Connection error: {e}")
await asyncio.sleep(1)
except Exception as e:
debug_log(f"An error occurred in main loop: {e}")
debug_log(traceback.format_exc())
await asyncio.sleep(1)
finally:
cleanup()
async def setup_session():
global ws
debug_log("ws -> session.update")
with open("prompt.txt", "r") as f:
system_prompt = f.read().strip()
transcription_model = None
if os.getenv("TRANSCRIBE").lower() == "true":
transcription_model = {
"model": "whisper-1"
}
await ws.send_str(json.dumps({
"type": "session.update",
"session": {
"instructions": system_prompt,
"modalities": ["text", "audio"],
"input_audio_format": "pcm16",
"output_audio_format": "pcm16",
"turn_detection": None,
"input_audio_transcription": transcription_model,
}
}))
debug_log("Session update sent")
async def wait_for_audio_completion():
global audio_output_stream, audio_output_queue, audio_output_buffer
while not audio_output_queue.empty() or len(audio_output_buffer) > 0 or (audio_output_stream and audio_output_stream.active):
await asyncio.sleep(0.1)
if audio_output_stream and audio_output_stream.active:
audio_output_stream.stop()
audio_output_stream.close()
audio_output_stream = None
debug_log("Audio playback completed")
def cleanup():
global audio_event_task, audio_output_stream
if audio_event_task:
audio_event_task.cancel()
executor.shutdown(wait=True)
if audio_output_stream is not None and audio_output_stream.active:
audio_output_stream.stop()
audio_output_stream.close()
while not audio_output_queue.empty():
try:
audio_output_queue.get_nowait()
except queue.Empty:
break
if __name__ == "__main__":
try:
debug_log("Starting main function")
asyncio.run(main())
except KeyboardInterrupt:
print("\nExiting...")
except Exception as e:
error_log(f"Unhandled exception: {e}")
error_log(traceback.format_exc())