From 556ca38e4ff52b371945c82a724a291c23f434e3 Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Sat, 3 Feb 2024 23:55:57 -0800 Subject: [PATCH] Error handling --- OS/01/assistant/assistant.py | 2 ++ OS/01/assistant/conversations/user.json | 2 +- OS/01/assistant/stt.py | 17 +++++++++++------ OS/01/user/user.py | 2 -- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/OS/01/assistant/assistant.py b/OS/01/assistant/assistant.py index 6342387..64e31cb 100644 --- a/OS/01/assistant/assistant.py +++ b/OS/01/assistant/assistant.py @@ -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: diff --git a/OS/01/assistant/conversations/user.json b/OS/01/assistant/conversations/user.json index 494fe84..0637a08 100644 --- a/OS/01/assistant/conversations/user.json +++ b/OS/01/assistant/conversations/user.json @@ -1 +1 @@ -[{"role": "user", "type": "message", "content": ".\n"}, {"role": "user", "type": "message", "content": "Oh wait, does this work?\n"}] \ No newline at end of file +[] \ No newline at end of file diff --git a/OS/01/assistant/stt.py b/OS/01/assistant/stt.py index 2e12d6c..07153bc 100644 --- a/OS/01/assistant/stt.py +++ b/OS/01/assistant/stt.py @@ -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 diff --git a/OS/01/user/user.py b/OS/01/user/user.py index 42bbda1..f939fc4 100644 --- a/OS/01/user/user.py +++ b/OS/01/user/user.py @@ -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()