- transcribe_backends.py: parakeet-tdt-0.6b-v3 (onnx-asr, VAD+timestamps) as selectable backend (TRANSCRIBE_BACKEND), token->word reconstruction keeping the leading-space convention, absolute timestamps, native floats, language detection via py3langid, and automatic whisper fallback (errors, empty output, or language outside parakeet's 25). - whisper singleton + ASR GPU semaphore (model stays resident; GPU decodes serialized; CPU path behavior unchanged). - ffmpeg_utils.py: central encoder args (FFMPEG_ENCODER=x264|nvenc|auto with cached probe + fallback); migrated the 9 hardcoded libx264 call sites. - saasshorts now honors WHISPER_* config instead of hardcoded base/cpu/int8. - Dockerfile: ARG GPU=1 installs CUDA user-space libs (cuBLAS/cuDNN9) + onnx-asr/onnxruntime-gpu; LD_LIBRARY_PATH + NVIDIA_DRIVER_CAPABILITIES. - Validated on prod GPU (RTX 4000 Ada, driver 580): 94s clip transcribed in 6.5s, word timestamps within ~0.1s of whisper, fallback path exercised.
286 lines
9.8 KiB
Python
286 lines
9.8 KiB
Python
"""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).
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
import threading
|
|
|
|
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()
|
|
|
|
# --- whisper singleton ------------------------------------------------------
|
|
|
|
_whisper_model = None
|
|
_whisper_key = None
|
|
_whisper_lock = threading.Lock()
|
|
|
|
|
|
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()
|
|
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_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).
|
|
"""
|
|
model, device = _get_whisper_model()
|
|
gate = _ASR_GATE if device != "cpu" else _NULL_GATE
|
|
with gate:
|
|
segments, info = model.transcribe(media_path, **params)
|
|
return list(segments), info
|
|
|
|
|
|
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:
|
|
with _ASR_GATE:
|
|
results = list(model.recognize(wav_path))
|
|
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)
|