Download vision/ocr/check.py from luokelabs/luoke-bench: direct link, hf CLI and curl.
- Browser
- Download file 5.21 kB
-
https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/vision/ocr/check.py
- Command line
-
hf download hf://datasets/luokelabs/luoke-bench/vision/ocr/check.py
-
curl -L -o check.py https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/vision/ocr/check.py
5.21 kB
| #!/usr/bin/env python3 | |
| """Checks the screenshot-reading set: vision/ocr/sets/screens.jsonl against vision/ocr/pages/. | |
| python3 -I vision/ocr/check.py | |
| What it checks: | |
| - every row has id, page, q, answers (non-empty list of strings), type, design; ids are unique (os001...); | |
| `type` is read / field / count / none; `today`, when present, is YYYY-MM-DD. | |
| - the page each row names exists as pages/<page>.html, and every page is used by at least one question. | |
| - `none` rows answer exactly ["图上没有"]; `count` rows start with a bare number. | |
| - for read and field rows, the FIRST answer (the canonical spelling) is found in the page's visible text | |
| (HTML tags removed, entities unescaped; also tried with all whitespace removed, since a page may split | |
| "11-23" and "22:16" into two elements). The other answers are accepted rewrites (1280 / 1280元 / ¥1,280.00, | |
| 明天18:00 for 11-25 18:00 ...) and are not looked up. count and none rows are not looked up either: their | |
| answers are a number we counted, or the fact that something is missing. | |
| - whether a none row's thing is really absent is a judgment call, left to the reviewer (the `design` says why). | |
| - no real brand or platform names on any page (a short block list from the shared rules). | |
| - warns (does not fail) when the rendered PNG is missing from the cache. | |
| """ | |
| import html | |
| import json | |
| import os | |
| import re | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| CACHE = os.path.abspath(os.path.expanduser(os.environ.get("LUOKE_BENCH_CACHE") or "~/.cache/luoke-bench")) | |
| IMG = os.path.join(CACHE, "vision", "img") | |
| TYPES = {"read", "field", "count", "none"} | |
| FIELDS = ["id", "page", "q", "answers", "type", "design"] | |
| BANNED = ["微信", "支付宝", "淘宝", "美团", "12306", "滴滴", "钉钉", "飞书", "小红书", "抖音", "B站", "iPhone", "iPad", | |
| "MacBook", "Apple", "苹果公司", "华为", "小米", "京东", "拼多多", "顺丰", "菜鸟", "饿了么", "携程", "Google", "谷歌"] | |
| def visible_text(raw): | |
| raw = re.sub(r"<(style|script|title)[^>]*>.*?</\1>", " ", raw, flags=re.S | re.I) | |
| raw = re.sub(r"<br\s*/?>", "\n", raw, flags=re.I) | |
| raw = re.sub(r"<[^>]+>", "", raw) | |
| return html.unescape(raw).replace("\xa0", " ") | |
| def main(): | |
| errors, warns = [], [] | |
| pdir = os.path.join(HERE, "pages") | |
| pages = {f[:-5]: open(os.path.join(pdir, f), encoding="utf-8").read() for f in os.listdir(pdir) if f.endswith(".html")} | |
| texts = {k: visible_text(v) for k, v in pages.items()} | |
| rows = [json.loads(line) for line in open(os.path.join(HERE, "sets", "screens.jsonl"), encoding="utf-8") if line.strip()] | |
| seen, used, types = set(), set(), {} | |
| for i, r in enumerate(rows, 1): | |
| rid = r.get("id", f"line {i}") | |
| for f in FIELDS: | |
| if f not in r or r[f] in ("", None, []): | |
| errors.append(f"{rid}: missing {f}") | |
| if rid in seen: | |
| errors.append(f"{rid}: duplicate id") | |
| seen.add(rid) | |
| if not re.fullmatch(r"os\d{3}", str(rid)): | |
| errors.append(f"{rid}: id should look like os001") | |
| t = r.get("type") | |
| types[t] = types.get(t, 0) + 1 | |
| if t not in TYPES: | |
| errors.append(f"{rid}: bad type {t}") | |
| if "today" in r and not re.fullmatch(r"\d{4}-\d{2}-\d{2}", str(r["today"])): | |
| errors.append(f"{rid}: bad today {r['today']}") | |
| ans = r.get("answers") or [] | |
| if not all(isinstance(a, str) and a.strip() for a in ans): | |
| errors.append(f"{rid}: answers must be non-empty strings") | |
| continue | |
| p = r.get("page") | |
| if p not in pages: | |
| errors.append(f"{rid}: page {p} not found") | |
| continue | |
| used.add(p) | |
| if t == "none" and ans != ["图上没有"]: | |
| errors.append(f"{rid}: none rows answer exactly 图上没有") | |
| if t == "count" and not re.fullmatch(r"\d+", ans[0]): | |
| errors.append(f"{rid}: count rows start with a bare number") | |
| if t in ("read", "field"): | |
| txt = texts[p] | |
| a = ans[0] | |
| if a not in txt and re.sub(r"\s+", "", a) not in re.sub(r"\s+", "", txt): | |
| errors.append(f"{rid}: first answer {a!r} not in the text of {p}") | |
| if not os.path.exists(os.path.join(IMG, p + ".png")): | |
| warns.append(f"{p}: no PNG in {IMG} (run screen/render.py)") | |
| for p in sorted(set(pages) - used): | |
| errors.append(f"{p}: page has no question") | |
| for p, txt in sorted(texts.items()): | |
| for b in BANNED: | |
| if b in txt: | |
| errors.append(f"{p}: real name {b!r} on the page") | |
| if re.search(r"https?://|www\.", txt): | |
| errors.append(f"{p}: a real-looking link") | |
| for dom in re.findall(r"\b[a-z0-9-]+\.(?:com|cn|net|org|io)\b", txt): | |
| errors.append(f"{p}: domain {dom} is not .example") | |
| for w in sorted(set(warns)): | |
| print("warn:", w) | |
| for e in errors: | |
| print("ERROR:", e) | |
| print(f"{len(rows)} questions on {len(used)} of {len(pages)} pages; types {dict(sorted(types.items()))}") | |
| print("ok" if not errors else f"{len(errors)} errors") | |
| sys.exit(1 if errors else 0) | |
| if __name__ == "__main__": | |
| main() | |