Download training/kaggle_cells/cell1_pass1.py from SinhalaMMLU-SharedTask/team-riswan: direct link, hf CLI and curl.
- Browser
- Download file 6.3 kB
-
https://huggingface.co/SinhalaMMLU-SharedTask/team-riswan/resolve/main/training/kaggle_cells/cell1_pass1.py
- Command line
-
hf download hf://SinhalaMMLU-SharedTask/team-riswan/training/kaggle_cells/cell1_pass1.py
-
curl -L -o cell1_pass1.py https://huggingface.co/SinhalaMMLU-SharedTask/team-riswan/resolve/main/training/kaggle_cells/cell1_pass1.py
6.3 kB
| import json, re, time, zipfile, shutil, urllib.request | |
| from pathlib import Path | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| # 1. Locate uploaded data in /kaggle/input and copy to /kaggle/working/work | |
| work_dir = Path("/kaggle/working/work") | |
| work_dir.mkdir(parents=True, exist_ok=True) | |
| for p in Path("/kaggle/input").rglob("*"): | |
| if p.name == "kaggle_data.zip": | |
| with zipfile.ZipFile(p, "r") as z: | |
| z.extractall(work_dir) | |
| break | |
| elif p.name == "predictions_gemma4_cot.jsonl": | |
| root = p.parent.parent | |
| shutil.copytree(root, work_dir, dirs_exist_ok=True) | |
| break | |
| data_dir = work_dir / "data" | |
| print("Data directory ready:", data_dir.exists()) | |
| # 2. Load canonical questions + existing checkpoints | |
| def load_all_questions(d_dir: Path): | |
| qs = [] | |
| for lvl in ["easy", "medium", "hard"]: | |
| for f in sorted((d_dir / lvl).glob("*.json")): | |
| for item in json.load(open(f, encoding="utf-8")): | |
| item["_level"] = lvl | |
| qs.append(item) | |
| return qs | |
| def load_ckpt(path: Path): | |
| m = {} | |
| if path.exists(): | |
| for line in open(path, encoding="utf-8"): | |
| if line.strip(): | |
| r = json.loads(line) | |
| k = f"{str(r['level']).lower().strip()}_{str(r['subject']).strip()}_{str(r['q_no']).strip()}" | |
| m[k] = r | |
| return m | |
| canonical_qs = load_all_questions(data_dir) | |
| cot_map = load_ckpt(data_dir / "predictions_gemma4_cot.jsonl") | |
| dir_map = load_ckpt(data_dir / "predictions_gemma4_direct.jsonl") | |
| # Sort: 749 Disputed (Hard -> Medium -> Easy) first, then Undisputed (Hard -> Medium -> Easy) | |
| lvl_rank = {"hard": 0, "medium": 1, "easy": 2} | |
| def sort_key(q): | |
| k = f"{q['_level'].lower()}_{q['subject'].strip()}_{q['q_no']}" | |
| disputed = 0 if cot_map.get(k, {}).get("answer") != dir_map.get(k, {}).get("answer") else 1 | |
| return (disputed, lvl_rank.get(q["_level"].lower(), 3)) | |
| priority_qs = sorted(canonical_qs, key=sort_key) | |
| print(f"Loaded {len(canonical_qs)} total questions. Starting Dual-T4 Parallel Thinking Run...") | |
| SYSTEM_THINK = ( | |
| "You are an expert teacher of the Sri Lankan national school curriculum (grades 6-13), " | |
| "fluent in Sinhala. You will receive a multiple-choice question written in Sinhala with four " | |
| "numbered options. Exactly one option is correct. Read the Sinhala carefully, including " | |
| "negations such as 'නොවන' / 'නොවේ' (NOT). You may reason in English. " | |
| "Finish with the final answer in the form 'Answer: <number>'." | |
| ) | |
| ANS_RE = re.compile(r"(?:Answer|පිළිතුර)\s*[::]?\s*\**\s*\(?([1-4])\)?", re.I) | |
| HOSTS = ["http://127.0.0.1:11434", "http://127.0.0.1:11435"] | |
| def solve_question(idx_q): | |
| idx, q = idx_q | |
| host = HOSTS[idx % 2] # Alternate evenly between T4 GPU #0 and T4 GPU #1 | |
| lvl = q["_level"].lower() | |
| subj = q.get("subject", "General") | |
| q_no = q["q_no"] | |
| q_key = f"{lvl}_{subj.strip()}_{q_no}" | |
| grade = "6-9" if lvl == "easy" else ("10-11 (O/L)" if lvl == "medium" else "12-13 (A/L)") | |
| opts = "\n".join(f"{i}. {c}" for i, c in enumerate(q["choices"], 1)) | |
| user_msg = ( | |
| f"විෂය (Subject): {subj} | ශ්රේණිය (Grade): {grade}\n\n" | |
| f"ප්රශ්නය (Question):\n{q['question'].strip()}\n\n" | |
| f"විකල්ප (Options):\n{opts}\n\n" | |
| "Which option (1, 2, 3 or 4) is correct? End your reply with 'Answer: <number>'." | |
| ) | |
| payload = json.dumps({ | |
| "model": "gemma4:e4b", | |
| "messages": [{"role": "system", "content": SYSTEM_THINK}, {"role": "user", "content": user_msg}], | |
| "stream": False, | |
| "think": True, | |
| "options": {"temperature": 0.0, "num_predict": 2048} | |
| }).encode("utf-8") | |
| try: | |
| req = urllib.request.Request(f"{host}/api/chat", data=payload, headers={"Content-Type": "application/json"}) | |
| with urllib.request.urlopen(req, timeout=240) as resp: | |
| res = json.loads(resp.read().decode("utf-8")) | |
| msg = res.get("message", {}) | |
| content, thinking = msg.get("content", ""), msg.get("thinking", "") | |
| m = ANS_RE.findall(content) or ANS_RE.findall(thinking) | |
| if m: | |
| ans = m[-1] | |
| else: | |
| digits = re.findall(r"\b([1-4])\b", content if content.strip() else thinking) | |
| ans = digits[-1] if digits else cot_map.get(q_key, {}).get("answer", "1") | |
| except Exception as e: | |
| ans = cot_map.get(q_key, {}).get("answer", "1") | |
| return q_key, { | |
| "q_no": q_no, | |
| "subject": subj, | |
| "category": q.get("category", "general"), | |
| "level": lvl, | |
| "answer": str(ans) | |
| } | |
| ckpt_path = Path("/kaggle/working/predictions_gemma4_think_ckpt.jsonl") | |
| full_path = Path("/kaggle/working/predictions.jsonl") | |
| zip_path = Path("/kaggle/working/submission_gemma4_think.zip") | |
| completed_map = load_ckpt(ckpt_path) | |
| todo = [(i, q) for i, q in enumerate(priority_qs) if f"{q['_level'].lower()}_{q['subject'].strip()}_{q['q_no']}" not in completed_map] | |
| print(f"Already completed: {len(completed_map)} | Remaining to solve: {len(todo)}") | |
| def save_outputs(): | |
| with open(ckpt_path, "w", encoding="utf-8") as f: | |
| for r in completed_map.values(): | |
| f.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| with open(full_path, "w", encoding="utf-8") as f: | |
| for q in canonical_qs: | |
| k = f"{q['_level'].lower()}_{q['subject'].strip()}_{q['q_no']}" | |
| rec = completed_map.get(k) or cot_map.get(k) | |
| f.write(json.dumps(rec, ensure_ascii=False) + "\n") | |
| with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: | |
| zf.write(full_path, arcname="predictions.jsonl") | |
| t0 = time.time() | |
| done_count = 0 | |
| with ThreadPoolExecutor(max_workers=4) as ex: | |
| futures = [ex.submit(solve_question, item) for item in todo] | |
| for fut in as_completed(futures): | |
| k, rec = fut.result() | |
| completed_map[k] = rec | |
| done_count += 1 | |
| if done_count % 20 == 0 or done_count == len(todo): | |
| save_outputs() | |
| elapsed = time.time() - t0 | |
| rate = elapsed / done_count | |
| print(f"[{len(completed_map)}/2238] ({elapsed/60:.1f}m elapsed, {rate:.1f}s/q effective) -> {zip_path.name} updated!") | |