Datasets:
Download code/v2/runall.py from esscor/TamilBench: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/datasets/esscor/TamilBench/resolve/main/code/v2/runall.py
- Command line
-
hf download hf://datasets/esscor/TamilBench/code/v2/runall.py
-
curl -L -o runall.py https://huggingface.co/datasets/esscor/TamilBench/resolve/main/code/v2/runall.py
3.58 kB
| import os,json,time,threading,concurrent.futures as cf,sys | |
| from openai import OpenAI | |
| from translit import tanglish | |
| from hard import H | |
| from writing import W | |
| T0=time.time(); BUDGET=1080 | |
| c=OpenAI(base_url="https://router.huggingface.co/v1",api_key=os.environ.get("HF_TOKEN","x"),timeout=240) | |
| en={(d["link"],d["question_number"]):d for d in map(json.loads,open("eng_Latn.jsonl"))} | |
| ta={(d["link"],d["question_number"]):d for d in map(json.loads,open("tam_Taml.jsonl"))} | |
| keys=sorted(k for k in ta if k in en) | |
| INS={"en":"Answer with only the number of the correct option (1, 2, 3 or 4).","ta":"சரியான விடையின் எண்ணை மட்டும் (1, 2, 3 அல்லது 4) கூறவும்."} | |
| def bprompt(k,cond): | |
| if cond=="translit": return tanglish(bprompt(k,"ta").rsplit("\n\n",1)[0])+"\n\nCorrect option number mattum sollunga (1, 2, 3 illa 4)." | |
| P=(ta if cond in("ta","mix") else en)[k]; Q=(ta if cond=="ta" else en)[k] | |
| lab={"en":("Passage","Question","Options"),"ta":("பத்தி","கேள்வி","விடைகள்")}["ta" if cond=="ta" else "en"] | |
| opts="\n".join(f"{i}. {Q['mc_answer'+str(i)]}" for i in range(1,5)) | |
| return f"{lab[0]}:\n{P['flores_passage']}\n\n{lab[1]}: {Q['question']}\n\n{lab[2]}:\n{opts}\n\n{INS['ta' if cond=='ta' else 'en']}" | |
| SUF="\n\nஇறுதி விடையை மட்டும் கூறவும், வேறு எதுவும் வேண்டாம்." | |
| jobs=[] | |
| for h in H: | |
| for s in range(3): jobs.append(("hard",h["id"],s,h["ta"]+SUF,{})) | |
| for w in W: jobs.append(("write",w["id"],0,w["prompt"],{})) | |
| for cd in ("en","ta","mix","translit"): | |
| for k in keys: jobs.append(("belebele",f"{k[0]}#{k[1]}",cd,bprompt(k,cd),{"gold":str(en[k]["correct_answer_num"])})) | |
| OUT="all_raw.jsonl"; done=set(); lock=threading.Lock() | |
| if os.path.exists(OUT): | |
| for l in open(OUT): r=json.loads(l); done.add((r["split"],r["id"],str(r["cond"]))) | |
| # migrate earlier belebele | |
| if os.path.exists("belebele_raw.jsonl") and not os.path.exists(".migrated"): | |
| with open(OUT,"a") as f: | |
| for l in open("belebele_raw.jsonl"): | |
| r=json.loads(l); rid=f"{r['link']}#{r['qn']}" | |
| if ("belebele",rid,r["cond"]) in done: continue | |
| rec=dict(split="belebele",id=rid,cond=r["cond"],gold=r["gold"],**{k:r[k] for k in("output","prompt_tokens","completion_tokens","reasoning_tokens","latency","finish")}) | |
| f.write(json.dumps(rec,ensure_ascii=False)+"\n"); done.add(("belebele",rid,r["cond"])) | |
| open(".migrated","w").write("1") | |
| todo=[j for j in jobs if (j[0],j[1],str(j[2])) not in done] | |
| print("todo",len(todo),flush=True) | |
| down=threading.Event() | |
| def run(j): | |
| split,id_,cond,content,extra=j | |
| while time.time()-T0<BUDGET: | |
| try: | |
| t=time.time() | |
| r=c.chat.completions.create(model="reflection/Beam-1.0-501B-A23B",messages=[{"role":"user","content":content}],max_tokens=6000) | |
| m=r.choices[0].message;u=r.usage | |
| rec=dict(split=split,id=id_,cond=cond,**extra,output=m.content,prompt_tokens=u.prompt_tokens,completion_tokens=u.completion_tokens,reasoning_tokens=u.completion_tokens_details.reasoning_tokens if u.completion_tokens_details else None,latency=round(time.time()-t,2),finish=r.choices[0].finish_reason) | |
| with lock: open(OUT,"a").write(json.dumps(rec,ensure_ascii=False)+"\n") | |
| return 1 | |
| except Exception as e: | |
| time.sleep(20) | |
| return 0 | |
| with cf.ThreadPoolExecutor(10) as ex: n=sum(ex.map(run,todo)) | |
| print("ok",n,"left",len(todo)-n,flush=True) | |