DerHansVader's picture
Add default inference system prompt
1b8f552 verified
Raw
History Blame Contribute Delete
5.37 kB
#!/usr/bin/env python3
"""Pointerbench-Sheets official scorer.
Metric: **point-in-bbox accuracy** (the ScreenSpot standard). A prediction is
correct when the predicted click point falls inside the target's ground-truth
bounding box. Reports overall accuracy plus per-category, per-UI-style, and
per-data-type breakdowns. Pure standard library, no dependencies.
Ground truth is read from `data/test/metadata.jsonl` (shipped with the repo).
Predictions file: JSONL or JSON list, one object per example, e.g.
{"id": "pbs_0001", "point": [612, 388]}
Accepted point keys: "point" / "pred" / "coordinate", or flat "x" and "y".
Coordinates are absolute pixels on the 1024x768 image.
Usage:
python eval.py --show-system-prompt
python eval.py --predictions preds.jsonl
python eval.py --predictions preds.jsonl --json report.json
"""
from __future__ import annotations
import argparse
import json
from collections import defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parent
GT_PATH = ROOT / "data" / "test" / "metadata.jsonl"
DEFAULT_SYSTEM_PROMPT = (
"You are evaluating Pointerbench, a GUI grounding benchmark. "
"You will receive one 1024x768 screenshot and one task instruction. "
"Use absolute pixel coordinates with origin at the top-left of the image. "
"Do not return normalized coordinates. Do not crop or resize the coordinate frame. "
"For point tasks, return JSON like {\"point\": [x, y]}. "
"For bounding-box tasks, return JSON like {\"bbox\": [x0, y0, x1, y1]}."
)
def _load_jsonl(path: Path) -> list[dict]:
text = path.read_text(encoding="utf-8").strip()
if not text:
return []
if text[0] == "[": # tolerate a JSON array too
return json.loads(text)
return [json.loads(ln) for ln in text.splitlines() if ln.strip()]
def _point(rec: dict) -> tuple[float, float] | None:
for key in ("point", "pred", "coordinate", "prediction"):
v = rec.get(key)
if isinstance(v, (list, tuple)) and len(v) >= 2:
return float(v[0]), float(v[1])
if "x" in rec and "y" in rec:
return float(rec["x"]), float(rec["y"])
return None
def _in_bbox(pt: tuple[float, float], bbox: list[int]) -> bool:
x0, y0, x1, y1 = bbox
return min(x0, x1) <= pt[0] <= max(x0, x1) and min(y0, y1) <= pt[1] <= max(y0, y1)
def evaluate(gt: list[dict], preds: dict[str, dict]) -> dict:
by = {"category": defaultdict(lambda: [0, 0]),
"ui_style": defaultdict(lambda: [0, 0]),
"data_type": defaultdict(lambda: [0, 0])}
hits = missing = 0
for ex in gt:
pred = preds.get(ex["id"])
pt = _point(pred) if pred else None
if pt is None:
missing += 1
ok = False
else:
ok = _in_bbox(pt, ex["bbox"])
hits += ok
for axis in by:
cell = by[axis][ex[axis]]
cell[0] += ok
cell[1] += 1
n = len(gt)
def table(axis: str) -> dict:
return {k: {"acc": round(v[0] / v[1], 4), "n": v[1]}
for k, v in sorted(by[axis].items())}
return {
"n": n,
"accuracy": round(hits / n, 4) if n else 0.0,
"hits": hits,
"missing_predictions": missing,
"by_category": table("category"),
"by_ui_style": table("ui_style"),
"by_data_type": table("data_type"),
}
def _print(report: dict) -> None:
print(f"\nPointerbench-Sheets: {report['n']} examples")
print("=" * 44)
print(f"Accuracy: {report['accuracy'] * 100:5.2f}% "
f"({report['hits']}/{report['n']})")
if report["missing_predictions"]:
print(f" ! {report['missing_predictions']} examples had no prediction "
f"(counted as wrong)")
for axis, title in (("by_category", "By category"),
("by_ui_style", "By UI style"),
("by_data_type", "By data type")):
print(f"\n{title}:")
for k, v in report[axis].items():
print(f" {k:18s} {v['acc'] * 100:5.2f}% (n={v['n']})")
print()
def main() -> None:
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--show-system-prompt", action="store_true",
help="print the recommended inference system prompt and exit")
ap.add_argument("--predictions", type=Path,
help="JSONL/JSON predictions: {id, point:[x,y]} per example")
ap.add_argument("--gt", type=Path, default=GT_PATH,
help=f"ground-truth metadata (default: {GT_PATH})")
ap.add_argument("--json", type=Path, default=None,
help="also write the full report to this JSON path")
args = ap.parse_args()
if args.show_system_prompt:
print(DEFAULT_SYSTEM_PROMPT)
return
if args.predictions is None:
ap.error("--predictions is required unless --show-system-prompt is used")
gt = _load_jsonl(args.gt)
preds = {r["id"]: r for r in _load_jsonl(args.predictions) if "id" in r}
report = evaluate(gt, preds)
_print(report)
if args.json:
args.json.write_text(json.dumps(report, indent=2), encoding="utf-8")
print(f"report -> {args.json}")
if __name__ == "__main__":
main()