Download screen/render.py from luokelabs/luoke-bench: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/screen/render.py
- Command line
-
hf download hf://datasets/luokelabs/luoke-bench/screen/render.py
-
curl -L -o render.py https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/screen/render.py
3.8 kB
| #!/usr/bin/env python3 | |
| """Renders our own made-up pages (HTML in git) into the screenshots the questions show (PNG, in the cache). | |
| python3 screen/render.py screen/webqa/pages [more dirs] [--out <cache>/screen/img] [--only w001,w002] | |
| Every `<name>.html` becomes `<out>/<name>.png`. The page says how to shoot it in its head: | |
| <meta name="shot" content="phone"> 390 x 844 CSS px at 2x (an iPhone screenshot), the whole page down to 2400 px | |
| <meta name="shot" content="desktop"> 1440 x 900 at 1x (a browser window), the whole page down to 2400 px | |
| <meta name="shot" content="1280x720"> any other size, at 1x | |
| <meta name="shot-full" content="no"> only the first screen, not the whole page | |
| Pages use only macOS's own fonts (PingFang SC, Hiragino Sans GB, Songti SC, Menlo) and nothing from the network: | |
| every request leaving the page is blocked, so a page renders the same offline. The same page and the same machine give | |
| the same picture; other Macs may differ by a pixel here and there, which is why the PNGs themselves are what the | |
| release carries (vision-data-v1 / screen-data-v1), and this script is how they were made. | |
| Needs Playwright with its Chromium (`pip install playwright && playwright install chromium`). | |
| """ | |
| import argparse | |
| import os | |
| import re | |
| import sys | |
| CACHE = os.path.abspath(os.path.expanduser(os.environ.get("LUOKE_BENCH_CACHE") or "~/.cache/luoke-bench")) | |
| SIZES = {"phone": (390, 844, 2), "desktop": (1440, 900, 1)} | |
| MAX_H = 2400 | |
| def shot_of(html): | |
| m = re.search(r'<meta\s+name="shot"\s+content="([^"]+)"', html) | |
| kind = m.group(1) if m else "desktop" | |
| if kind in SIZES: | |
| w, h, scale = SIZES[kind] | |
| else: | |
| w, h = map(int, kind.lower().split("x")) | |
| scale = 1 | |
| full = not re.search(r'<meta\s+name="shot-full"\s+content="no"', html) | |
| return w, h, scale, full | |
| def render(paths, out): | |
| from playwright.sync_api import sync_playwright | |
| os.makedirs(out, exist_ok=True) | |
| with sync_playwright() as p: | |
| browser = p.chromium.launch() | |
| for path in paths: | |
| html = open(path, encoding="utf-8").read() | |
| w, h, scale, full = shot_of(html) | |
| ctx = browser.new_context(viewport={"width": w, "height": h}, device_scale_factor=scale, | |
| locale="zh-CN", timezone_id="Asia/Shanghai") | |
| page = ctx.new_page() | |
| page.route("**/*", lambda route: route.abort() if not route.request.url.startswith(("file:", "data:")) | |
| else route.continue_()) | |
| page.goto("file://" + os.path.abspath(path)) | |
| page.wait_for_timeout(50) | |
| height = h | |
| if full: | |
| height = min(MAX_H, max(h, page.evaluate("document.documentElement.scrollHeight"))) | |
| name = os.path.splitext(os.path.basename(path))[0] | |
| page.screenshot(path=os.path.join(out, name + ".png"), full_page=True, | |
| clip={"x": 0, "y": 0, "width": w, "height": height}) | |
| ctx.close() | |
| browser.close() | |
| def main(): | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("dirs", nargs="+") | |
| ap.add_argument("--out", default=os.path.join(CACHE, "screen", "img")) | |
| ap.add_argument("--only", help="names without .html, comma separated") | |
| a = ap.parse_args() | |
| only = set(a.only.split(",")) if a.only else None | |
| paths = [] | |
| for d in a.dirs: | |
| for f in sorted(os.listdir(d)): | |
| if f.endswith(".html") and (only is None or f[:-5] in only): | |
| paths.append(os.path.join(d, f)) | |
| if not paths: | |
| sys.exit("no pages") | |
| render(paths, a.out) | |
| print(f"{len(paths)} pages -> {a.out}") | |
| if __name__ == "__main__": | |
| main() | |