"""Transcription backends: NVIDIA Parakeet (onnx-asr) with faster-whisper fallback. Every caller goes through transcribe_media(), which returns the transcript contract the whole pipeline depends on: { "text": str, # full punctuated transcript "language": str, # whisper-style short code ("es", "en", ...) "segments": [ {"start": float, "end": float, "text": str, "words": [{"word": str, "start": float, "end": float}, ...]}, ], } Invariants the consumers rely on (clip cutting, karaoke subtitles, Remotion): - word["word"] carries a LEADING SPACE on true word starts; continuation fragments are merged into their base word (merge_continuation_words). - all numerics are native Python floats (json.dump of the transcript). - words sorted by start, segments chronological, absolute file timestamps. TRANSCRIBE_BACKEND env: "whisper" (default) | "parakeet". The parakeet path falls back to whisper automatically when the model errors, produces no usable words, or the detected language is outside its 25 supported European languages (e.g. Japanese/Chinese/Arabic uploads). GPU whisper in turn falls back to CPU whisper on CUDA errors (VRAM is shared with other models on the host, so loads can OOM under load). """ import os import subprocess import tempfile import threading import time from subtitles import ( get_whisper_config, WHISPER_TRANSCRIBE_PARAMS, merge_continuation_words, ) PARAKEET_MODEL_ID = "nemo-parakeet-tdt-0.6b-v3" # The 25 European languages parakeet-tdt-0.6b-v3 supports (ISO 639-1). PARAKEET_LANGS = { "bg", "hr", "cs", "da", "nl", "en", "et", "fi", "fr", "de", "el", "hu", "it", "lv", "lt", "mt", "pl", "pt", "ro", "sk", "sl", "es", "sv", "ru", "uk", } # Serializes GPU transcription across concurrent jobs so N jobs can't stack # N model contexts / decode batches in VRAM. CPU whisper stays ungated # (CTranslate2 models are thread-safe and that matches the old behavior). _ASR_GATE = threading.Semaphore(int(os.environ.get("ASR_GPU_CONCURRENCY", "1"))) class _NullGate: def __enter__(self): return self def __exit__(self, *exc): return False _NULL_GATE = _NullGate() class _TranscribeProgress: """Emits '🎙️ Transcribing… NN% (Xs)' lines at 25% steps. These are the only transcription lines cloud users see (log_view keeps them), so they must stay free of technical detail. """ def __init__(self, total_seconds): self.total = max(float(total_seconds or 0), 0.0) self.started = time.time() self.next_pct = 25 def update(self, position_seconds): if self.total <= 0: return pct = min(int(position_seconds / self.total * 100), 100) while pct >= self.next_pct and self.next_pct <= 100: elapsed = int(time.time() - self.started) print(f"🎙️ Transcribing… {self.next_pct}% ({elapsed}s)", flush=True) self.next_pct += 25 # --- whisper singleton ------------------------------------------------------ _whisper_model = None _whisper_key = None _whisper_lock = threading.Lock() # Set after a CUDA failure (e.g. VRAM exhausted by other models on the GPU) # so every later transcription goes straight to CPU instead of re-failing. _whisper_force_cpu = False def _get_whisper_model(): """Process-wide WhisperModel singleton, rebuilt if the env config changes. Keeping the model resident avoids a full reload per transcription (which on GPU would also mean re-allocating a couple of GB of VRAM per job). """ global _whisper_model, _whisper_key cfg = get_whisper_config() if _whisper_force_cpu: cfg["device"] = "cpu" cfg["compute_type"] = "int8" key = (cfg["model_size"], cfg["device"], cfg["compute_type"]) with _whisper_lock: if _whisper_model is None or _whisper_key != key: from faster_whisper import WhisperModel _whisper_model = WhisperModel(key[0], device=key[1], compute_type=key[2]) _whisper_key = key return _whisper_model, cfg["device"] def _run_whisper_once(media_path, **params): model, device = _get_whisper_model() gate = _ASR_GATE if device != "cpu" else _NULL_GATE with gate: segments, info = model.transcribe(media_path, **params) progress = _TranscribeProgress(getattr(info, "duration", 0)) materialized = [] for segment in segments: materialized.append(segment) progress.update(segment.end) # VAD trims trailing silence, so the last segment can end short of the # media duration — force the 100% line. progress.update(progress.total) return materialized, info def run_whisper_transcription(media_path, **params): """Transcribe and FULLY materialize the segments inside the GPU gate. faster-whisper returns a lazy generator — decoding happens while iterating, so the gate must wrap list(segments), not just transcribe(). Returns (segments_list, info). A CUDA failure (model load OOM or mid-decode) retries once on CPU and pins CPU for the rest of the process — the GPU is shared with other models, so a job must degrade instead of dying when VRAM runs out. """ global _whisper_model, _whisper_force_cpu try: return _run_whisper_once(media_path, **params) except RuntimeError as e: if _whisper_force_cpu or "cuda" not in str(e).lower(): raise print(f"⚠️ [ASR] whisper GPU failed ({e}) — retrying on CPU", flush=True) _whisper_force_cpu = True with _whisper_lock: _whisper_model = None # drop the GPU model to release its VRAM return _run_whisper_once(media_path, **params) def _transcribe_with_whisper(media_path): segments, info = run_whisper_transcription(media_path, **WHISPER_TRANSCRIBE_PARAMS) out_segments = [] text_parts = [] for segment in segments: words = [ {"word": w.word, "start": float(w.start), "end": float(w.end)} for w in (segment.words or []) ] out_segments.append({ "start": float(segment.start), "end": float(segment.end), "text": segment.text, "words": merge_continuation_words(words), }) text_parts.append(segment.text.strip()) return { "text": " ".join(part for part in text_parts if part), "language": info.language, "segments": out_segments, } # --- parakeet --------------------------------------------------------------- _parakeet_model = None _parakeet_lock = threading.Lock() def _get_parakeet_model(): global _parakeet_model with _parakeet_lock: if _parakeet_model is None: import onnx_asr model = onnx_asr.load_model( PARAKEET_MODEL_ID, providers=["CUDAExecutionProvider", "CPUExecutionProvider"], ) vad = onnx_asr.load_vad("silero") _parakeet_model = model.with_vad(vad).with_timestamps() return _parakeet_model def _extract_wav(media_path): """Parakeet wants 16kHz mono PCM wav; ffmpeg-extract to a temp file.""" fd, wav_path = tempfile.mkstemp(suffix=".wav", prefix="asr_") os.close(fd) cmd = [ "ffmpeg", "-y", "-loglevel", "error", "-i", media_path, "-vn", "-ac", "1", "-ar", "16000", "-c:a", "pcm_s16le", wav_path, ] subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=1800) return wav_path def _words_from_tokens(tokens, timestamps, seg_start, seg_end): """Group parakeet BPE tokens into words with absolute timestamps. Verified on the prod model: tokens already carry the leading-space word-start convention (" T", "odo", " el", ...) and timestamps are token START times in seconds relative to the VAD segment. A token without a leading space (subword continuations, punctuation like ",") belongs to the previous word — same semantics merge_continuation_words expects. Word end is inferred: next word's start, capped near the word's last token so a long inter-word silence doesn't stretch the highlight. """ words = [] last_token_ts = [] for token, ts in zip(tokens, timestamps): if not token: continue abs_ts = float(ts) + seg_start if token.startswith(" ") or not words: words.append({ "word": token if token.startswith(" ") else " " + token, "start": abs_ts, }) last_token_ts.append(abs_ts) else: words[-1]["word"] += token last_token_ts[-1] = abs_ts for i, word in enumerate(words): next_start = words[i + 1]["start"] if i + 1 < len(words) else seg_end cap = last_token_ts[i] + 0.6 word["end"] = float(max(word["start"] + 0.05, min(next_start, cap))) return words def _transcribe_with_parakeet(media_path): model = _get_parakeet_model() wav_path = _extract_wav(media_path) try: # 16kHz mono s16le wav -> 32000 bytes per second of audio. try: duration = os.path.getsize(wav_path) / 32000.0 except OSError: duration = 0.0 with _ASR_GATE: progress = _TranscribeProgress(duration) results = [] for seg in model.recognize(wav_path): results.append(seg) progress.update(float(seg.end)) progress.update(progress.total) finally: try: os.remove(wav_path) except OSError: pass out_segments = [] text_parts = [] for seg in results: seg_start = float(seg.start) seg_end = float(seg.end) seg_text = str(seg.text or "").strip() if not seg_text: continue out_segments.append({ "start": seg_start, "end": seg_end, "text": seg_text, "words": _words_from_tokens( list(seg.tokens or []), list(seg.timestamps or []), seg_start, seg_end, ), }) text_parts.append(seg_text) text = " ".join(text_parts) return { "text": text, "language": _detect_language(text), "segments": out_segments, } def _detect_language(text): """Parakeet doesn't report a language; classify the transcribed text. py3langid is pure-Python and returns ISO 639-1 codes compatible with the whisper codes the pipeline expects (thumbnail titles, Gemini prompts). """ sample = (text or "").strip() if len(sample) < 20: return "en" try: import py3langid lang, _score = py3langid.classify(sample[:4000]) return lang except Exception: return "en" def _parakeet_fallback_reason(transcript, duration_hint=None): """Return why the parakeet result is untrustworthy, or None if it's fine.""" segments = transcript.get("segments") or [] total_words = sum(len(s.get("words") or []) for s in segments) if total_words == 0: return "no words recognized" language = transcript.get("language") if language not in PARAKEET_LANGS: return f"language '{language}' outside parakeet's supported set" duration = duration_hint or (segments[-1]["end"] if segments else 0) # Real speech averages >100 wpm; under ~12 wpm on a long video means the # audio was mostly not recognized (e.g. unsupported language or music). if duration > 60 and total_words < duration * 0.2: return f"only {total_words} words in {duration:.0f}s of audio" return None # --- public entry point ----------------------------------------------------- def transcribe_media(media_path): """Transcribe with the configured backend, falling back to whisper.""" backend = os.environ.get("TRANSCRIBE_BACKEND", "whisper").strip().lower() if backend == "parakeet": try: transcript = _transcribe_with_parakeet(media_path) reason = _parakeet_fallback_reason(transcript) if reason is None: print(f"🎙️ [ASR] parakeet ok: lang={transcript['language']} " f"segments={len(transcript['segments'])}") return transcript print(f"⚠️ [ASR] parakeet result rejected ({reason}) — " f"falling back to whisper") except Exception as e: print(f"⚠️ [ASR] parakeet failed ({type(e).__name__}: {e}) — " f"falling back to whisper") return _transcribe_with_whisper(media_path)