Error handling
This commit is contained in:
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")
|
||||||
transcript = client.audio.transcriptions.create(
|
try:
|
||||||
model="whisper-1",
|
transcript = client.audio.transcriptions.create(
|
||||||
file=audio_file,
|
model="whisper-1",
|
||||||
response_format="text"
|
file=audio_file,
|
||||||
)
|
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…
Reference in New Issue