Download vision/compare/unit.py from luokelabs/luoke-bench: direct link, hf CLI and curl.
- Browser
- Download file 11.6 kB
-
https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/vision/compare/unit.py
- Command line
-
hf download hf://datasets/luokelabs/luoke-bench/vision/compare/unit.py
-
curl -L -o unit.py https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/vision/compare/unit.py
11.6 kB
| """两图对比的出题和判分(接进 vision/run_vision.py 用)。 | |
| 一题两张图,**多图**:unit(item) 返回 | |
| [{"text", "media": "images", "images": [图1 绝对路径, 图2 绝对路径], "labels": ["第一张:", "第二张:"], "expect", "check"}] | |
| 接法和 album/ 一样:run_vision.messages_for 里 `if unit["media"] == "images": return part_module(part).messages(unit)` | |
| (每张图前面一行 label,最后是 text)。 | |
| 判分(check 返回 {"score": 0/1, ...}): | |
| - diff(说出差异):题后加「说出具体哪里不一样,没变的地方不用说。」facts 每条「关键词|同义」用 vlm.has 找, | |
| 覆盖率 ≥ 0.66 记 1(1–2 条要全中,3 条可漏 1 条,4 条可漏 1 条);never(没改的地方说成改了、旧值当新值)命中就 0。 | |
| - which(哪一张):题后加「回答第一张还是第二张。」先找「第X张(才)是…新 / 更新 / 最新 / P过 / 改过 / 降价后」这样的句子 | |
| (中间有「不没未」的不算),找不到就看回答里**最后**提到的那张(第一张 / 第1张 / 图1 / 前一张 / 左边……)。 | |
| 「第二张是旧版,第一张才是更新后的」判第一张。 | |
| - point(一个短答案):题后加「只回答答案本身,尽量短。」answers(「|」隔开的都算)用 vlm.has 找到,且没有 never | |
| (错的候选都写在 never 里:「C23(原来 B12)」可以,「B12」「鼠标和键盘」不行)。 | |
| - never 不算命中的情况照 board/:前 3 个字有否定词(不没别无未非),或前后约 6 个字里有「原来、原、旧、改成、改为、变成、 | |
| 从、之前、以前、第一张」这类说「改之前」的词(「B12 改成了 C23」里的 B12 不算);括号里说旧值的(「(原来 G7015 05车08A)」) | |
| 整段不看;同一分句里说「没变、不变、一样、保持」的整句不看(「飞行模式、勿扰模式没变」)。 | |
| """ | |
| import json | |
| import os | |
| import re | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, os.path.dirname(HERE)) | |
| import vlm # noqa: E402 | |
| CACHE = os.path.abspath(os.path.expanduser(os.environ.get("LUOKE_BENCH_CACHE") or "~/.cache/luoke-bench")) | |
| IMG = os.path.join(CACHE, "vision", "img") | |
| LEAD = "我发了两张图,第一张在前,第二张在后。\n" | |
| SUFFIX = {"diff": "\n说出具体哪里不一样,没变的地方不用说。", "which": "\n回答第一张还是第二张。", "point": "\n只回答答案本身,尽量短。"} | |
| COVER = 0.66 | |
| NEG = set("不没别勿无未非") | |
| CUES = ("原来", "原为", "原先", "原本", "原", "旧", "之前", "以前", "改成", "改为", "改到", "变成", "变为", "换成", "从", "→", "->", | |
| "第一张", "第1张", "图1", "降到", "涨到", "调到", "删", "去掉", "撤回") | |
| WIN = 6 | |
| SAME = ("没变", "不变", "未变", "没有变", "没改", "未改", "没动", "一样", "相同", "保持", "照旧") | |
| ONE = r"第一张|第1张|第一幅|第一个|图1|图一|前一张|前面那张|前面一张|左边|左图|上面那张|上一张|1号图" | |
| TWO = r"第二张|第2张|第二幅|第二个|图2|图二|后一张|后面那张|后面一张|右边|右图|下面那张|下一张|2号图" | |
| CLAUSE = re.compile(r"[^,。;;,!!??\n]+") | |
| PAREN = re.compile(r"[((][^(())]*[))]") | |
| def strip_old(reply): | |
| """Drop what talks about the old state or what stayed the same: a bracket with 原来 / 之前 / 从 … in it | |
| (「07车12C(原来G7015 05车08A)」), and a whole clause saying 没变 / 不变 / 一样 (「飞行模式、勿扰模式没变」).""" | |
| r = PAREN.sub(lambda m: " " if any(c in m.group(0) for c in CUES) else m.group(0), reply or "") | |
| return ",".join(c for c in CLAUSE.findall(r) if not any(w in c for w in SAME)) | |
| def never_hits(never, reply): | |
| t = vlm.norm(strip_old(reply)) | |
| hits = [] | |
| for spec in never: | |
| for alt in vlm.alts(spec): | |
| a = vlm.norm(alt) | |
| if not a: | |
| continue | |
| pre = r"(?<![\d.至])" if a[0].isdigit() else "" | |
| post = r"(?![\d.])" if a[-1].isdigit() else "" | |
| for m in re.finditer(pre + re.escape(a) + post, t): | |
| around = t[max(0, m.start() - WIN):m.start()] + "|" + t[m.end():m.end() + WIN] | |
| after = t[m.end():m.end() + 6] | |
| if not NEG & set(t[max(0, m.start() - 3):m.start()]) and not any(vlm.norm(c) in around for c in CUES if vlm.norm(c)) \ | |
| and not any(w in after for w in SAME): | |
| hits.append(alt) | |
| break | |
| return hits | |
| PICK = re.compile(r"(" + ONE + "|" + TWO + r")\s*(?:才是|就是|是|为)?([^,。;,;!!??\n]{0,16}?)" | |
| r"(新版|新的|更新|最新|P过|p过|PS过|ps过|修过|改过|降价后|降价以后|降过价|降价了|已降价)") | |
| def which_of(reply): | |
| """1 or 2: the picture a 「第X张(才)是…新 / 更新 / P过 / 降价后」 clause names; else the one named last.""" | |
| r = PAREN.sub(lambda m: re.sub(r"[,,;;]", " ", m.group(0)), reply or "") | |
| for m in PICK.finditer(r): | |
| if not NEG & set(m.group(2)): | |
| return 1 if re.fullmatch(ONE, m.group(1)) else 2 | |
| last = [(m.start(), 1) for m in re.finditer(ONE, r)] + [(m.start(), 2) for m in re.finditer(TWO, r)] | |
| if last: | |
| return max(last)[1] | |
| s = r.strip() | |
| if re.match(r"^[((]?[1一][))]?\W*$", s): | |
| return 1 | |
| if re.match(r"^[((]?[2二][))]?\W*$", s): | |
| return 2 | |
| return None | |
| def check(item, reply): | |
| kind, never = item["kind"], item.get("never", []) | |
| if kind == "which": | |
| got = which_of(reply) | |
| return {"score": float(got == int(item["answers"][0])), "picked": got} | |
| bad = never_hits(never, reply) | |
| if kind == "point": | |
| answers = [a for spec in item["answers"] for a in vlm.alts(spec)] | |
| return {"score": float(not bad and (vlm.short_match(answers, reply or "") or any(vlm.has(reply or "", a) for a in answers))), | |
| "never": bad} | |
| facts = item["facts"] | |
| missed = [f for f in facts if not vlm.has(reply or "", f)] | |
| cover = 1 - len(missed) / len(facts) | |
| return {"score": float(cover >= COVER and not bad), "coverage": round(cover, 3), "missed": missed, "never": bad} | |
| def expect(item): | |
| if item["kind"] == "which": | |
| return "第一张" if item["answers"][0] == "1" else "第二张" | |
| if item["kind"] == "point": | |
| return vlm.alts(item["answers"][0])[0] | |
| return ";".join(vlm.alts(f)[0] for f in item["facts"]) | |
| def unit(item): | |
| return [{"text": LEAD + item["q"] + SUFFIX[item["kind"]], "media": "images", | |
| "images": [os.path.join(IMG, p) for p in item["images"]], "labels": ["第一张:", "第二张:"], | |
| "expect": expect(item), "check": lambda rep: check(item, rep)}] | |
| def messages(u): | |
| """One user turn: each picture after its label, then the question.""" | |
| content = [] | |
| for lab, path in zip(u.get("labels") or [None] * len(u["images"]), u["images"]): | |
| if lab: | |
| content.append({"type": "text", "text": lab}) | |
| content.append(vlm.image_part(path)) | |
| return [{"role": "user", "content": content + [{"type": "text", "text": u["text"]}]}] | |
| def load(): | |
| path = os.path.join(HERE, "sets", "compare.jsonl") | |
| return [json.loads(line) for line in open(path, encoding="utf-8") if line.strip()] | |
| def selftest(): | |
| ok = True | |
| items = load() | |
| for it in items: | |
| u = unit(it)[0] | |
| right, wrong = u["check"](u["expect"]), u["check"]("我不太确定。") | |
| if right["score"] != 1.0 or wrong["score"] != 0.0: | |
| print(f" {it['id']}: 参考答案 {right},乱答 {wrong}") | |
| ok = False | |
| for p in u["images"]: | |
| if not os.path.exists(p): | |
| print(f" {it['id']}: 缺图 {os.path.basename(p)}") | |
| ok = False | |
| def find(q): | |
| return next(i for i in items if i["q"].startswith(q)) | |
| cases = [ | |
| (find("哪张截图是降价以后的"), "第二张是降价后的,第一张还是原价", 1), (find("哪张截图是降价以后的"), "第一张", 0), | |
| (find("哪张截图是降价以后的"), "图2", 1), (find("哪张截图是降价以后的"), "2", 1), | |
| (find("哪张是降价以后的"), "第一张(3,500 元/月,已降价),第二张是 3,800", 1), | |
| (find("我现在应该去哪个登机口"), "C23", 1), (find("我现在应该去哪个登机口"), "C23 登机口(原来是 B12)", 1), | |
| (find("我现在应该去哪个登机口"), "B12", 0), | |
| (find("两份报价单哪家"), "森泰家具更便宜,便宜 900 元", 1), (find("两份报价单哪家"), "森泰更便宜,便宜 1,900 元", 0), | |
| (find("这两张设置截图"), "蓝牙从开变成关,低电量模式打开了", 1), | |
| (find("这两张设置截图"), "蓝牙关了,低电量模式开了,勿扰模式也开了", 0), | |
| (find("这两张设置截图"), "蓝牙关了,低电量模式开了,勿扰模式没变", 1), | |
| (find("菜单上哪些涨价了"), "牛肉面 28→32,卤蛋 3→4", 1), (find("菜单上哪些涨价了"), "牛肉面、卤蛋、酸辣粉", 0), | |
| (find("两次查的路线"), "多了 9 分钟", 1), (find("两次查的路线"), "19分钟", 0), | |
| (find("这两张表哪里不一样"), "华东 10 月从 12,800 改成 13,200,还多了西南一行", 1), | |
| (find("这两张表哪里不一样"), "多了西南", 0), | |
| (find("第二张比第一张少了什么"), "沙滩上的那只鸟不见了", 1), (find("第二张购物车"), "无线鼠标和键盘", 0), | |
| (find("第二张比第一张少了什么"), "鸟", 1), (find("第二张比第一张少了什么"), "一只水鸟", 1), | |
| (find("哪一张是P过的"), "第一张,海上多了一艘帆船", 1), | |
| (find("这两张是同一个通知"), "第二张是旧版,第一张才是更新后的", 1), (find("这两张是同一个通知"), "第一张(标题带「更新」)", 1), | |
| (find("这两张是同一个通知"), "第一张不是新的,第二张是", 0), | |
| (find("哪张是最新的物流"), "第二张", 1), (find("哪张是最新的物流"), "第一张还是运输中,第二张已经在派送了", 1), | |
| (find("这两张设置截图"), "蓝牙关了,低电量模式开了;飞行模式、勿扰模式没变", 1), | |
| (find("这两张设置截图"), "蓝牙关了,低电量模式打开了,其他(无线局域网、自动亮度)不变", 1), | |
| (find("改签以后"), "G7023,07车12C(原来G7015 05车08A)", 1), (find("改签以后"), "G7023,05车08A", 0), | |
| (find("第二张账单"), "多了一笔11月24日小禾外卖的支出,-46.50元", 1), | |
| (find("这两张图有什么不一样"), "第二张直升机是紫色的,第一张是红色的", 1), | |
| (find("这两张照片哪里不一样?"), "第一张有7只羊,第二张只有5只", 1), | |
| (find("这两张照片有什么不同"), "酱碟和面包从右边跑到了左边", 1), | |
| (find("两张照片里,中间那辆车"), "第一张是橙色的,第二张是蓝色的", 1), | |
| ] | |
| for it, rep, want in cases: | |
| got = check(it, rep)["score"] | |
| if got != want: | |
| print(f" 用例 {it['id']} {rep!r}: 得 {got},应 {want}") | |
| ok = False | |
| print(f"compare selftest {'OK' if ok else 'FAIL'}:{len(items)} 题,{len(cases)} 条用例") | |
| return ok | |
| if __name__ == "__main__": | |
| sys.exit(0 if selftest() else 1) | |