Datasets:
Add Beam->Qwen3 distill data + Colab notebook
Browse files- distill/distill.py +34 -0
distill/distill.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os,json,time,random,threading,concurrent.futures as cf,re
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
V="../v2/"
|
| 4 |
+
c=OpenAI(base_url="https://router.huggingface.co/v1",api_key=os.environ.get("HF_TOKEN","x"),timeout=240)
|
| 5 |
+
ta={(d["link"],d["question_number"]):d for d in map(json.loads,open(V+"tam_Taml.jsonl"))}
|
| 6 |
+
links=sorted({k[0] for k in ta}); random.Random(42).shuffle(links)
|
| 7 |
+
evl=set(links[:int(len(links)*0.25)])
|
| 8 |
+
split={k:("eval" if k[0] in evl else "train") for k in ta}
|
| 9 |
+
json.dump({f"{k[0]}|{k[1]}":v for k,v in split.items()},open("split.json","w"))
|
| 10 |
+
def body(k):
|
| 11 |
+
Q=ta[k]; opts="\n".join(f"{i}. {Q['mc_answer'+str(i)]}" for i in range(1,5))
|
| 12 |
+
return f"பத்தி:\n{Q['flores_passage']}\n\nகேள்வி: {Q['question']}\n\nவிடைகள்:\n{opts}"
|
| 13 |
+
INS="\n\nபத்தியின் அடிப்படையில் 1-2 வாக்கியங்களில் தமிழில் சுருக்கமாக காரணம் கூறவும். பிறகு கடைசி வரியில் 'விடை: <எண்>' என்று மட்டும் எழுதவும்."
|
| 14 |
+
OUT="beam_distill.jsonl"; done=set(); lock=threading.Lock()
|
| 15 |
+
if os.path.exists(OUT):
|
| 16 |
+
for l in open(OUT):
|
| 17 |
+
r=json.loads(l)
|
| 18 |
+
if "error" not in r: done.add((r["link"],r["qn"]))
|
| 19 |
+
jobs=[k for k in ta if split[k]=="train" and k not in done]
|
| 20 |
+
print("todo",len(jobs),flush=True)
|
| 21 |
+
def run(k):
|
| 22 |
+
err=None
|
| 23 |
+
for a in range(6):
|
| 24 |
+
try:
|
| 25 |
+
r=c.chat.completions.create(model="reflection/Beam-1.0-501B-A23B",messages=[{"role":"user","content":body(k)+INS}],max_tokens=3000)
|
| 26 |
+
m=r.choices[0].message.content or ""
|
| 27 |
+
mm=re.findall(r"விடை\s*[::]\s*([1-4])",m)
|
| 28 |
+
rec=dict(link=k[0],qn=k[1],gold=str(ta[k]["correct_answer_num"]),pred=mm[-1] if mm else None,output=m,usage=r.usage.model_dump() if r.usage else None,finish=r.choices[0].finish_reason)
|
| 29 |
+
break
|
| 30 |
+
except Exception as e: err=str(e)[:200]; time.sleep(4*(a+1))
|
| 31 |
+
else: rec=dict(link=k[0],qn=k[1],error=err)
|
| 32 |
+
with lock: open(OUT,"a").write(json.dumps(rec,ensure_ascii=False)+"\n")
|
| 33 |
+
with cf.ThreadPoolExecutor(12) as ex: list(ex.map(run,jobs))
|
| 34 |
+
print("done",flush=True)
|