File size: 2,521 Bytes
496f8c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Re-score WER/CER of Lua Nova V1 eval WAVs with faster-whisper large-v3 (same normalization as tts-eval).

Run with experiments/tts-eval/.venv python after sourcing tts-eval/scripts/env.ps1.
Writes eval/scores_asr_large.json and merges `wer_large`/`cer_large` into eval/scores.json summaries.
"""
from __future__ import annotations

import json
import sys
from pathlib import Path

import numpy as np

sys.path.insert(0, r"C:\dev\AjudaEstudos\experiments\tts-eval\src")
from faster_whisper import WhisperModel  # noqa: E402
from tts_eval.env_setup import configure_env, load_audio_dual_rate  # noqa: E402
from tts_eval.text_ptbr import cer, wer  # noqa: E402

EXP = Path(__file__).resolve().parents[1]
EVAL = EXP / "eval"
MODEL = "large-v3"


def main() -> None:
    configure_env()
    model = WhisperModel(MODEL, device="cuda", compute_type="float16")
    scores = json.loads((EVAL / "scores.json").read_text(encoding="utf-8"))
    import importlib.util
    spec = importlib.util.spec_from_file_location("es", Path(__file__).with_name("eval_score.py"))
    es = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(es)
    txt = es.texts()

    out: dict = {"asr_model": f"faster-whisper {MODEL} (fp16, beam 5, lang=pt, vad)"}
    for key in scores:
        system, split = key.split("/")
        rows = []
        for wid, text in txt[split].items():
            p = EVAL / system / split / f"{wid}.wav"
            if not p.exists():
                continue
            _, _, w16 = load_audio_dual_rate(p)
            segs, _ = model.transcribe(w16, language="pt", vad_filter=True, beam_size=5,
                                       condition_on_previous_text=False, temperature=0.0)
            hyp = " ".join(s.text.strip() for s in segs).strip()
            rows.append({"id": wid, "asr_text": hyp, "wer": round(wer(text, hyp), 4), "cer": round(cer(text, hyp), 4)})
        summ = {"wer": round(float(np.mean([r["wer"] for r in rows])), 4),
                "cer": round(float(np.mean([r["cer"] for r in rows])), 4), "n": len(rows)}
        out[key] = {"summary": summ, "items": rows}
        scores[key]["summary"]["wer_large"] = summ["wer"]
        scores[key]["summary"]["cer_large"] = summ["cer"]
        print(key, summ, flush=True)

    (EVAL / "scores_asr_large.json").write_text(json.dumps(out, indent=2, ensure_ascii=False), encoding="utf-8")
    (EVAL / "scores.json").write_text(json.dumps(scores, indent=2, ensure_ascii=False), encoding="utf-8")


if __name__ == "__main__":
    main()