Spaces:
Runtime error
Runtime error
File size: 1,641 Bytes
5348e91 2c970f4 5348e91 2c970f4 5348e91 2c970f4 5348e91 2c970f4 5348e91 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# modules/stt_handler.py
import speech_recognition as sr
import os
def transcribe_audio(audio_filepath):
if not audio_filepath or not os.path.exists(audio_filepath):
print("STT Error: No audio file provided or file does not exist.")
return "[Transcription error: Invalid audio file path]"
recognizer = sr.Recognizer()
try:
with sr.AudioFile(audio_filepath) as source:
audio_data = recognizer.record(source)
print("Transcribing with Whisper...")
# Use Whisper for transcription
text = recognizer.recognize_whisper(audio_data, language="english")
print(f"User transcribed as: {text}")
return text
except sr.UnknownValueError:
print("STT Error: Whisper could not understand the audio.")
return "[Could not understand audio - please try speaking more clearly]"
except sr.RequestError as e:
print(f"STT Error: Could not request results from Whisper service; {e}")
return "[Transcription service temporarily unavailable - please try again]"
except ImportError as e:
print(f"STT Error: Whisper not available: {e}")
return "[Speech recognition not available in this environment]"
except Exception as e:
print(f"STT Error: An unexpected error occurred during transcription: {e}")
return f"[Transcription error: Please try again]"
finally:
if os.path.exists(audio_filepath):
try:
os.remove(audio_filepath)
except OSError as e:
print(f"Error deleting temp audio file {audio_filepath}: {e}") |