Error handling

pull/11/head
killian 11 months ago
parent a4fcac8926
commit 556ca38e4f

@ -69,6 +69,8 @@ def queue_listener():
audio_file.extend(bytes(ast.literal_eval(message["content"]))) audio_file.extend(bytes(ast.literal_eval(message["content"])))
if "end" in message: if "end" in message:
content = stt(audio_file, message["format"]) content = stt(audio_file, message["format"])
if content == None: # If it was nothing / silence
continue
audio_file = bytearray() audio_file = bytearray()
message = {"role": "user", "type": "message", "content": content} message = {"role": "user", "type": "message", "content": content}
else: else:

@ -1 +1 @@
[{"role": "user", "type": "message", "content": ".\n"}, {"role": "user", "type": "message", "content": "Oh wait, does this work?\n"}] []

@ -8,8 +8,9 @@ import contextlib
import tempfile import tempfile
import ffmpeg import ffmpeg
import subprocess import subprocess
import openai
from openai import OpenAI from openai import OpenAI
client = OpenAI() client = OpenAI()
def convert_mime_type_to_format(mime_type: str) -> str: def convert_mime_type_to_format(mime_type: str) -> str:
@ -48,11 +49,15 @@ def export_audio_to_wav_ffmpeg(audio: bytearray, mime_type: str) -> str:
def stt(audio_bytes: bytearray, mime_type): def stt(audio_bytes: bytearray, mime_type):
with export_audio_to_wav_ffmpeg(audio_bytes, mime_type) as wav_file_path: with export_audio_to_wav_ffmpeg(audio_bytes, mime_type) as wav_file_path:
audio_file = open(wav_file_path, "rb") audio_file = open(wav_file_path, "rb")
try:
transcript = client.audio.transcriptions.create( transcript = client.audio.transcriptions.create(
model="whisper-1", model="whisper-1",
file=audio_file, file=audio_file,
response_format="text" response_format="text"
) )
except openai.BadRequestError as e:
print("openai.BadRequestError:", e)
return None
print("Exciting transcription result:", transcript) print("Exciting transcription result:", transcript)
return transcript return transcript

@ -106,8 +106,6 @@ def on_release(key):
toggle_recording(False) toggle_recording(False)
def main(): def main():
import time
time.sleep(10)
# Start the WebSocket communication in a separate asyncio event loop # Start the WebSocket communication in a separate asyncio event loop
ws_thread = threading.Thread(target=lambda: asyncio.run(websocket_communication()), daemon=True) ws_thread = threading.Thread(target=lambda: asyncio.run(websocket_communication()), daemon=True)
ws_thread.start() ws_thread.start()

Loading…
Cancel
Save