import os,json,time,re,sys,concurrent.futures as cf from openai import OpenAI from items import ITEMS c=OpenAI(base_url="https://router.huggingface.co/v1",api_key=os.environ.get("HF_TOKEN","x"),timeout=180) SUFFIX={"en":"\n\nReply with only the final answer (a number or a single word), nothing else.", "ta":"\n\nஇறுதி விடையை மட்டும் (ஒரு எண் அல்லது ஒரு சொல்) கூறவும், வேறு எதுவும் வேண்டாம்.", "tl":"\n\nFinal answer mattum sollunga (oru number illa oru word), vera edhuvum venaam."} N=int(sys.argv[1]) if len(sys.argv)>1 else 2 jobs=[(it,l,s) for it in ITEMS for l in("en","ta","tl") for s in range(N)] def run(j): it,l,s=j for a in range(8): try: t=time.time() r=c.chat.completions.create(model="reflection/Beam-1.0-501B-A23B",messages=[{"role":"user","content":it[l]+SUFFIX[l]}],max_tokens=6000) m=r.choices[0].message; u=r.usage rd=u.completion_tokens_details.reasoning_tokens if u.completion_tokens_details else None return dict(id=it["id"],cat=it["cat"],lang=l,sample=s,answer=it["answer"],aliases=it.get("aliases",[]),output=m.content,reasoning=getattr(m,"reasoning_content",None), prompt_tokens=u.prompt_tokens,completion_tokens=u.completion_tokens,reasoning_tokens=rd,latency=round(time.time()-t,2),finish=r.choices[0].finish_reason) except Exception as e: err=str(e); time.sleep(5*(a+1)) return dict(id=it["id"],lang=l,sample=s,error=err) import os.path prev={} if os.path.exists("raw_results.json"): for r in json.load(open("raw_results.json")): if "error" not in r: prev[(r["id"],r["lang"],r["sample"])]=r todo=[j for j in jobs if (j[0]["id"],j[1],j[2]) not in prev] print("todo",len(todo),flush=True) with cf.ThreadPoolExecutor(4) as ex: for r in ex.map(run,todo): prev[(r["id"],r["lang"],r["sample"])]=r out=list(prev.values()) json.dump(out,open("raw_results.json","w"),ensure_ascii=False,indent=1) print(len(out),sum('error' in r for r in out))