Download analysis/study.py from hedwigai/model-bench-v2: direct link, hf CLI and curl.
- Browser
- Download file 4.04 kB
-
https://huggingface.co/datasets/hedwigai/model-bench-v2/resolve/main/analysis/study.py
- Command line
-
hf download hf://datasets/hedwigai/model-bench-v2/analysis/study.py
-
curl -L -o study.py https://huggingface.co/datasets/hedwigai/model-bench-v2/resolve/main/analysis/study.py
4.04 kB
| """How did runs differ by model? Source profile of every research move, mechanically classified by tool signature. | |
| Permutation test: shuffle model labels within each (task, repeat) triple, so task and round are held fixed.""" | |
| import json, re, random, statistics as st, collections, sys | |
| from pathlib import Path | |
| B = Path(sys.argv[1]) | |
| H = re.compile(r"^(draft:|shell:inspect|help:|skills|catalog|file:|shell:other)") | |
| GOV = re.compile(r"(\.gov$|\.gov\.|europa\.eu$|ecb\.|\.int$|sec\.gov)") | |
| def kind(sig): | |
| s = sig | |
| if re.match(r"^(websearch|browser:search|pplx)", s): return "search" | |
| if re.match(r"^(sec-filings|trade|funding|fda|biotech)", s): return "data" | |
| if re.match(r"^(shell:process|script|xlsx|describe_image|workbooks)", s): return "process" | |
| m = re.match(r"^(webfetch|pdftext|doctext|browser:(asset|session|fetch|get|show)|curl)(:(.+))?$", s) | |
| if m: | |
| d = (m.group(4) or "") | |
| return "official" if GOV.search(d) else "page" | |
| return "other" | |
| KINDS = ["search", "page", "official", "data", "process"] | |
| moves = collections.defaultdict(list) | |
| for l in open(B / "moves.jsonl"): | |
| m = json.loads(l) | |
| sigs = [x for x in m["signature"].split(" + ") if not H.match(x)] | |
| if not sigs: continue | |
| m["kinds"] = sorted({kind(x) for x in sigs}); m["sigs"] = sigs | |
| moves[m["run_key"]].append(m) | |
| M = json.loads((B / "metrics.json").read_text()) | |
| runs = {} | |
| for key, r in M["runs"].items(): | |
| ms = sorted(moves.get(key, []), key=lambda m: m["move"]) | |
| n = len(ms) | |
| cnt = collections.Counter(k for m in ms for k in m["kinds"]) | |
| tot = sum(cnt[k] for k in KINDS) or 1 | |
| domains = {re.sub(r"^(webfetch|pdftext|doctext|browser:\w+|curl):", "", x) for m in ms for x in m["sigs"] | |
| if re.match(r"^(webfetch|pdftext|doctext|browser:(asset|session|fetch|get|show)|curl):.+", x)} | |
| searches = cnt["search"]; reads = cnt["page"] + cnt["official"] | |
| runs[key] = dict(task=r["task"], model=r["model"], repeat=r["repeat"], acc=r.get("grade_accuracy"), | |
| moves=n, **{f"share_{k}": cnt[k] / tot for k in KINDS}, | |
| domains=len(domains), reads_per_search=(reads / searches) if searches else None, | |
| wall=r["wall_s"]) | |
| json.dump(runs, open(Path(sys.argv[2]) / "runs_profile.json", "w"), indent=0) | |
| FEATS = [f"share_{k}" for k in KINDS] + ["domains", "moves"] | |
| def mean(xs): xs = [x for x in xs if x is not None]; return st.mean(xs) if xs else None | |
| models = ["P1", "DS", "GLM"] | |
| def by_model(feat, rs): | |
| return {m: mean([r[feat] for r in rs if r["model"] == m]) for m in models} | |
| def spread(feat, rs): | |
| v = by_model(feat, rs); xs = [x for x in v.values() if x is not None] | |
| return max(xs) - min(xs) | |
| def perm_p(feat, rs, n=5000, seed=0): | |
| rng = random.Random(seed); obs = spread(feat, rs) | |
| groups = collections.defaultdict(list) | |
| for r in rs: groups[(r["task"], r["repeat"])].append(r) | |
| hits = 0 | |
| for _ in range(n): | |
| shuf = [] | |
| for g in groups.values(): | |
| labs = [r["model"] for r in g]; rng.shuffle(labs) | |
| shuf += [dict(r, model=l) for r, l in zip(g, labs)] | |
| if spread(feat, shuf) >= obs - 1e-12: hits += 1 | |
| return (hits + 1) / (n + 1) | |
| rs = list(runs.values()) | |
| print("ALL TASKS (n=%d runs)" % len(rs)) | |
| res = {} | |
| for f in FEATS: | |
| v = by_model(f, rs); p = perm_p(f, rs) | |
| res[f] = {"byModel": {m: round(x, 4) for m, x in v.items()}, "p": round(p, 4)} | |
| print(f"{f:16s}", {m: round(x, 3) for m, x in v.items()}, "perm p =", round(p, 4)) | |
| json.dump(res, open(Path(sys.argv[2]) / "source_tests.json", "w"), indent=1) | |
| print("\nPER TASK, share of search / page / data / process (P1|DS|GLM)") | |
| for t in sorted({r['task'] for r in rs}): | |
| sub = [r for r in rs if r["task"] == t] | |
| row = [] | |
| for f in ["share_search", "share_page", "share_official", "share_data", "share_process", "domains"]: | |
| v = by_model(f, sub); row.append(f"{f[6:] if f.startswith('share') else f}=" + "|".join(f"{v[m]:.2f}" if v[m] is not None else "-" for m in models)) | |
| print(t, " ".join(row)) | |