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"])))
if "end" in message:
content = stt(audio_file, message["format"])
if content == None: # If it was nothing / silence
continue
audio_file = bytearray()
message = {"role": "user", "type": "message", "content": content}
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 ffmpeg
import subprocess
import openai
from openai import OpenAI
client = OpenAI()
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):
with export_audio_to_wav_ffmpeg(audio_bytes, mime_type) as wav_file_path:
audio_file = open(wav_file_path, "rb")
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
try:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
except openai.BadRequestError as e:
print("openai.BadRequestError:", e)
return None
print("Exciting transcription result:", transcript)
return transcript

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

Loading…
Cancel
Save