Download recipe/kit-v3/tests/test_data_frontend.py from gowombat/tiny-voca-da: direct link, hf CLI and curl.
- Browser
- Download file 6.08 kB
-
https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/kit-v3/tests/test_data_frontend.py
- Command line
-
hf download hf://gowombat/tiny-voca-da/recipe/kit-v3/tests/test_data_frontend.py
-
curl -L -o test_data_frontend.py https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/kit-v3/tests/test_data_frontend.py
6.08 kB
| """Integration checks for preparation with the installed Danish tiny-voca frontend.""" | |
| import io | |
| import json | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| from types import SimpleNamespace | |
| import unittest | |
| from unittest.mock import patch | |
| import numpy as np | |
| import soundfile as sf | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| import prepare_coral as pc | |
| import score_audio as score | |
| from tiny_voca.text import normalize_text, phonemize | |
| def source_row(index, text): | |
| time = np.arange(24000) / 24000 | |
| wave = 0.1 * np.sin(2 * np.pi * (440 + index * 30) * time) | |
| stream = io.BytesIO() | |
| sf.write(stream, wave, 24000, format="WAV", subtype="FLOAT") | |
| return {"speaker_id": "mic", "transcription_id": index, "text": text, | |
| "audio": {"bytes": stream.getvalue(), "path": None}} | |
| class FrontendPreparationTests(unittest.TestCase): | |
| def test_number_and_abbreviation_variants_share_split(self): | |
| # v3 expands generated hundreds into separate normative words; it does | |
| # not rewrite originally supplied compounds such as "tohundrede". | |
| texts = ["Trykket er 1,2 bar.", "Trykket er en komma to bar.", | |
| "Ventil nr. 204 er lukket.", "Ventil nummer to hundrede og fire er lukket."] | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) / "data" | |
| args = pc.parse_args(["--speaker", "mic", "--check-frontend", "--limit", "4", "--output", str(root)]) | |
| with patch.object(pc, "load_rows", return_value=iter(source_row(i, t) for i, t in enumerate(texts))): | |
| pc.prepare(args, "f" * 40) | |
| records = [] | |
| for split in ("train", "validation", "test"): | |
| records.extend(json.loads(line) for line in (root / f"{split}.jsonl").read_text().splitlines()) | |
| by_text = {row["text"]: row for row in records} | |
| self.assertEqual(len(records), 4) | |
| for a, b in ((texts[0], texts[1]), (texts[2], texts[3])): | |
| self.assertEqual(by_text[a]["text_id"], by_text[b]["text_id"]) | |
| self.assertEqual(by_text[a]["split"], by_text[b]["split"]) | |
| for row in records: | |
| self.assertEqual(row["split_text"], normalize_text(row["text"], "da")) | |
| metadata = json.loads((root / "preparation.json").read_text()) | |
| self.assertEqual(metadata["split_version"], pc.DANISH_SPLIT_VERSION) | |
| self.assertTrue(metadata["admission"]["check_frontend"]) | |
| self.assertTrue(metadata["frontend"]["fingerprint"]) | |
| def test_unknown_and_overlong_are_logged_without_silent_fallback(self): | |
| texts = ["Pumpen er stoppet.", "Unsupported fixture.", "Overlong fixture."] | |
| def controlled(text, profile): | |
| if text == texts[1]: | |
| return SimpleNamespace(ids=(1, 4, 2), unknown=("fixture_phone",)) | |
| if text == texts[2]: | |
| return SimpleNamespace(ids=tuple([5] * 513), unknown=()) | |
| return phonemize(text, profile) | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) / "data" | |
| args = pc.parse_args(["--speaker", "mic", "--check-frontend", "--limit", "3", "--output", str(root)]) | |
| with patch.object(pc, "load_rows", return_value=iter(source_row(i, t) for i, t in enumerate(texts))): | |
| with patch("tiny_voca.text.phonemize", controlled): | |
| result = pc.prepare(args, "f" * 40) | |
| self.assertEqual(sum(result["accepted"].values()), 1) | |
| rejects = [json.loads(line) for line in (root / "rejected.jsonl").read_text().splitlines()] | |
| self.assertEqual(len(rejects), 2) | |
| self.assertIn("fixture_phone", rejects[0]["reason"]) | |
| self.assertIn("513", rejects[1]["reason"]) | |
| self.assertEqual(len(list((root / "audio").glob("*.wav"))), 1) | |
| def test_invalid_date_and_time_reject_only_their_records(self): | |
| texts = ["Pumpen er stoppet.", "31.02.2026", "27:65", "Ventilen er åben."] | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) / "data" | |
| args = pc.parse_args(["--speaker", "mic", "--check-frontend", "--limit", "4", "--output", str(root)]) | |
| with patch.object(pc, "load_rows", return_value=iter(source_row(i, t) for i, t in enumerate(texts))): | |
| result = pc.prepare(args, "f" * 40) | |
| self.assertEqual(sum(result["accepted"].values()), 2) | |
| rejects = [json.loads(line) for line in (root / "rejected.jsonl").read_text().splitlines()] | |
| self.assertEqual([row["text"] for row in rejects], texts[1:3]) | |
| self.assertTrue(all("frontend: invalid Danish" in row["reason"] for row in rejects)) | |
| self.assertTrue(json.loads((root / "preparation.json").read_text())["complete"]) | |
| class ASRNormalizationTests(unittest.TestCase): | |
| def test_invalid_asr_time_remains_in_metric_and_report(self): | |
| reference, hypothesis = "Klokken er tolv.", "Klokken er 27:65." | |
| clip = score.score_transcript("clip-1", reference, hypothesis) | |
| self.assertEqual(clip["normalization_error"]["id"], "clip-1") | |
| self.assertEqual(clip["normalization_error"]["ref"], reference) | |
| self.assertEqual(clip["normalization_error"]["hyp"], hypothesis) | |
| self.assertEqual(clip["normalized_hyp"], "klokken er 27 65") | |
| self.assertEqual(clip["reference_words"], 3) | |
| self.assertGreater(clip["word_errors"], 0) | |
| correct = score.score_transcript("clip-2", reference, reference) | |
| total = score.aggregate([clip, correct]) | |
| self.assertEqual(total["reference_words"], 6) | |
| self.assertEqual(total["word_errors"], clip["word_errors"]) | |
| self.assertIsNone(correct["normalization_error"]) | |
| def test_invalid_reference_has_id_and_original_text(self): | |
| with self.assertRaisesRegex(ValueError, r"Reference normalization failed for 'bad-ref'.*31\.02\.2026"): | |
| score.score_transcript("bad-ref", "31.02.2026", "enogtredive februar") | |
| if __name__ == "__main__": | |
| unittest.main() | |