Download data_synthetic_extended/scripts/grade_ds.py from WMTH-100d2exam/hackathon-data: direct link, hf CLI and curl.
- Browser
- Download file 5.88 kB
-
https://huggingface.co/datasets/WMTH-100d2exam/hackathon-data/resolve/main/data_synthetic_extended/scripts/grade_ds.py
- Command line
-
hf download hf://datasets/WMTH-100d2exam/hackathon-data/data_synthetic_extended/scripts/grade_ds.py
-
curl -L -o grade_ds.py https://huggingface.co/datasets/WMTH-100d2exam/hackathon-data/resolve/main/data_synthetic_extended/scripts/grade_ds.py
5.88 kB
| #!/usr/bin/env python3 | |
| """DeepSeek as a grader of matura answers, calibrated against the Claude reference grades. | |
| python grade_ds.py --dir /scratch/wiki_ext/grading --prefixes val,v3,v3t [--concurrency 128] [--budget 25.8] | |
| Input (copied from l40s-results/claude-grading/): <prefix>_batch_<n>.jsonl rows {item, max_points, question, rubric, | |
| key_answer, answer}, <prefix>_grades_<n>.jsonl (Claude: {item, points, reason}), items_meta.jsonl / <prefix>_items_meta.jsonl | |
| ({item, variant, id}). Same grading rules as the Claude workflow (grade-rft-batches). | |
| Output: ds_grades.jsonl {item, points, reason}; report on stdout + ds_report.json (per-variant % by DeepSeek and by | |
| Claude, exact agreement, mean |diff|, confusion on full points). | |
| """ | |
| import argparse | |
| import asyncio | |
| import glob | |
| import json | |
| from collections import Counter, defaultdict | |
| from pathlib import Path | |
| from common import append_jsonl, read_jsonl | |
| from ds import DS, BudgetExceeded | |
| RULES = """Jesteś egzaminatorem CKE oceniającym maturę z historii (poziom rozszerzony). Oceniasz jedną odpowiedź zdającego. | |
| Zasady: | |
| - Oceniaj ściśle według "zasad oceniania" (klucz CKE). "Przykładowe rozwiązanie" to wzór: akceptuj każdą merytorycznie poprawną odpowiedź spełniającą zasady, także sformułowaną inaczej. | |
| - Nie przyznawaj punktów za błędy merytoryczne, za treści niezwiązane z poleceniem ani za samą długość. | |
| - Gdy zasady oceniania są puste albo przykładowe rozwiązanie wygląda na uszkodzone, oceniaj z własnej wiedzy. | |
| - Zadania mogą odwoływać się do ilustracji/map ([mapa/ilustracja]), których nie widać – oceniaj treść względem klucza; opis ilustracji zmyślony wbrew kluczowi to błąd. | |
| - W zadaniach zamkniętych (wybór A–F, P/F) liczy się wskazany wybór; gdy odpowiedź wskazuje kilka sprzecznych opcji albo warianty warunkowe zamiast jednego rozstrzygnięcia – 0. | |
| - "punkty" to liczba całkowita od 0 do maksymalnej liczby punktów. | |
| Zwróć json: {"uzasadnienie": "jedno krótkie zdanie", "punkty": 0}""" | |
| def prompt(r): | |
| task = (f"ZADANIE (maks. {r['max_points']} pkt):\n{r['question']}\n\nZASADY OCENIANIA:\n{r.get('rubric') or '(brak)'}\n\n" | |
| f"PRZYKŁADOWE ROZWIĄZANIE:\n{r.get('key_answer') or '(brak)'}\n\nODPOWIEDŹ ZDAJĄCEGO:\n{r['answer']}") | |
| return [{"role": "system", "content": RULES}, {"role": "user", "content": task}] | |
| async def main(a): | |
| d = Path(a.dir) | |
| rows, claude, variant = {}, {}, {} | |
| for p in a.prefixes.split(","): | |
| for f in glob.glob(str(d / f"{p}_batch_*.jsonl")): | |
| for r in read_jsonl(f): | |
| rows[r["item"]] = r | |
| for f in glob.glob(str(d / f"{p}_grades_*.jsonl")): | |
| for g in read_jsonl(f): | |
| claude[g["item"]] = g["points"] | |
| meta = d / ("items_meta.jsonl" if p == "val" else f"{p}_items_meta.jsonl") | |
| for m in read_jsonl(meta): | |
| variant[m["item"]] = m["variant"] | |
| out = d / "ds_grades.jsonl" | |
| done = {g["item"] for g in read_jsonl(out)} | |
| todo = [r for i, r in sorted(rows.items()) if i not in done] | |
| print(f"items {len(rows)}, to grade {len(todo)}", flush=True) | |
| ds = DS(concurrency=a.concurrency, budget_usd=a.budget, tag="grade") | |
| def val(r): | |
| def v(o): | |
| p = o.get("punkty") | |
| if not isinstance(p, int) or not 0 <= p <= r["max_points"]: | |
| return f'"punkty" musi być liczbą całkowitą 0–{r["max_points"]}' | |
| return None | |
| return v | |
| async def one(r): | |
| try: | |
| o, err = await ds.json(prompt(r), max_tokens=200, temperature=0.0, validate=val(r), tag="grade") | |
| except BudgetExceeded: | |
| return | |
| if o: | |
| append_jsonl(out, [{"item": r["item"], "points": o["punkty"], "reason": o.get("uzasadnienie", "")}]) | |
| await asyncio.gather(*(one(r) for r in todo)) | |
| print("grading done:", ds.summary(), flush=True) | |
| await ds.aclose() | |
| dsg = {g["item"]: g["points"] for g in read_jsonl(out)} | |
| per = defaultdict(lambda: Counter()) | |
| agree = Counter() | |
| for i, r in rows.items(): | |
| v = variant.get(i, "?") | |
| per[v]["max"] += r["max_points"] | |
| if i in dsg: | |
| per[v]["ds"] += dsg[i] | |
| if i in claude: | |
| per[v]["claude"] += claude[i] | |
| if i in dsg and i in claude: | |
| agree["n"] += 1 | |
| agree["exact"] += dsg[i] == claude[i] | |
| agree["absdiff"] += abs(dsg[i] - claude[i]) | |
| full_d, full_c = dsg[i] == r["max_points"], claude[i] == r["max_points"] | |
| agree[f"full_ds{int(full_d)}_cl{int(full_c)}"] += 1 | |
| rep = {"variants": {v: {"max": c["max"], "ds_pct": round(100 * c["ds"] / c["max"], 1), | |
| "claude_pct": round(100 * c["claude"] / c["max"], 1)} for v, c in sorted(per.items())}, | |
| "agreement": {"n": agree["n"], "exact_pct": round(100 * agree["exact"] / max(1, agree["n"]), 1), | |
| "mean_abs_diff": round(agree["absdiff"] / max(1, agree["n"]), 3), | |
| "full_points_precision": round(agree["full_ds1_cl1"] / max(1, agree["full_ds1_cl1"] + agree["full_ds1_cl0"]), 3), | |
| "full_points_recall": round(agree["full_ds1_cl1"] / max(1, agree["full_ds1_cl1"] + agree["full_ds0_cl1"]), 3), | |
| "confusion_full": {k: v for k, v in agree.items() if k.startswith("full")}}} | |
| (d / "ds_report.json").write_text(json.dumps(rep, ensure_ascii=False, indent=1), encoding="utf-8") | |
| print(json.dumps(rep, ensure_ascii=False, indent=1)) | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--dir", default="/scratch/wiki_ext/grading") | |
| ap.add_argument("--prefixes", default="val,v3,v3t") | |
| ap.add_argument("--concurrency", type=int, default=128) | |
| ap.add_argument("--budget", type=float, default=25.8) | |
| asyncio.run(main(ap.parse_args())) | |