"""Recompute per-clip CER and SECS for a folder of released clips. Usage: python score_released.py --wavs stress_set/audio --manifest stress_set/manifest.tsv \ --reference voices/reference_merged.wav --out stress_set/scores.csv [--no-cer] [--no-secs] CER : hishab/titu_stt_bn_conformer_large (NeMo) transcripts vs the manifest text, both passed through the same Stage-2 normalisation used at synthesis (bn_norm.for_cer: digits->words, honorific expansion, unicode normalisation, punctuation/whitespace/zero-width removal). Character-level Levenshtein / len(ref). SECS: ECAPA-TDNN (speechbrain/spkrec-ecapa-voxceleb) cosine similarity between the clip and the reference voice, both resampled to 16 kHz. manifest.tsv columns: id text group. Clips are .wav. Requires: bn_norm.py next to this script (shipped in the model repo, inference/bn_norm.py). """ import os, sys, glob, csv, argparse, numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) def cer(r, h): d = np.zeros((len(r) + 1, len(h) + 1), int) for i in range(len(r) + 1): d[i][0] = i for j in range(len(h) + 1): d[0][j] = j for i in range(1, len(r) + 1): for j in range(1, len(h) + 1): d[i][j] = min(d[i-1][j] + 1, d[i][j-1] + 1, d[i-1][j-1] + (r[i-1] != h[j-1])) return d[len(r)][len(h)] / max(1, len(r)) def main(): ap = argparse.ArgumentParser(); ap.add_argument("--wavs", required=True); ap.add_argument("--manifest", required=True) ap.add_argument("--reference", default=None); ap.add_argument("--out", required=True) ap.add_argument("--no-cer", action="store_true"); ap.add_argument("--no-secs", action="store_true"); a = ap.parse_args() rows = {l.split("\t")[0]: l.rstrip("\n").split("\t") for l in open(a.manifest, encoding="utf-8") if "\t" in l} files = sorted(glob.glob(os.path.join(a.wavs, "*.wav"))); ids = [os.path.basename(p)[:-4] for p in files] hyps, cers, secs = [""] * len(files), [""] * len(files), [""] * len(files) if not a.no_cer: import torch, nemo.collections.asr as nemo_asr from bn_norm import for_cer as norm m = nemo_asr.models.ASRModel.from_pretrained("hishab/titu_stt_bn_conformer_large", map_location="cuda" if torch.cuda.is_available() else "cpu").eval() hyps = [(o.text if hasattr(o, "text") else o) for o in m.transcribe(files, batch_size=16)] cers = [f"{cer(norm(rows[i][1]), norm(h)):.4f}" for i, h in zip(ids, hyps)] if not a.no_secs: import torch, torchaudio from speechbrain.inference.speaker import EncoderClassifier dev = "cuda" if torch.cuda.is_available() else "cpu" enc = EncoderClassifier.from_hparams(source="speechbrain/spkrec-ecapa-voxceleb", run_opts={"device": dev}) def emb(p): w, sr = torchaudio.load(p); w = w.mean(0, keepdim=True) if sr != 16000: w = torchaudio.functional.resample(w, sr, 16000) with torch.no_grad(): return enc.encode_batch(w.to(dev)).squeeze().cpu().numpy() ref = emb(a.reference) secs = [] for p in files: e = emb(p); secs.append(f"{float(e @ ref / (np.linalg.norm(e) * np.linalg.norm(ref))):.4f}") with open(a.out, "w", encoding="utf-8", newline="") as f: w = csv.writer(f); w.writerow(["id", "group", "text", "asr_hypothesis", "cer", "secs"]) for i, h, c, s in zip(ids, hyps, cers, secs): w.writerow([i, rows[i][2], rows[i][1], h, c, s]) import collections g = collections.defaultdict(lambda: [[], []]) for i, c, s in zip(ids, cers, secs): if c: g[rows[i][2]][0].append(float(c)) if s: g[rows[i][2]][1].append(float(s)) for k in sorted(g): c, s = g[k] print(f"{k:28s} n={max(len(c), len(s)):3d}" + (f" CER mean={100*np.mean(c):5.2f}% median={100*np.median(c):5.2f}%" if c else "") + (f" SECS={np.mean(s):.3f}" if s else "")) if __name__ == "__main__": main()