""" Speaker diarization (multi-talker) benchmark: tasks, conditions, metrics, CSV columns. Shared by the Space UI and the Hub Job worker, so it must stay import-light (no torch, pandas, or gradio). The benchmark crosses three SNR levels with two speech-overlap regimes: high / mid / low SNR x without overlap / with overlap -> 6 packed conditions Every per-condition metric is stored in its own CSV column named ``_`` (e.g. ``der_low_ovl``). Error rates are fractions (0-1) like the ASR WER columns. """ from __future__ import annotations from collections.abc import Sequence from dataclasses import dataclass # --------------------------------------------------------------------------- # Tasks # --------------------------------------------------------------------------- TASK_ASR = "asr" TASK_DIARIZATION = "diarization" # Streaming ASR: the same WER conditions as ASR, but the model hears the audio chunk by chunk # (or is causal by design) and the results go to their own board (streaming_results.py). TASK_STREAMING = "streaming" TASKS: tuple[str, ...] = (TASK_ASR, TASK_DIARIZATION, TASK_STREAMING) TASK_UI_CHOICES: tuple[tuple[str, str], ...] = ( ("ASR (transcription)", TASK_ASR), ("Streaming ASR (live transcription)", TASK_STREAMING), ("Speaker diarization (multiple talkers)", TASK_DIARIZATION), ) def normalize_task(raw: str | None) -> str: s = (raw or "").strip().lower() return s if s in TASKS else TASK_ASR def is_diarization(task: str | None) -> bool: return normalize_task(task) == TASK_DIARIZATION def is_streaming(task: str | None) -> bool: return normalize_task(task) == TASK_STREAMING # --------------------------------------------------------------------------- # Conditions # --------------------------------------------------------------------------- SNR_LEVELS: tuple[tuple[str, str], ...] = ( ("high", "High SNR"), ("mid", "Mid SNR"), ("low", "Low SNR"), ) SNR_LABELS: dict[str, str] = dict(SNR_LEVELS) OVERLAP_SPLITS: tuple[tuple[str, str], ...] = ( ("noovl", "Without overlap"), ("ovl", "With overlap"), ) OVERLAP_LABELS: dict[str, str] = dict(OVERLAP_SPLITS) @dataclass(frozen=True) class DiarCondition: key: str snr: str overlap: str label: str short: str packed_path: str CONDITIONS: tuple[DiarCondition, ...] = tuple( DiarCondition( key=f"{snr}_{ovl}", snr=snr, overlap=ovl, label=f"{snr_label} · {ovl_label.lower()}", short=f"{snr_label.split()[0]} · {'ovl' if ovl == 'ovl' else 'no ovl'}", packed_path=f"packed/diar_{snr}_{ovl}.pt", ) for snr, snr_label in SNR_LEVELS for ovl, ovl_label in OVERLAP_SPLITS ) CONDITIONS_BY_KEY: dict[str, DiarCondition] = {c.key: c for c in CONDITIONS} CONDITION_KEYS: tuple[str, ...] = tuple(c.key for c in CONDITIONS) # Moderator checkbox labels (display, condition_key). CONDITION_UI_CHOICES: tuple[tuple[str, str], ...] = tuple((c.label, c.key) for c in CONDITIONS) def condition_key(snr: str, overlap: str) -> str: return f"{snr}_{overlap}" def resolve_condition_keys(keys: Sequence[str] | None) -> tuple[str, ...]: """Validated condition keys; ``None`` / empty / all-invalid means the full set.""" if not keys: return CONDITION_KEYS valid = [k for k in keys if k in CONDITIONS_BY_KEY] return tuple(valid) if valid else CONDITION_KEYS def is_full_condition_set(keys: Sequence[str] | None) -> bool: return set(resolve_condition_keys(keys)) == set(CONDITION_KEYS) # --------------------------------------------------------------------------- # Metrics # --------------------------------------------------------------------------- @dataclass(frozen=True) class DiarMetric: key: str short: str label: str lower_is_better: bool METRICS: tuple[DiarMetric, ...] = ( DiarMetric("der", "DER", "Diarization error rate", True), DiarMetric("jer", "JER", "Jaccard error rate", True), DiarMetric("miss", "Missed", "Missed speech (share of reference speech)", True), DiarMetric("fa", "False alarm", "False-alarm speech (share of reference speech)", True), DiarMetric("conf", "Confusion", "Speaker confusion (share of reference speech)", True), DiarMetric("spkacc", "Spk. count acc.", "Recordings with the correct number of speakers", False), ) METRICS_BY_KEY: dict[str, DiarMetric] = {m.key: m for m in METRICS} METRIC_KEYS: tuple[str, ...] = tuple(m.key for m in METRICS) # Metrics a user can rank by. DER is the headline. RANK_METRICS: tuple[str, ...] = ("der", "jer") DER_COMPONENTS: tuple[str, ...] = ("miss", "fa", "conf") # Scoring policy, surfaced in the About tab so numbers are interpretable. SCORING_COLLAR_S = 0.0 SCORING_FRAME_S = 0.01 def metric_column(metric: str, cond_key: str) -> str: return f"{metric}_{cond_key}" METRIC_COLUMNS: tuple[str, ...] = tuple( metric_column(m, c) for c in CONDITION_KEYS for m in METRIC_KEYS ) CSV_FIELDS: list[str] = [ "model_id", "eval_family", *METRIC_COLUMNS, "num_samples", "eval_audio_seconds", "eval_wall_time_s", "eval_rtf", "num_params", "submission_notes", "contact_email", "submitted_at", ] # --------------------------------------------------------------------------- # Leaderboard views # --------------------------------------------------------------------------- OVERLAP_VIEW_ALL = "all" OVERLAP_VIEWS: tuple[tuple[str, str], ...] = ( ("All conditions", OVERLAP_VIEW_ALL), ("Without overlap", "noovl"), ("With overlap", "ovl"), ) def overlaps_for_view(view: str | None) -> tuple[str, ...]: """Overlap splits averaged by a leaderboard view (``all`` averages both).""" v = (view or OVERLAP_VIEW_ALL).strip().lower() if v in OVERLAP_LABELS: return (v,) return tuple(k for k, _ in OVERLAP_SPLITS) def view_label(view: str | None) -> str: for label, key in OVERLAP_VIEWS: if key == (view or OVERLAP_VIEW_ALL): return label return OVERLAP_VIEWS[0][0]