File size: 1,222 Bytes
4cded81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
"""
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 ""