"""Telegram voice notes → text with faster-whisper, locally on the VM (no audio leaves it). The model loads on first use and stays in memory.""" import threading _model = None _lock = threading.Lock() def transcribe(path: str, model_name: str = "small") -> str: global _model with _lock: if _model is None: from faster_whisper import WhisperModel # heavy import: only when a voice note comes _model = WhisperModel(model_name, device="cpu", compute_type="int8") segments, _ = _model.transcribe(path, language="ru", vad_filter=True, beam_size=1) return " ".join(s.text.strip() for s in segments).strip()