Datasets:
Download evaluate.py from digitable-lol/ouroboros-trace-help: direct link, hf CLI and curl.
- Browser
- Download file 6.87 kB
-
https://huggingface.co/datasets/digitable-lol/ouroboros-trace-help/resolve/main/evaluate.py
- Command line
-
hf download hf://datasets/digitable-lol/ouroboros-trace-help/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/digitable-lol/ouroboros-trace-help/resolve/main/evaluate.py
6.87 kB
| #!/usr/bin/env python3 | |
| """Score model answers for the trace-help dataset. | |
| The parsing and matching rules below were fixed before any model was run and | |
| have not been changed since. Grading is blind: `judge` receives only the | |
| accepted answers and the answer text, never the setting the answer came from. | |
| Usage: | |
| evaluate.py data/test.jsonl answers.jsonl | |
| `answers.jsonl` — one JSON object per line: | |
| {"id": "orders-1", "setting": "with_trace", "repeat": 1, "output": "..."} | |
| `setting` is either "no_trace" or "with_trace". | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import random | |
| import re | |
| import statistics | |
| import sys | |
| from pathlib import Path | |
| ABSTAIN = ("не знаю", "неизвестно", "нельзя определить", "недостаточно", | |
| "i don't know", "unknown", "cannot be determined") | |
| NUMBER = re.compile(r"-?\d+") | |
| SETTINGS = ("no_trace", "with_trace") | |
| def tidy(text: str) -> str: | |
| text = text.strip().strip("«»\"'`").strip().rstrip(".") | |
| return " ".join(text.lower().split()) | |
| def extract(raw: str) -> str | None: | |
| """The last line containing ОТВЕТ: — everything after the colon.""" | |
| found = None | |
| for line in raw.splitlines(): | |
| if "ОТВЕТ:" in line.upper() or "ответ:" in line: | |
| found = line.split(":", 1)[1] if ":" in line else line | |
| return found | |
| def judge(accepted: list[str], raw: str) -> str: | |
| """correct / abstain / wrong. Sees only accepted answers and the text.""" | |
| said = extract(raw) | |
| if said is None: | |
| return "wrong" | |
| said = tidy(said) | |
| if any(word in said for word in ABSTAIN): | |
| return "abstain" | |
| for want in accepted: | |
| want = tidy(want) | |
| if said == want: | |
| return "correct" | |
| if want in ("да", "нет") and said.startswith(want): | |
| return "correct" | |
| if NUMBER.fullmatch(want) and NUMBER.findall(said) == [want]: | |
| return "correct" | |
| return "wrong" | |
| def bootstrap(groups: list[list[tuple[str, str]]], rounds: int = 10000) -> dict: | |
| """95% interval, resampling whole programs with replacement.""" | |
| rnd = random.Random(20260830) | |
| n = len(groups) | |
| keep = {k: [] for k in ("no_trace", "with_trace", "difference", | |
| "wrong_no_trace", "wrong_with_trace", "wrong_difference")} | |
| for _ in range(rounds): | |
| flat = [item for _ in range(n) for item in groups[rnd.randrange(n)]] | |
| for setting in SETTINGS: | |
| mine = [v for s, v in flat if s == setting] | |
| keep[setting].append(sum(v == "correct" for v in mine) / len(mine)) | |
| keep["wrong_" + setting].append( | |
| sum(v == "wrong" for v in mine) / len(mine)) | |
| keep["difference"].append(keep["with_trace"][-1] - keep["no_trace"][-1]) | |
| keep["wrong_difference"].append( | |
| keep["wrong_with_trace"][-1] - keep["wrong_no_trace"][-1]) | |
| out = {} | |
| for key, values in keep.items(): | |
| values.sort() | |
| out[key] = (values[int(0.025 * rounds)], values[int(0.975 * rounds)]) | |
| return out | |
| def pct(x: float) -> str: | |
| return f"{100 * x:.1f} %" | |
| def main() -> int: | |
| rows = [json.loads(l) for l in Path(sys.argv[1]).read_text( | |
| encoding="utf-8").splitlines() if l.strip()] | |
| answers = [json.loads(l) for l in Path(sys.argv[2]).read_text( | |
| encoding="utf-8").splitlines() if l.strip()] | |
| accepted = {r["id"]: r["answers"] for r in rows} | |
| kind = {r["id"]: r["question_kind"] for r in rows} | |
| language = {r["id"]: r["language"] for r in rows} | |
| program = {r["id"]: r["program"] for r in rows} | |
| # --- blind part: only question id and answer text go in ----------------- | |
| blind = [(i, a["id"], a["output"]) for i, a in enumerate(answers)] | |
| random.Random(1).shuffle(blind) | |
| verdicts = {i: judge(accepted[qid], text) for i, qid, text in blind} | |
| # --- from here on the setting may be looked at -------------------------- | |
| for i, a in enumerate(answers): | |
| a["verdict"] = verdicts[i] | |
| def share(rows_, verdict): | |
| return sum(r["verdict"] == verdict for r in rows_) / len(rows_) if rows_ else 0.0 | |
| print(f"answers: {len(answers)}; questions: {len(accepted)}; " | |
| f"programs: {len(set(program.values()))}; " | |
| f"repeats: {len({a.get('repeat', 1) for a in answers})}") | |
| print() | |
| print("| setting | correct | confidently wrong | abstained | answers |") | |
| print("|---|---|---|---|---|") | |
| for setting in SETTINGS: | |
| mine = [a for a in answers if a["setting"] == setting] | |
| print(f"| {setting} | {pct(share(mine, 'correct'))} | " | |
| f"{pct(share(mine, 'wrong'))} | {pct(share(mine, 'abstain'))} | " | |
| f"{len(mine)} |") | |
| groups = [] | |
| for prog in sorted(set(program.values())): | |
| ids = {q for q, p in program.items() if p == prog} | |
| groups.append([(a["setting"], a["verdict"]) for a in answers | |
| if a["id"] in ids]) | |
| band = bootstrap(groups) | |
| print() | |
| print("95% intervals (10 000 resamples over programs):") | |
| print(f" correct, no trace {pct(band['no_trace'][0])} … {pct(band['no_trace'][1])}") | |
| print(f" correct, with trace {pct(band['with_trace'][0])} … {pct(band['with_trace'][1])}") | |
| print(f" difference {pct(band['difference'][0])} … {pct(band['difference'][1])}") | |
| print(f" confidently wrong, difference {pct(band['wrong_difference'][0])} … " | |
| f"{pct(band['wrong_difference'][1])}") | |
| repeats = sorted({a.get("repeat", 1) for a in answers}) | |
| if len(repeats) > 1: | |
| print() | |
| print("| repeat | no trace | with trace |") | |
| print("|---|---|---|") | |
| per = {} | |
| for rep in repeats: | |
| line = [share([a for a in answers if a.get("repeat", 1) == rep | |
| and a["setting"] == s], "correct") for s in SETTINGS] | |
| per[rep] = line | |
| print(f"| {rep} | {pct(line[0])} | {pct(line[1])} |") | |
| for idx, title in enumerate(SETTINGS): | |
| vals = [v[idx] for v in per.values()] | |
| print(f" {title}: range {pct(min(vals))} … {pct(max(vals))}, " | |
| f"standard deviation {pct(statistics.stdev(vals))}") | |
| for title, table in (("language", language), ("question kind", kind)): | |
| print() | |
| print(f"| {title} | correct, no trace | correct, with trace | answers each |") | |
| print("|---|---|---|---|") | |
| for value in sorted(set(table.values())): | |
| ids = {q for q, v in table.items() if v == value} | |
| line = [[a for a in answers if a["id"] in ids and a["setting"] == s] | |
| for s in SETTINGS] | |
| print(f"| {value} | {pct(share(line[0], 'correct'))} | " | |
| f"{pct(share(line[1], 'correct'))} | {len(line[0])} |") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |