Download screen/digest/unit.py from luokelabs/luoke-bench: direct link, hf CLI and curl.
- Browser
- Download file 6.71 kB
-
https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/screen/digest/unit.py
- Command line
-
hf download hf://datasets/luokelabs/luoke-bench/screen/digest/unit.py
-
curl -L -o unit.py https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/screen/digest/unit.py
6.71 kB
| #!/usr/bin/env python3 | |
| """总结这页(digest)的出题和打分接口:unit(item) -> {images, text, expect, check};selftest() -> bool。 | |
| python3 -I screen/digest/unit.py # 跑 selftest | |
| 判法(一题一个 0/1,细节另报): | |
| - 必须事实覆盖率:need 为「必须」的事实里,`must` 每一项(`|` 隔开的同义写法任一个)都在回答里出现,这条才算覆盖; | |
| 覆盖率 = 覆盖的必须事实 / 必须事实数。可选事实另报覆盖率,不进总分。 | |
| - 硬性错(任一就 0 分): | |
| · 编数字:回答里的数字在页面上找不到(复用 meeting/summary/run_summary.invented_numbers,原文是这题所有截图的 | |
| HTML 去标签后的文字,含广告和评论——那些里的数字算「页面上有」,写进总结由 never 管;题里的 numbers_ok 是 | |
| 由原文算得出的数,比如两项费用相加的合计,也算有); | |
| · never:广告、评论区网友的说法、相关推荐的标题,回答里出现任一个。 | |
| - 及格 = 没有硬性错且必须事实覆盖率 ≥ 0.7。 | |
| - 字数(提示要求 400 字以内;run_summary.count 的数法:不算标点和空白)分档打折:400 字以内 1,600 以内 0.6, | |
| 800 以内 0.3,更长 0。一题得分 = 及格(1/0)× 字数折扣;字数、折扣另报(`chars`、`length_factor`)。 | |
| """ | |
| import html as _html | |
| import json | |
| import os | |
| import re | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| SCREEN = os.path.dirname(HERE) | |
| ROOT = os.path.dirname(SCREEN) | |
| sys.path.insert(0, SCREEN) | |
| sys.path.insert(0, os.path.join(ROOT, "meeting", "summary")) | |
| import run_screen # noqa: E402 (puts vision/ on the path for vlm) | |
| import run_summary as RS # noqa: E402 | |
| import vlm # noqa: E402 | |
| SET = os.path.join(HERE, "sets", "digest.jsonl") | |
| PAGES = os.path.join(HERE, "pages") | |
| MAX_CHARS = 400 | |
| LENGTH_TIERS = ((400, 1.0), (600, 0.6), (800, 0.3)) # 字数上限 → 折扣;更长 0 | |
| PASS_COVERAGE = 0.7 | |
| PROMPT = "这是我{dev}上的截图{n}。{ask}\n(用中文回答,400 字以内。)" | |
| def load(path=SET): | |
| return [json.loads(l) for l in open(path, encoding="utf-8") if l.strip()] | |
| def page_text(name): | |
| raw = open(os.path.join(PAGES, name + ".html"), encoding="utf-8").read() | |
| body = re.sub(r"(?s)<(style|title)[^>]*>.*?</\1>", "", raw) | |
| return _html.unescape(re.sub(r"<[^>]+>", " ", body)) | |
| def source(item): | |
| return "\n".join(page_text(p) for p in item["pages"]) | |
| def prompt(item): | |
| n = len(item["pages"]) | |
| dev = "电脑" if item["device"] == "desktop" else "手机" | |
| return PROMPT.format(dev=dev, n=f"(共 {n} 张,是同一页往下翻的)" if n > 1 else "", ask=item["ask"]) | |
| def covered(fact, text): | |
| return all(vlm.has(text, m) for m in fact["must"]) | |
| def length_factor(chars): | |
| return next((f for top, f in LENGTH_TIERS if chars <= top), 0.0) | |
| def score(item, reply, src=None): | |
| text = RS.strip_think(reply or "") | |
| src = source(item) if src is None else src | |
| need = [f for f in item["facts"] if f["need"] == "必须"] | |
| opt = [f for f in item["facts"] if f["need"] != "必须"] | |
| miss = [f["text"] for f in need if not covered(f, text)] | |
| cov = (len(need) - len(miss)) / len(need) if need else 1.0 | |
| opt_cov = sum(covered(f, text) for f in opt) / len(opt) if opt else None | |
| chars = RS.count(text) | |
| invented = RS.invented_numbers(text, src + "\n" + " ".join(item.get("numbers_ok", []))) | |
| never = [n for n in item.get("never", []) if vlm.has(text, n)] | |
| hard = [] | |
| if invented: | |
| hard.append("编数字") | |
| if never: | |
| hard.append("never") | |
| ok = not hard and cov >= PASS_COVERAGE | |
| factor = length_factor(chars) | |
| return {"score": factor if ok else 0.0, "passed": ok, "length_factor": factor, "coverage": round(cov, 3), "optional_coverage": None if opt_cov is None else round(opt_cov, 3), | |
| "missed": miss, "chars": chars, "too_long": chars > MAX_CHARS, "invented_numbers": invented, "never": never, "hard": hard} | |
| def unit(item): | |
| src = source(item) | |
| return {"images": [run_screen.pic(p) for p in item["pages"]], "text": prompt(item), "expect": item["ref"], | |
| "check": lambda rep: score(item, rep, src)} | |
| def selftest(): | |
| rows = load() | |
| ok = True | |
| def expect(name, got, want): | |
| nonlocal ok | |
| if got != want: | |
| ok = False | |
| print(f"FAIL {name}: got {got}, want {want}") | |
| for it in rows: | |
| u = unit(it) | |
| r = u["check"](u["expect"]) | |
| expect(f"{it['id']} ref", (r["score"], r["coverage"], r["hard"]), (1.0, 1.0, [])) | |
| expect(f"{it['id']} unsure", u["check"]("我不太确定。")["score"], 0.0) | |
| it = rows[0] | |
| u = unit(it) | |
| chk = u["check"] | |
| ref = it["ref"] | |
| # 加一句广告或评论区的话:硬性错 | |
| r = chk(ref[:200] + "另外" + it["never"][0].split("|")[0] + "。") | |
| expect("never", "never" in r["hard"], True) | |
| # 编一个页面上没有的数字 | |
| r = chk(ref.replace("1.5万", "1.8万", 1)) | |
| expect("invented", (r["score"], "编数字" in r["hard"]), (0.0, True)) | |
| # 太长:按字数打折 | |
| expect("length tiers", [length_factor(n) for n in (400, 401, 600, 601, 800, 801)], [1.0, 0.6, 0.6, 0.3, 0.3, 0.0]) | |
| r = chk(ref + ref) | |
| expect("too long", (r["too_long"], r["hard"], r["passed"], r["score"] == length_factor(r["chars"])), (True, [], True, True)) | |
| # 千位分隔符、列表序号不算编数字 | |
| expect("thousands", RS.invented_numbers("车价 71,000 元", "车价 71000 元"), []) | |
| expect("list numbers", RS.invented_numbers("1. 甲\n13. **乙**", "甲乙"), []) | |
| expect("own char count", RS.invented_numbers("要点如上。(全文约380字)", "甲乙"), []) | |
| expect("still invented", RS.invented_numbers("预算 700 元(全文约380字)", "甲乙"), ["700"]) | |
| # 只说了一半:覆盖率不够 | |
| half = "成都发布了汽车以旧换新补贴细则,新能源车最高补1.5万元,燃油车最高8000元,12月1日开始申请。" | |
| r = chk(half) | |
| expect("half coverage < 0.7", (r["score"], r["coverage"] < 0.7, r["hard"]), (0.0, True, [])) | |
| # 思考段不算 | |
| r = chk("<think>川A 一周就没了 99999</think>" + ref) | |
| expect("think stripped", r["score"], 1.0) | |
| # 同义写法、数字换写法:12月1日写成 12/1 不算覆盖(must 里没列),但「九成」算 90% | |
| it3 = next(x for x in rows if x["id"] == "dg03") | |
| r = unit(it3)["check"](it3["ref"].replace("90%", "九成")) | |
| expect("synonym 九成", r["score"], 1.0) | |
| print("selftest", "ok" if ok else "FAILED", f"({len(rows)} items)") | |
| return ok | |
| if __name__ == "__main__": | |
| sys.exit(0 if selftest() else 1) | |