Spaces:
Sleeping
Sleeping
| """ | |
| STT Module - faster-whisper medium int8 on CPU | |
| Transcribes Hindi audio to text. | |
| Faster than openai/whisper via API, runs locally, no API cost. | |
| """ | |
| from faster_whisper import WhisperModel | |
| import numpy as np | |
| import soundfile as sf | |
| import io | |
| print("Loading Whisper model (medium int8)... first run downloads ~500MB") | |
| whisper_model = WhisperModel("medium", device="cpu", compute_type="int8") | |
| print("Whisper model loaded.") | |
| def stt_whisper(audio_array: np.ndarray, sample_rate: int) -> str: | |
| """ | |
| Convert Hindi audio array to text. | |
| Keeps same function signature as existing stt_whisper() in app.py. | |
| """ | |
| try: | |
| buf = io.BytesIO() | |
| sf.write(buf, audio_array, sample_rate, format="WAV", subtype="PCM_16") | |
| buf.seek(0) | |
| segments, info = whisper_model.transcribe( | |
| buf, | |
| language="hi", | |
| beam_size=5 | |
| ) | |
| transcript = " ".join([s.text for s in segments]).strip() | |
| print(f"DEBUG STT transcript: {transcript}") | |
| print(f"DEBUG STT detected language: {info.language} confidence: {info.language_probability:.2f}") | |
| return transcript | |
| except Exception as e: | |
| print(f"STT error: {e}") | |
| return "" | |