From 375192afe65b0efd5655dd83366fa32907e71d0e Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Fri, 9 Feb 2024 23:45:58 -0800 Subject: [PATCH] Ready for whisper --- OS/01/start.sh | 4 ---- OS/01/stt.py | 37 +++++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/OS/01/start.sh b/OS/01/start.sh index 8ef3bfd..e4a5df7 100755 --- a/OS/01/start.sh +++ b/OS/01/start.sh @@ -26,10 +26,6 @@ SERVER_PORT=$(echo $SERVER_URL | grep -oE "[0-9]+") if [ -n "$SERVER_PORT" ]; then lsof -ti tcp:$SERVER_PORT | xargs kill fi -DEVICE_PORT=$(echo $DEVICE_URL | grep -oE "[0-9]+") -if [ -n "$DEVICE_PORT" ]; then - lsof -ti tcp:$DEVICE_PORT | xargs kill -fi ### START diff --git a/OS/01/stt.py b/OS/01/stt.py index d53134a..e4b282b 100644 --- a/OS/01/stt.py +++ b/OS/01/stt.py @@ -49,16 +49,29 @@ def stt_bytes(audio_bytes: bytearray, mime_type="audio/wav"): return stt_wav(wav_file_path) def stt_wav(wav_file_path: str): - audio_file = open(wav_file_path, "rb") - 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("Transcription result:", transcript) - return transcript + if os.getenv('ALL_LOCAL') == 'False': + audio_file = open(wav_file_path, "rb") + 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("Transcription result:", transcript) + return transcript + else: + # Local whisper here, given `wav_file_path` + pass + +def stt(input_data, mime_type="audio/wav"): + if isinstance(input_data, str): + return stt_wav(input_data) + elif isinstance(input_data, bytearray): + return stt_bytes(input_data, mime_type) + else: + raise ValueError("Input data should be either a path to a wav file (str) or audio bytes (bytearray)") \ No newline at end of file