""" Streaming ASR: which streaming decoder runs a model, and the fields a streaming result carries. Shared by the Space (dependency selection, labels) and the Hub Job worker (backend choice), so it stays import-light: no torch, pandas or gradio. ``backends/streaming.py`` holds the decoders. A streaming model must transcribe audio as it arrives. The streaming board accepts two kinds: * ``incremental`` — the evaluation feeds the audio in short chunks through the model's own streaming API (cache-aware NeMo FastConformer, Nemotron ASR streaming in Transformers, Kyutai delayed streams via ``moshi``, sherpa-onnx online transducers, VibeVoice-ASR-Streaming's ``streaming_generate``). The transcript is whatever the model has emitted when the last chunk is in. * ``causal`` — the model is streaming by design (causal encoder, fixed text delay), so one pass over the whole utterance gives the same transcript as a live feed (Voxtral Realtime, Moonshine Streaming, Kyutai STT in Transformers). These run through the regular Auto backend. Every other model fails with a clear message: the streaming board does not chunk offline models. """ from __future__ import annotations import os import re FAMILY_ID = "streaming" NEMO_CACHE_AWARE = "nemo_cache_aware" TRANSFORMERS_RNNT = "transformers_rnnt" MOSHI = "moshi" SHERPA_ONNX = "sherpa_onnx" VIBEVOICE = "vibevoice" CAUSAL = "causal" CUSTOM = "custom" MODE_INCREMENTAL = "incremental" MODE_CAUSAL = "causal" FAMILY_LABELS: dict[str, str] = { NEMO_CACHE_AWARE: "NeMo cache-aware streaming", TRANSFORMERS_RNNT: "Nemotron ASR streaming (Transformers)", MOSHI: "Kyutai delayed streams (moshi)", SHERPA_ONNX: "sherpa-onnx online transducer", VIBEVOICE: "VibeVoice-ASR-Streaming (chunked generate)", CAUSAL: "Causal streaming model", CUSTOM: "Custom streaming evaluator", } FAMILY_MODES: dict[str, str] = { NEMO_CACHE_AWARE: MODE_INCREMENTAL, TRANSFORMERS_RNNT: MODE_INCREMENTAL, MOSHI: MODE_INCREMENTAL, SHERPA_ONNX: MODE_INCREMENTAL, VIBEVOICE: MODE_INCREMENTAL, CAUSAL: MODE_CAUSAL, CUSTOM: MODE_INCREMENTAL, } # config.json ``model_type`` values. RNNT_STREAM_MODEL_TYPES = frozenset({"nemotron_asr_streaming", "nemotron3_5_asr"}) CAUSAL_MODEL_TYPES = frozenset({"voxtral_realtime", "moonshine_streaming", "kyutai_speech_to_text"}) MOSHI_MODEL_TYPES = frozenset({"stt"}) # VibeVoice checkpoints share ``model_type: vibevoice`` (ASR, ASR-Streaming and TTS); the decoder # checks for the streaming tokenizer (``<|text_chunk_end|>``) and the chunk config. VIBEVOICE_MODEL_TYPES = frozenset({"vibevoice"}) # Extra CSV columns of the streaming board (after ``init.CSV_FIELDS``). STREAM_FIELDS: tuple[str, ...] = ("stream_family", "stream_mode", "stream_latency_ms") # Lookahead for models with a choice of latencies (NeMo cache-aware ``att_context_size`` right # context, Nemotron ``num_lookahead_tokens``), in 80 ms encoder frames. 6 frames is a 560 ms chunk # (the current frame plus six frames of lookahead), the setting NVIDIA documents as the # accuracy/latency middle ground and close to Voxtral Realtime's 480 ms default. DEFAULT_RIGHT_CONTEXT = 6 FRAME_MS = 80 # Live feed chunk for decoders that accept any chunk size (sherpa-onnx). FEED_CHUNK_MS = 100 def right_context() -> int: """``FFASR_STREAM_RIGHT_CONTEXT`` (80 ms frames of lookahead), default 6.""" raw = os.environ.get("FFASR_STREAM_RIGHT_CONTEXT", "").strip() try: v = int(raw) if raw else DEFAULT_RIGHT_CONTEXT except ValueError: v = DEFAULT_RIGHT_CONTEXT return max(0, min(v, 64)) def classify( model_id: str, *, model_type: str = "", library: str = "", files: list[str] | tuple[str, ...] = (), ) -> str | None: """Streaming family for a model from its Hub metadata, or ``None`` if it cannot stream.""" mt = (model_type or "").strip().lower() lib = (library or "").strip().lower() names = [str(f) for f in files or ()] if mt in RNNT_STREAM_MODEL_TYPES: return TRANSFORMERS_RNNT if mt in MOSHI_MODEL_TYPES or lib == "moshi": return MOSHI if mt in VIBEVOICE_MODEL_TYPES: return VIBEVOICE if mt in CAUSAL_MODEL_TYPES: return CAUSAL if lib == "nemo" or any(n.endswith(".nemo") for n in names): return NEMO_CACHE_AWARE onnx = [n for n in names if n.endswith(".onnx")] if "tokens.txt" in {n.rsplit("/", 1)[-1] for n in names} and any("encoder" in n for n in onnx): return SHERPA_ONNX return None def hub_metadata(model_id: str) -> tuple[str, str, list[str]]: """``(model_type, library_name, files)`` from the Hub; empty values when unavailable.""" from backends.family_resolve import infer_model_type library, files = "", [] try: from huggingface_hub import HfApi token = os.environ.get("HF_TOKEN") or os.environ.get("ds_token") or None info = HfApi(token=token).model_info(model_id) library = str(getattr(info, "library_name", "") or "") files = [s.rfilename for s in (info.siblings or [])] except Exception: pass model_type = infer_model_type(model_id) if "config.json" in files or not files else "" return model_type, library, files def resolve_family(model_id: str) -> str | None: """Streaming family for ``model_id``, read from the Hub (``None`` if it cannot stream).""" model_type, library, files = hub_metadata(model_id) return classify(model_id, model_type=model_type, library=library, files=files) def sherpa_chunk_ms(filename: str) -> int | None: """Algorithmic chunk of a sherpa-onnx streaming Zipformer from its ``chunk-`` name. Zipformer streaming chunks count 50 Hz frames (after the 2x embed subsampling), 20 ms each. """ m = re.search(r"chunk-(\d+)", filename or "") return int(m.group(1)) * 20 if m else None def family_label(family: str | None) -> str: return FAMILY_LABELS.get(str(family or ""), str(family or "")) def mode_for(family: str | None) -> str: return FAMILY_MODES.get(str(family or ""), MODE_INCREMENTAL)