visualears-corpus-transfer / meta /bench_final.py
Reza2kn's picture
Upload meta/bench_final.py with huggingface_hub
94d8559 verified
Raw
History Blame Contribute Delete
2.53 kB
import sys,json,io,glob,re,os,unicodedata,soundfile as sf
from huggingface_hub import hf_hub_download
import pyarrow.parquet as pq
from nemo.collections.asr.models import ASRModel
CK=sys.argv[1]
PD="۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩";AD="01234567890123456789";DM=str.maketrans(PD,AD)
DIA=re.compile(r"[ً-ٰٟۖ-ۭ]");PUN=re.compile(r"[\W_]+",re.UNICODE)
def nf(t,keep=True):
t=unicodedata.normalize("NFKC",t or "").replace("ي","ی").replace("ك","ک").replace("ۀ","ه").replace("ة","ه").replace("أ","ا").replace("إ","ا").replace("ٱ","ا").replace("آ","ا").replace("ؤ","و").replace("ئ","ی").replace("ى","ی")
t=t.translate(DM);t=DIA.sub("",t);t=t.replace("‌"," ").replace("‍","").replace("​","");t=PUN.sub(" ",t);t=re.sub(r"\s+"," ",t).strip()
return t if keep else t.replace(" ","")
def lev(a,b):
dp=list(range(len(b)+1))
for i,x in enumerate(a,1):
p=dp[0];dp[0]=i
for j,y in enumerate(b,1):o=dp[j];dp[j]=min(dp[j]+1,dp[j-1]+1,p+(x!=y));p=o
return dp[-1]
def WC(refs,hyps):
we=rw=ce=rc=0
for r,h in zip(refs,hyps):
rn=nf(r);hn=nf(h);we+=lev(rn.split(),hn.split());rw+=len(rn.split());ce+=lev(list(nf(r,0)),list(nf(h,0)));rc+=len(nf(r,0))
return 100*we/max(1,rw),100*ce/max(1,rc)
tok=open("/root/.cache/huggingface/token").read().strip()
refs=[];paths=[];n=0;os.makedirs("/tmp/g6f",exist_ok=True)
for i in range(5):
p=hf_hub_download("Reza2kn/visualears-golden-6669",f"data/test-{i:05d}-of-00005.parquet",repo_type="dataset",token=tok,local_dir="/tmp/g6669dl")
for r in pq.read_table(p).to_pylist():
au=r.get("audio");txt=r.get("text")
if au is None or txt is None: continue
b=au["bytes"] if isinstance(au,dict) else au
try:
arr,sr=sf.read(io.BytesIO(b),dtype="float32");arr=arr[:,0] if arr.ndim>1 else arr
wp=f"/tmp/g6f/{n:05d}.wav";sf.write(wp,arr,sr);paths.append(wp);refs.append(txt);n+=1
except: pass
m=ASRModel.restore_from(CK,map_location="cuda");m.eval()
def dec(dt):
m.change_decoding_strategy(decoder_type=dt)
o=m.transcribe(paths,batch_size=16,verbose=False);o=o[0] if isinstance(o,tuple) else o
return [h.text if hasattr(h,"text") else h for h in o]
rw,rc=WC(refs,dec("rnnt")); cw,cc=WC(refs,dec("ctc"))
print(f"\n##### BENCH {os.path.basename(CK)} — GOLDEN-6669 ({n} clips) #####")
print(f" RNNT: WER {rw:.2f}% CER {rc:.2f}%")
print(f" CTC : WER {cw:.2f}% CER {cc:.2f}%")
print(f" refs: warm-A 12.80% | phaseB2(old) 8.02% | Gemini 6.49%\n")