Download source/shruti_coreml/score.py from bingokingooo/shruti-coreml: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/bingokingooo/shruti-coreml/resolve/main/source/shruti_coreml/score.py
- Command line
-
hf download hf://bingokingooo/shruti-coreml/source/shruti_coreml/score.py
-
curl -L -o score.py https://huggingface.co/bingokingooo/shruti-coreml/resolve/main/source/shruti_coreml/score.py
3 kB
| """Score paired transcriptions with the published Hindi WER normalization.""" | |
| import unicodedata | |
| import numpy as np | |
| def words(text): | |
| clean = unicodedata.normalize("NFC", text).lower() | |
| return "".join(ch for ch in clean if not unicodedata.category(ch).startswith("P")).split() | |
| def edit_distance(reference, hypothesis): | |
| previous = list(range(len(hypothesis) + 1)) | |
| for i, ref in enumerate(reference, start=1): | |
| row = [i] | |
| for j, hyp in enumerate(hypothesis, start=1): | |
| row.append(min(row[-1] + 1, previous[j] + 1, previous[j - 1] + (ref != hyp))) | |
| previous = row | |
| return previous[-1] | |
| def score_paired(rows, resamples=1000, seed=42, cluster_key=None): | |
| measurements = [] | |
| for item in rows: | |
| reference = words(item["reference"]) | |
| measurements.append([ | |
| len(reference), | |
| edit_distance(reference, words(item["baseline"]["text"])), | |
| edit_distance(reference, words(item["coreml"]["text"])), | |
| ]) | |
| metrics = np.asarray(measurements, np.int64) | |
| if not len(metrics): | |
| raise ValueError("No rows to score") | |
| denom, base, coreml = metrics.sum(0) | |
| if not denom: | |
| return {"clips": len(rows), "baseline_words_on_silence": int(base), "coreml_words_on_silence": int(coreml)} | |
| units = metrics | |
| if cluster_key: | |
| clusters = {} | |
| for row, values in zip(rows, metrics): | |
| key = row[cluster_key] | |
| clusters[key] = clusters.get(key, np.zeros(3, np.int64)) + values | |
| units = np.asarray(list(clusters.values())) | |
| prng = np.random.default_rng(seed) | |
| indexes = prng.integers(0, len(units), (resamples, len(units))) | |
| draws = units[indexes].sum(1) | |
| draws = draws[draws[:, 0] > 0] | |
| intervals = (draws[:, 2] - draws[:, 1]) / draws[:, 0] * 100 | |
| return { | |
| "clips": len(rows), "words": int(denom), | |
| "baseline_wer": float(base / denom * 100), "coreml_wer": float(coreml / denom * 100), | |
| "difference_wer_points": float((coreml - base) / denom * 100), | |
| "paired_95_percent_interval": np.quantile(intervals, [0.025, 0.975]).tolist(), | |
| "exact_text_matches": sum(row["baseline"]["text"] == row["coreml"]["text"] for row in rows), | |
| } | |
| def score_lattices(rows): | |
| """Corpus OIWER calculated by the official Voice Arena package.""" | |
| import importlib.metadata | |
| from voi_oiwer import oiwer | |
| out = {"clips": len(rows), "scorer": "voi-oiwer", "scorer_version": importlib.metadata.version("voi-oiwer")} | |
| for engine in ("baseline", "coreml"): | |
| errors = total = 0 | |
| for row in rows: | |
| result = oiwer(row[engine]["text"], row["lattice"], "hindi") | |
| errors += sum(result[4]) | |
| total += result[5] | |
| out[f"{engine}_errors"] = int(errors) | |
| out[f"{engine}_reference_words"] = int(total) | |
| out[f"{engine}_oiwer"] = float(errors / total * 100) | |
| out["difference_oiwer_points"] = out["coreml_oiwer"] - out["baseline_oiwer"] | |
| return out | |