Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download tests/test_init_rows.py from treble-technologies/ffasr: direct link, hf CLI and curl.
- Browser
- Download file 4.56 kB
-
https://huggingface.co/spaces/treble-technologies/ffasr/resolve/main/tests/test_init_rows.py
- Command line
-
hf download hf://spaces/treble-technologies/ffasr/tests/test_init_rows.py
-
curl -L -o test_init_rows.py https://huggingface.co/spaces/treble-technologies/ffasr/resolve/main/tests/test_init_rows.py
4.56 kB
| """Legacy CSV normalization, merge, display rounding, and the results cache.""" | |
| from __future__ import annotations | |
| import os | |
| import tempfile | |
| import time | |
| import init | |
| from storage import HISTORY_PATH, RESULTS_PATH | |
| from utils_display import AutoEvalColumn | |
| def test_normalize_legacy_csv_row_maps_old_wer_keys(): | |
| row = {"model_id": "acme/old", "wer_clean": "0.125", "wer_high_snr": "0.5"} | |
| init.normalize_legacy_csv_row(row) | |
| assert row["wer_anechoic_speech"] == "0.125" | |
| assert row["wer_realistic_high_snr"] == "0.5" | |
| def test_display_rounds_numeric_columns_and_marks_custom_eval(): | |
| df = init._display_df_from_raw_rows( | |
| [ | |
| { | |
| "model_id": "openai/whisper-tiny", | |
| "eval_family": "auto", | |
| "wer_anechoic_speech": "0.123456", | |
| "wer_realistic_high_snr": "0.2", | |
| "wer_realistic_mid_snr": "0.2", | |
| "wer_realistic_low_snr": "0.2", | |
| "eval_rtf": "211.1197", | |
| "num_params": "2038000000", | |
| "custom_eval": "1", | |
| } | |
| ] | |
| ) | |
| assert df[AutoEvalColumn.model.name].iloc[0].endswith(" †") | |
| assert df[AutoEvalColumn.wer_anechoic.name].iloc[0] == 12.35 | |
| assert df[AutoEvalColumn.eval_rtf.name].iloc[0] == 211.1 | |
| assert df[AutoEvalColumn.params_m.name].iloc[0] == 2.04 | |
| assert str(df[AutoEvalColumn.eval_rtf.name].dtype) == "Float64" | |
| assert str(df[AutoEvalColumn.wer_anechoic.name].dtype) == "Float64" | |
| def _csv_file(text: str) -> str: | |
| handle = tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False, suffix=".csv") | |
| handle.write(text) | |
| handle.close() | |
| return handle.name | |
| def test_merge_writes_scores_without_email(monkeypatch): | |
| files: dict[str, bytes] = {} | |
| private: list[dict] = [] | |
| def fake_download(path, **kwargs): | |
| blob = files.get(path) | |
| if blob is None: | |
| raise FileNotFoundError(path) | |
| return _csv_file(blob.decode("utf-8")) | |
| def fake_upload(bucket, *, add=None, **kwargs): | |
| for src, dst in add or []: | |
| files[dst] = src if isinstance(src, (bytes, bytearray)) else open(src, "rb").read() | |
| monkeypatch.setattr(init, "download_bucket_file", fake_download) | |
| monkeypatch.setattr(init, "upload_to_bucket", fake_upload) | |
| monkeypatch.setattr( | |
| "storage.append_private_submission", | |
| lambda **kwargs: private.append(kwargs), | |
| ) | |
| init.invalidate_results_cache() | |
| init.merge_leaderboard_row_from_eval_result( | |
| { | |
| "model_id": "openai/whisper-tiny", | |
| "eval_family": "auto", | |
| "wer_clean": 0.25, | |
| "wer_measured": 0.4, | |
| "custom_eval": "1", | |
| }, | |
| "2026-01-01T00:00:00Z", | |
| submission_notes="bench note", | |
| contact_email="submitter@example.com", | |
| job_id="job123", | |
| ) | |
| init.merge_leaderboard_row_from_eval_result( | |
| { | |
| "model_id": "openai/whisper-tiny", | |
| "eval_family": "auto", | |
| "wer_clean": 0.1, | |
| }, | |
| "2026-01-02T00:00:00Z", | |
| contact_email="submitter@example.com", | |
| job_id="job123", | |
| ) | |
| board = files[RESULTS_PATH].decode("utf-8") | |
| assert "contact_email" not in board.splitlines()[0] | |
| assert "submitter@example.com" not in board | |
| assert "custom_eval" in board.splitlines()[0] | |
| assert "0.4" in board | |
| assert "0.1" in board | |
| assert private and private[0]["contact_email"] == "submitter@example.com" | |
| assert private[0]["job_id"] == "job123" | |
| history = files.get(HISTORY_PATH, b"").decode("utf-8") | |
| assert "submitter@example.com" not in history | |
| def test_results_cache_respects_ttl(monkeypatch): | |
| calls = {"n": 0} | |
| def fake_download(path, **kwargs): | |
| calls["n"] += 1 | |
| return _csv_file("model_id,eval_family\nopenai/whisper-tiny,auto\n") | |
| monkeypatch.setattr(init, "download_bucket_file", fake_download) | |
| init.invalidate_results_cache() | |
| init.load_raw_results() | |
| init.load_raw_results() | |
| assert calls["n"] == 1 | |
| init._raw_results_cached_at = time.monotonic() - 10_000 | |
| init.load_raw_results() | |
| assert calls["n"] == 2 | |
| init.load_raw_results(refresh=True) | |
| assert calls["n"] == 3 | |
| init.invalidate_results_cache() | |
| init.load_raw_results() | |
| assert calls["n"] == 4 | |
| os.environ["FFASR_RESULTS_CACHE_TTL_S"] = "0" | |
| try: | |
| init.load_raw_results() | |
| assert calls["n"] == 5 | |
| finally: | |
| os.environ.pop("FFASR_RESULTS_CACHE_TTL_S", None) | |
| init.invalidate_results_cache() | |