Ai-interview / modules /stt_handler.py
Parimal Kalpande
update
2c970f4
# 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}")