Shenava-Rizeh-v1.5 / training /prep_data.py
Reza2kn's picture
Shenava Rizeh v1.5 (fine-tuned tinyasr en-fa)
a8248a0 verified
Raw History Blame Contribute Delete
2.97 kB
"""Build r15 training manifest: audio path, duration, tinyasr-style text (raw teacher text, normalized chars)."""
import json,re,sys,os,hashlib,collections,unicodedata
sys.path[:0]=[os.path.dirname(os.path.abspath(__file__)),'/home/rezo/value_study/phase2']
from tzf_io import read_tzf
from tok import Tok
from shenavasanj import fair_text
B='/home/rezo/value_study/'
d=read_tzf(B+'claude_tinyasr/m/en-fa.tzf');cfg=d['cfg'];sh,ls=cfg['shared'],cfg['lang_size']
allowed=list(range(sh))+list(range(sh+ls,sh+2*ls));T=Tok(d['pieces'],allowed)
DIG=str.maketrans('0123456789٠١٢٣٤٥٦٧٨٩','۰۱۲۳۴۵۶۷۸۹۰۱۲۳۴۵۶۷۸۹')
HAR=re.compile('[ً-ٰٟۖ-ۭ]')
def norm(t):
t=unicodedata.normalize('NFC',t or '').replace('ي','ی').replace('ك','ک').replace('ى','ی').replace('ة','ه')
t=HAR.sub('',t).translate(DIG)
t=t.replace(',','،').replace('?','؟').replace(';','؛')
t=re.sub(r'\s*‌\s*','‌',t);t=re.sub(r'\s+',' ',t).strip()
return t
bench=set(json.load(open(B+'codex_p5_train/benchmark_sentence_hashes.json'))['hashes'])
FET={c['key']:c for l in open('audio/fetched.jsonl') for c in json.loads(l)['clips']}
rows=[];st=collections.Counter();hrs=collections.Counter()
def add(src,key,dur,text):
c=FET.get(key);path=c['path'] if c else None;dur=c['duration'] if c else dur
if not path or not os.path.exists(path): st[src,'noaudio']+=1;return
if not (1<=dur<=40): st[src,'dur']+=1;return
t=norm(text)
if not t: st[src,'empty']+=1;return
if hashlib.sha1(fair_text(t).replace(' ','').encode()).hexdigest() in bench: st[src,'bench']+=1;return
ids=T.encode(t)
if ids is None:
# drop characters the vocab cannot write (rare symbols), retry
t2=''.join(c for c in t if c==' ' or T.encode('x'+c) is not None or T.word(c) is not None);ids=T.encode(t2);t=t2
if not ids: st[src,'untokenizable']+=1;return
if len(ids)+5>dur*25: st[src,'too_many_tokens']+=1;return
rows.append({'audio_filepath':path,'duration':dur,'text':t,'src':src});st[src,'keep']+=1;hrs[src]+=dur/3600
for r in map(json.loads,open(B+'codex_p5_train/targeted_combined.jsonl')):
s=r.get('new_source') or r.get('gate')
if s=='targeted_scribe_flash': continue
txt=r.get('scribe') or r.get('teacher_text_raw') or r.get('text_before_phase5_normalization') or r['text']
add(s,r['key'],float(r['duration']),r.get('original_text') if s=='Mana_K2N' else txt)
for r in map(json.loads,open(B+'codex_p5_labels/labels.jsonl')):
if not str(r.get('verdict','')).startswith('keep'): continue
add('targeted',r['key'],float(r['duration']),r.get('scribe_text'))
import random;random.seed(0);random.shuffle(rows)
with open('r15_train.jsonl','w') as o:
for r in rows: o.write(json.dumps(r,ensure_ascii=False)+'\n')
print({f'{a}|{b}':v for (a,b),v in sorted(st.items())});print({k:round(v,1) for k,v in hrs.items()},'total h',round(sum(hrs.values()),1))
for r in rows[:5]: print(r['src'],'|',r['text'][:110])