File size: 5,271 Bytes
e39a6b5 | 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #!/usr/bin/env python3
"""多页对比(multi)的打分接口:unit(item) -> {images, text, expect, check};selftest() -> bool。
python3 -I screen/multi/unit.py # 跑 selftest
提示:「这是我手机/电脑(或手机和电脑)上的 N 张截图,是我在几个页面之间来回看的。今天是 2026年11月24日(周二)。{q}
只回答答案本身,尽量短;截图上找不到就回答「截图上没有」。」
判法同 webqa_zh:answers 里任一写法,回答归一化后相等或只多几个字(vlm.short_match,数字不从大数里截)。
题表也可以用 facts([关键词, ...],每个 `|` 隔开同义写法)代替 answers:全部出现、never 一个都不出现、回答不超过 60 个字才算对
(这期 40 题都用 answers)。
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(HERE))
import run_screen # noqa: E402 (puts vision/ on the path for vlm)
import vlm # noqa: E402
SET = os.path.join(HERE, "sets", "multi.jsonl")
PROMPT = ("这是我{dev}上的 {n} 张截图,是我在几个页面之间来回看的。今天是 2026年11月24日(周二)。{q}\n"
"只回答答案本身,尽量短;截图上找不到就回答「截图上没有」。")
FACTS_MAX = 60
def load(path=SET):
return [json.loads(l) for l in open(path, encoding="utf-8") if l.strip()]
def prompt(item):
shots = set(item.get("shots") or ["phone"])
dev = "手机和电脑" if len(shots) > 1 else ("电脑" if "desktop" in shots else "手机")
return PROMPT.format(dev=dev, n=len(item["pages"]), q=item["q"])
def facts_score(item, reply):
text = vlm.strip_think(reply or "")
missed = [f for f in item["facts"] if not vlm.has(text, f)]
never = [n for n in item.get("never", []) if vlm.has(text, n)]
long_ = len(vlm.norm(text)) > FACTS_MAX
return {"score": float(not missed and not never and not long_), "missed": missed, "never": never, "too_long": long_}
def unit(item):
images = [run_screen.pic(p) for p in item["pages"]]
if item.get("facts"):
return {"images": images, "text": prompt(item), "expect": ",".join(vlm.alts(f)[0] for f in item["facts"]),
"check": lambda rep: facts_score(item, rep)}
return {"images": images, "text": prompt(item), "expect": item["answers"][0],
"check": lambda rep: float(vlm.short_match(item["answers"], vlm.strip_think(rep or "")))}
def selftest():
ok = True
def expect(name, got, want):
nonlocal ok
if got != want:
ok = False
print(f"FAIL {name}: got {got!r}, want {want!r}")
rows = load()
for it in rows:
u = unit(it)
expect(f"{it['id']} 参考答案", run_screen.as_score(u["check"](u["expect"])), 1.0)
expect(f"{it['id']} 乱答", run_screen.as_score(u["check"]("我不太确定。")), 0.0)
missing = [p for p in u["images"] if not os.path.exists(p)]
if missing:
ok = False
print(f"FAIL {it['id']} 没有截图 {os.path.basename(missing[0])}")
by = {it["id"]: it for it in rows}
chk = lambda i, rep: run_screen.as_score(unit(by[i])["check"](rep)) # noqa: E731
cases = [
("mq001", "蓝鲸电器旗舰店,299 元", 1.0), ("mq001", "橙光小家电", 0.0), ("mq001", "禾木优选", 0.0),
("mq006", "约 49.8 元/公斤", 1.0), ("mq006", "49.9", 0.0),
("mq010", "1179元", 1.0), ("mq010", "1199", 0.0), ("mq010", "11790", 0.0),
("mq021", "18:10 的 YX5106", 1.0), ("mq021", "YX5110", 0.0),
("mq024", "G7318", 1.0), ("mq024", "G7326", 0.0),
("mq034", "下午2点", 1.0), ("mq034", "17:00", 0.0),
("mq037", "YX8803", 1.0), ("mq037", "YX8801", 0.0),
("mq039", "B301", 1.0), ("mq039", "会议室 A", 0.0),
("mq025", "11月30日", 1.0), ("mq025", "12月1日", 0.0),
("mq029", "<think>9:30 减 30 分钟再减 45</think>8:15", 1.0),
# 复核实测:第一个分句是肯定/否定词就算对(vlm.short_match)
("mq023", "不能,到会场要10:30", 1.0), ("mq026", "不能,末班车00:30已经开走", 1.0),
("mq030", "不能,8:15就得出门", 1.0), ("mq030", "不行,周会8:30才结束", 1.0),
("mq031", "没有,还是普陀区的地址", 1.0), ("mq023", "不能确定,要看当天地铁和出站排队的情况", 0.0),
("mq024", "07:40那班", 1.0), ("mq024", "7:40", 1.0),
# 一长串把每家价格都列出来的不算(short_match 只容几个多余的字)
("mq008", "暖杯 133.5,禾木 143,山岚 158,所以最少 133.5 元", 0.0),
]
for i, rep, want in cases:
expect(f"{i} 答「{rep}」", chk(i, rep), want)
f = {"facts": ["九州", "409"], "never": ["南风"]}
expect("facts 都对", facts_score(f, "九州运动专营店,409 元")["score"], 1.0)
expect("facts 缺一个", facts_score(f, "九州运动专营店")["score"], 0.0)
expect("facts 踩 never", facts_score(f, "九州 409,南风缺货")["score"], 0.0)
print("selftest", "ok" if ok else "FAILED", f"({len(rows)} items)")
return ok
if __name__ == "__main__":
sys.exit(0 if selftest() else 1)
|