Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download evaluation/diarization.py from treble-technologies/ffasr: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/treble-technologies/ffasr/resolve/main/evaluation/diarization.py
- Command line
-
hf download hf://spaces/treble-technologies/ffasr/evaluation/diarization.py
-
curl -L -o diarization.py https://huggingface.co/spaces/treble-technologies/ffasr/resolve/main/evaluation/diarization.py
3.24 kB
| """Run a speaker-diarization model over the packed diarization conditions (Hub Job side).""" | |
| from __future__ import annotations | |
| from collections.abc import Callable | |
| from diarization_config import ( | |
| TASK_DIARIZATION, | |
| is_full_condition_set, | |
| metric_column, | |
| resolve_condition_keys, | |
| ) | |
| from evaluation.runtime import ( | |
| apply_cpu_thread_settings_once, | |
| disable_broken_torchcodec, | |
| resolve_eval_devices, | |
| ) | |
| def run_diarization_evaluation( | |
| model_id: str, | |
| family_id: str = "auto", | |
| progress_cb: Callable[[int, int, str], None] | None = None, | |
| custom_script: str = "", | |
| condition_keys: list[str] | None = None, | |
| ) -> dict: | |
| """Score ``model_id`` on each diarization condition. | |
| Returns ``task``, ``model_id``, ``eval_family``, ``num_params``, one | |
| ``<metric>_<condition>`` field per metric (see ``diarization_config.METRICS``), and the | |
| same timing fields as ASR runs (``eval_rtf`` etc.). | |
| """ | |
| import torch | |
| from backends.diarization import build_diarizer | |
| from benchmark.diarization import evaluate_condition_timed, load_packed_condition | |
| apply_cpu_thread_settings_once() | |
| disable_broken_torchcodec() | |
| device_str, _device_int = resolve_eval_devices() | |
| keys = resolve_condition_keys(condition_keys) | |
| totals: dict[str, int] = {} | |
| for ck in keys: | |
| try: | |
| totals[ck] = len(load_packed_condition(ck)) | |
| except Exception: | |
| totals[ck] = 0 | |
| grand_total = sum(totals.values()) | |
| if progress_cb is not None: | |
| progress_cb(0, grand_total, "") | |
| diarize, cleanup, family_label = build_diarizer( | |
| model_id, family_id, device_str, custom_script=custom_script | |
| ) | |
| try: | |
| results: dict = { | |
| "task": TASK_DIARIZATION, | |
| "model_id": model_id, | |
| "eval_family": family_label, | |
| "num_params": int(getattr(diarize, "_num_params", 0) or 0), | |
| } | |
| total_audio_s = 0.0 | |
| total_infer_s = 0.0 | |
| total_samples = 0 | |
| done = 0 | |
| for ck in keys: | |
| base = done | |
| def _cb(cond: str, i: int, _n: int, _base: int = base) -> None: | |
| if progress_cb is not None: | |
| progress_cb(_base + i, grand_total, cond) | |
| metrics, n, audio_s, infer_s = evaluate_condition_timed(diarize, ck, progress_cb=_cb) | |
| for metric, value in metrics.items(): | |
| results[metric_column(metric, ck)] = value | |
| total_audio_s += audio_s | |
| total_infer_s += infer_s | |
| total_samples = max(total_samples, n) | |
| done += n | |
| results["eval_conditions"] = list(keys) | |
| results["eval_full_run"] = is_full_condition_set(condition_keys) | |
| results["num_samples"] = total_samples | |
| results["eval_audio_seconds"] = round(total_audio_s, 3) | |
| results["eval_wall_time_s"] = round(total_infer_s, 3) | |
| results["eval_rtf"] = round(total_audio_s / total_infer_s, 4) if total_infer_s > 1e-6 else "" | |
| if progress_cb is not None: | |
| progress_cb(done, grand_total, "") | |
| return results | |
| finally: | |
| cleanup() | |
| if device_str == "cuda" and torch.cuda.is_available(): | |
| torch.cuda.empty_cache() | |