Datasets:
Upload summarize.py with huggingface_hub
Browse files- summarize.py +88 -0
summarize.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Aggregate results/*.jsonl into a comparison table."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
import pandas as pd
|
| 11 |
+
|
| 12 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent / "bench"))
|
| 13 |
+
import oolong # noqa: E402
|
| 14 |
+
|
| 15 |
+
RESULTS = Path(__file__).resolve().parent / "results"
|
| 16 |
+
|
| 17 |
+
# Published list prices, USD per million tokens (input, output). Cache reads bill
|
| 18 |
+
# at 0.1x input and cache writes at 1.25x input.
|
| 19 |
+
PRICES = {
|
| 20 |
+
"claude-haiku-4-5-20251001": (1.0, 5.0),
|
| 21 |
+
"claude-sonnet-4-6": (3.0, 15.0),
|
| 22 |
+
"claude-opus-4-8": (15.0, 75.0),
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def api_cost(row, model: str) -> float:
|
| 27 |
+
"""Cost from raw token counts at list prices.
|
| 28 |
+
|
| 29 |
+
The CLI's own total_cost_usd also bills a small hidden Haiku call it makes per
|
| 30 |
+
invocation, so this is the apples-to-apples number for model comparisons.
|
| 31 |
+
"""
|
| 32 |
+
pin, pout = PRICES[model.replace("[1m]", "")]
|
| 33 |
+
return (
|
| 34 |
+
row.get("usage_input_tokens", 0) * pin
|
| 35 |
+
+ row.get("usage_cache_creation_tokens", 0) * pin * 1.25
|
| 36 |
+
+ row.get("usage_cache_read_tokens", 0) * pin * 0.1
|
| 37 |
+
+ row.get("usage_output_tokens", 0) * pout
|
| 38 |
+
) / 1e6
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def load(tag: str) -> pd.DataFrame:
|
| 42 |
+
rows = [json.loads(l) for l in (RESULTS / f"{tag}.jsonl").read_text().splitlines() if l.strip()]
|
| 43 |
+
df = pd.json_normalize(rows, sep="_")
|
| 44 |
+
meta = json.loads((RESULTS / f"{tag}.meta.json").read_text())
|
| 45 |
+
df = df.assign(tag=tag, **meta)
|
| 46 |
+
df["api_cost"] = df.apply(lambda r: api_cost(r, meta["model"]), axis=1)
|
| 47 |
+
|
| 48 |
+
# Guard the headline number against the answer extractor. `score` uses our
|
| 49 |
+
# clean_final() pre-step; `score_raw` is the official parser on the untouched
|
| 50 |
+
# response. If the two disagree by much, the gap is a parsing artifact rather
|
| 51 |
+
# than a capability difference.
|
| 52 |
+
df["score_raw"] = [
|
| 53 |
+
oolong.score_response({"answer": repr([g]), "answer_type": t}, a)["score"] if a else 0.0
|
| 54 |
+
for g, t, a in zip(df.gold, df.answer_type, df.full_answer)
|
| 55 |
+
]
|
| 56 |
+
return df
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def main(tags: list[str]) -> None:
|
| 60 |
+
tags = tags or sorted(p.stem for p in RESULTS.glob("*.jsonl"))
|
| 61 |
+
df = pd.concat([load(t) for t in tags], ignore_index=True)
|
| 62 |
+
|
| 63 |
+
agg = df.groupby(["tag", "mode", "model", "context_len"], as_index=False).agg(
|
| 64 |
+
n=("score", "size"),
|
| 65 |
+
score=("score", "mean"),
|
| 66 |
+
score_raw=("score_raw", "mean"),
|
| 67 |
+
exact=("score", lambda s: (s == 1.0).mean()),
|
| 68 |
+
errors=("error", lambda e: e.notna().sum()),
|
| 69 |
+
cost_per_q=("api_cost", "mean"),
|
| 70 |
+
cli_cost_per_q=("usage_cost_usd", "mean"),
|
| 71 |
+
calls_per_q=("usage_calls", "mean"),
|
| 72 |
+
sec_per_q=("seconds", "mean"),
|
| 73 |
+
)
|
| 74 |
+
for c in ("score", "score_raw", "exact"):
|
| 75 |
+
agg[c] = agg[c].round(3)
|
| 76 |
+
for c in ("cost_per_q", "cli_cost_per_q"):
|
| 77 |
+
agg[c] = agg[c].round(4)
|
| 78 |
+
agg[["calls_per_q", "sec_per_q"]] = agg[["calls_per_q", "sec_per_q"]].round(1)
|
| 79 |
+
print(agg.to_string(index=False))
|
| 80 |
+
|
| 81 |
+
print("\nper source dataset (mean score):")
|
| 82 |
+
print(df.pivot_table(index="dataset", columns="tag", values="score", aggfunc="mean").round(2).to_string())
|
| 83 |
+
print("\nper task (mean score):")
|
| 84 |
+
print(df.pivot_table(index="task", columns="tag", values="score", aggfunc="mean").round(2).to_string())
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
main(sys.argv[1:])
|