Datasets:
cleanup: training scripts live in deploy/, not the dataset repo
Browse files- eval_ears_wer.py +0 -88
eval_ears_wer.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
# /// script
|
| 2 |
-
# dependencies = ["transformers>=4.52.1", "peft>=0.13", "datasets==2.21.0",
|
| 3 |
-
# "soundfile", "librosa", "torchaudio", "jiwer", "huggingface_hub>=0.22"]
|
| 4 |
-
# ///
|
| 5 |
-
"""Per-language WER/CER gate for the Granite ears adapter on Bocalantics test.
|
| 6 |
-
|
| 7 |
-
Samples PER_LANG clips per language from the test split (streamed + shuffled so the
|
| 8 |
-
3 langs mix), transcribes with base+adapter, and reports WER/CER per language.
|
| 9 |
-
Expectation: Wolof strongest (most data), Pulaar weakest.
|
| 10 |
-
"""
|
| 11 |
-
import io
|
| 12 |
-
import json
|
| 13 |
-
import os
|
| 14 |
-
|
| 15 |
-
BASE = os.environ.get("BASE_MODEL", "ibm-granite/granite-4.0-1b-speech")
|
| 16 |
-
ADAPTER = os.environ.get("ADAPTER", "MOH749/yaatal-wa-ears-granite")
|
| 17 |
-
DATASET = os.environ.get("DATASET", "MOH749/Bocalantics")
|
| 18 |
-
PER_LANG = int(os.environ.get("PER_LANG", "50"))
|
| 19 |
-
INSTR = "Please transcribe the following audio to text<|audio|>"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def main():
|
| 23 |
-
import torch
|
| 24 |
-
import soundfile as sf
|
| 25 |
-
import jiwer
|
| 26 |
-
from datasets import load_dataset, Audio
|
| 27 |
-
from transformers import GraniteSpeechForConditionalGeneration, GraniteSpeechProcessor
|
| 28 |
-
from peft import PeftModel
|
| 29 |
-
|
| 30 |
-
proc = GraniteSpeechProcessor.from_pretrained(BASE)
|
| 31 |
-
tok = proc.tokenizer
|
| 32 |
-
sr_t = proc.audio_processor.sampling_rate
|
| 33 |
-
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
-
model = GraniteSpeechForConditionalGeneration.from_pretrained(
|
| 35 |
-
BASE, torch_dtype=torch.bfloat16, device_map=dev)
|
| 36 |
-
model = PeftModel.from_pretrained(model, ADAPTER)
|
| 37 |
-
model.eval()
|
| 38 |
-
print(f"[eval] base={BASE} adapter={ADAPTER} per_lang={PER_LANG}")
|
| 39 |
-
|
| 40 |
-
ds = load_dataset(DATASET, split="test", streaming=True).cast_column(
|
| 41 |
-
"audio", Audio(decode=False)).shuffle(seed=7, buffer_size=3000)
|
| 42 |
-
buckets, seen = {}, 0
|
| 43 |
-
for ex in ds:
|
| 44 |
-
seen += 1
|
| 45 |
-
lg = ex.get("lang") or "?"
|
| 46 |
-
b = buckets.setdefault(lg, [])
|
| 47 |
-
if len(b) < PER_LANG:
|
| 48 |
-
b.append(ex)
|
| 49 |
-
if len(buckets) >= 3 and all(len(v) >= PER_LANG for v in buckets.values()):
|
| 50 |
-
break
|
| 51 |
-
if seen > PER_LANG * 60: # safety: don't stream forever
|
| 52 |
-
break
|
| 53 |
-
|
| 54 |
-
results = {}
|
| 55 |
-
for lg, rows in buckets.items():
|
| 56 |
-
refs, hyps = [], []
|
| 57 |
-
for ex in rows:
|
| 58 |
-
try:
|
| 59 |
-
arr, sr = sf.read(io.BytesIO(ex["audio"]["bytes"]), dtype="float32")
|
| 60 |
-
except Exception:
|
| 61 |
-
continue
|
| 62 |
-
if arr.ndim > 1:
|
| 63 |
-
arr = arr.mean(axis=1)
|
| 64 |
-
if sr != sr_t:
|
| 65 |
-
import librosa
|
| 66 |
-
arr = librosa.resample(arr, orig_sr=sr, target_sr=sr_t)
|
| 67 |
-
ref = (ex.get("text") or "").strip()
|
| 68 |
-
if not ref:
|
| 69 |
-
continue
|
| 70 |
-
prompt = tok.apply_chat_template(
|
| 71 |
-
[{"role": "user", "content": INSTR}], tokenize=False, add_generation_prompt=True)
|
| 72 |
-
inputs = proc(text=prompt, audio=[arr], return_tensors="pt").to(dev)
|
| 73 |
-
with torch.no_grad():
|
| 74 |
-
gen = model.generate(**inputs, max_new_tokens=200, do_sample=False)
|
| 75 |
-
hyp = tok.decode(gen[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 76 |
-
refs.append(ref)
|
| 77 |
-
hyps.append(hyp)
|
| 78 |
-
if refs:
|
| 79 |
-
results[lg] = {"n": len(refs),
|
| 80 |
-
"wer": round(jiwer.wer(refs, hyps), 4),
|
| 81 |
-
"cer": round(jiwer.cer(refs, hyps), 4)}
|
| 82 |
-
print(f"[wer] {lg}: n={len(refs)} WER={results[lg]['wer']:.3f} CER={results[lg]['cer']:.3f}")
|
| 83 |
-
print("RESULTS:", json.dumps(results, ensure_ascii=False))
|
| 84 |
-
return 0
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
if __name__ == "__main__":
|
| 88 |
-
raise SystemExit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|