"""Score the edit track: Semantic Consistency (SC) and Presentation Quality (PQ). Predictions are read from /edit/.png. python eval/score_edit.py --pred_dir outputs/my_model --workers 16 SC = min(editing_success, overediting), judged from (source, edited) slides. PQ = unweighted mean of clarity, layout, style coherence, professionalism, judged from the edited slide alone. Edit score = sqrt(SC * PQ). One call per prompt, temperature 0.7, 512 tokens. """ import argparse import json import math import os import re from concurrent.futures import ThreadPoolExecutor from pathlib import Path from common import bench_path, chat, get_client, image_part, load_jsonl, load_prompt, mean, write_jsonl def _numbers(values, n): nums = [] for v in values if isinstance(values, list) else []: if isinstance(v, (int, float)): nums.append(float(v)) elif isinstance(v, str) and re.search(r"-?\d+(?:\.\d+)?", v): nums.append(float(re.search(r"-?\d+(?:\.\d+)?", v).group())) return (nums + [5.0] * n)[:n] # missing values fall back to the scale midpoint def score_case(client, case, img): sc_prompt = load_prompt("edit_evaluation_source_control.txt").replace("", case["instruction"]) sc_res = chat(client, "", [image_part(bench_path(case["source_image"])), image_part(img), {"type": "text", "text": sc_prompt}], 0.7, 512) pq_res = chat(client, "", [image_part(img), {"type": "text", "text": load_prompt("edit_evaluation_quality.txt")}], 0.7, 512) success, preserve = _numbers(sc_res.get("score"), 2) pq = mean(_numbers(pq_res.get("score"), 4)) sc = min(success, preserve) return {"id": case["id"], "operation": case["operation"], "editing_success": success, "edit_consistency": preserve, "sc": sc, "pq": pq, "edit_score": math.sqrt(max(sc, 0) * max(pq, 0))} def main(): ap = argparse.ArgumentParser() ap.add_argument("--pred_dir", required=True) ap.add_argument("--out", default=None, help="Defaults to /scores/edit.jsonl.") ap.add_argument("--workers", type=int, default=8) args = ap.parse_args() client = get_client() cases = load_jsonl(bench_path("data/edit.jsonl")) def run(case): img = Path(args.pred_dir) / "edit" / f"{case['id']}.png" if not img.exists(): return {"id": case["id"], "error": "missing prediction"} try: return score_case(client, case, str(img)) except Exception as e: return {"id": case["id"], "error": repr(e)} with ThreadPoolExecutor(args.workers) as ex: rows = list(ex.map(run, cases)) out = args.out or os.path.join(args.pred_dir, "scores", "edit.jsonl") write_jsonl(out, rows) ok = [r for r in rows if "sc" in r] sc, pq = mean(r["sc"] for r in ok), mean(r["pq"] for r in ok) print(json.dumps({"task": "edit", "sc": sc, "pq": pq, "edit": math.sqrt(sc * pq) if ok else None, "n_scored": len(ok)}, indent=1)) if __name__ == "__main__": main()