File size: 3,044 Bytes
2c76448 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """Score the edit track: Semantic Consistency (SC) and Presentation Quality (PQ).
Predictions are read from <pred_dir>/edit/<id>.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("<instruction>", 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 <pred_dir>/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()
|