Spaces:
Runtime error
Runtime error
| # File: src/stt.py | |
| # Purpose: Convert audio file to transcript using OpenAI Whisper (optional, not used on HF Spaces) | |
| try: | |
| import whisper # type: ignore[import] | |
| WHISPER_AVAILABLE = True | |
| except ImportError: | |
| WHISPER_AVAILABLE = False | |
| from pathlib import Path | |
| import sys | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from config import WHISPER_MODEL_SIZE | |
| _model = None | |
| def get_model(): | |
| if not WHISPER_AVAILABLE: | |
| raise RuntimeError("openai-whisper is not installed. Run: pip install openai-whisper") | |
| global _model | |
| if _model is None: | |
| print(f"Loading Whisper model: {WHISPER_MODEL_SIZE}") | |
| _model = whisper.load_model(WHISPER_MODEL_SIZE) | |
| return _model | |
| def transcribe_audio(audio_path: str) -> str: | |
| model = get_model() | |
| result = model.transcribe(audio_path, fp16=False, language="en") | |
| transcript = result["text"].strip() | |
| print(f"[STT] Transcript: {transcript}") | |
| return transcript | |
| def transcribe_from_bland_webhook(audio_url: str) -> str: | |
| import requests | |
| import tempfile | |
| import os | |
| response = requests.get(audio_url, timeout=30) | |
| response.raise_for_status() | |
| with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: | |
| tmp.write(response.content) | |
| tmp_path = tmp.name | |
| try: | |
| transcript = transcribe_audio(tmp_path) | |
| finally: | |
| os.unlink(tmp_path) | |
| return transcript | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| text = transcribe_audio(sys.argv[1]) | |
| print(f"Result: {text}") | |
| else: | |
| print("Usage: python src/stt.py <audio_file.wav>") |