Download scripts/visual_gallery.py from ramachandrajoshi/LoopedPhai-A1-35B: direct link, hf CLI and curl.
- Browser
- Download file 5.89 kB
-
https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/scripts/visual_gallery.py
- Command line
-
hf download hf://ramachandrajoshi/LoopedPhai-A1-35B/scripts/visual_gallery.py
-
curl -L -o visual_gallery.py https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/scripts/visual_gallery.py
5.89 kB
| #!/usr/bin/env python3 | |
| """Blind A/B gallery of the visual-bench pages of two eval runs (eval_code_api.py --bench visual). | |
| python scripts/visual_gallery.py base looped_t3 --out runs/code_eval/gallery_base_vs_t3.html | |
| Writes ONE self-contained HTML file (screenshots embedded) to open in any browser. For each | |
| prompt the two models' renders are shown side by side in random order without labels; pick | |
| Left / Right / Tie (stored in the browser), then press "Reveal" for the labels and the tally. | |
| "Live" runs the page itself in a sandboxed iframe (scripts on, no access to the gallery or disk). | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import base64 | |
| import html | |
| import json | |
| import random | |
| from pathlib import Path | |
| PAGE = """<!doctype html><html><head><meta charset="utf-8"><title>Visual A/B</title> | |
| <style> | |
| body{font-family:system-ui,sans-serif;margin:24px;background:#f6f6f4;color:#222} | |
| .task{background:#fff;border:1px solid #ddd;border-radius:8px;padding:16px;margin-bottom:24px} | |
| .prompt{font-size:13px;color:#555;white-space:pre-wrap;max-height:6em;overflow:auto} | |
| .pair{display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-top:10px} | |
| .side img,.side iframe{width:100%;aspect-ratio:16/10;border:1px solid #ccc;background:#eee} | |
| .side .lab{font-weight:600;margin:4px 0;visibility:hidden} | |
| .reveal .side .lab{visibility:visible} | |
| .err{color:#b00;font-size:12px} | |
| button{margin-right:6px;padding:6px 12px;border:1px solid #888;border-radius:6px;background:#fafafa;cursor:pointer} | |
| button.on{background:#222;color:#fff} | |
| #tally{font-size:18px;font-weight:600;margin:12px 0} | |
| </style></head><body> | |
| <h1>Blind visual comparison</h1> | |
| <p>Judge each pair against its prompt. Your choices are saved in this browser. <button id="rev">Reveal labels & tally</button></p> | |
| <div id="tally"></div> | |
| __TASKS__ | |
| <script> | |
| const KEY = "__KEY__"; | |
| const votes = JSON.parse(localStorage.getItem(KEY) || "{}"); | |
| function paint(){document.querySelectorAll(".task").forEach(t=>{t.querySelectorAll(".vote").forEach(b=> | |
| b.classList.toggle("on", votes[t.dataset.id]===b.dataset.v));});} | |
| document.querySelectorAll(".vote").forEach(b=>b.onclick=()=>{votes[b.closest(".task").dataset.id]=b.dataset.v; | |
| localStorage.setItem(KEY, JSON.stringify(votes)); paint();}); | |
| document.querySelectorAll(".live").forEach(b=>b.onclick=()=>{const s=b.closest(".side");const img=s.querySelector("img"); | |
| const f=document.createElement("iframe");f.setAttribute("sandbox","allow-scripts"); | |
| f.srcdoc=new TextDecoder().decode(Uint8Array.from(atob(b.dataset.page),c=>c.charCodeAt(0))); | |
| img.replaceWith(f);b.remove();}); | |
| document.getElementById("rev").onclick=()=>{document.body.classList.add("reveal");const c={}; | |
| document.querySelectorAll(".task").forEach(t=>{const v=votes[t.dataset.id];if(!v)return; | |
| const w=v==="tie"?"tie":t.querySelector(`.side[data-pos="${v}"]`).dataset.tag;c[w]=(c[w]||0)+1;}); | |
| document.getElementById("tally").textContent="Wins: "+Object.entries(c).map(([k,n])=>k+" "+n).join(" · ")+ | |
| " (judged "+Object.keys(votes).length+" / "+document.querySelectorAll(".task").length+")";}; | |
| paint(); | |
| </script></body></html>""" | |
| def side(run_dir: Path, tag: str, tid: str, sample: int, rec: dict, pos: str) -> str: | |
| stem = run_dir / "visual" / f"{tid}_s{sample}" | |
| png, page = stem.with_suffix(".png"), stem.with_suffix(".html") | |
| img = (f'<img src="data:image/png;base64,{base64.b64encode(png.read_bytes()).decode()}">' if png.exists() | |
| else '<div class="err">no page produced</div>') | |
| live = (f'<button class="live" data-page="{base64.b64encode(page.read_bytes()).decode()}">Live</button>' | |
| if page.exists() else "") | |
| r = rec.get("render") or {} | |
| errs = "; ".join((r.get("page_errors") or [])[:2]) or rec.get("detail", "") | |
| return (f'<div class="side" data-pos="{pos}" data-tag="{html.escape(tag)}"><div class="lab">{html.escape(tag)}</div>' | |
| f'{img}<div>{live}</div><div class="err">{html.escape(errs)}</div></div>') | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("tag_a") | |
| ap.add_argument("tag_b") | |
| ap.add_argument("--runs", default="runs/code_eval") | |
| ap.add_argument("--visual-file", default=str(Path(__file__).resolve().parent.parent / "data" / "visual_prompts.jsonl")) | |
| ap.add_argument("--out", default=None) | |
| ap.add_argument("--seed", type=int, default=0) | |
| args = ap.parse_args() | |
| runs = Path(args.runs) | |
| prompts = {r["id"]: r["prompt"] for r in map(json.loads, open(args.visual_file, encoding="utf-8")) if r} | |
| def recs(tag): | |
| d = {} | |
| for r in map(json.loads, open(runs / tag / "results.jsonl", encoding="utf-8")): | |
| if r["bench"] == "visual": | |
| d[(r["id"], r["sample"])] = r | |
| return d | |
| ra, rb = recs(args.tag_a), recs(args.tag_b) | |
| rng = random.Random(args.seed) | |
| blocks = [] | |
| for key in sorted(set(ra) & set(rb)): | |
| tid, s = key | |
| pair = [(args.tag_a, ra[key]), (args.tag_b, rb[key])] | |
| rng.shuffle(pair) | |
| sides = "".join(side(runs / tag, tag, tid, s, rec, pos) for (tag, rec), pos in zip(pair, ("left", "right"))) | |
| blocks.append( | |
| f'<div class="task" data-id="{html.escape(tid)}_s{s}"><h3>{html.escape(tid)} (sample {s})</h3>' | |
| f'<div class="prompt">{html.escape(prompts.get(tid, ""))}</div><div class="pair">{sides}</div>' | |
| '<p><button class="vote" data-v="left">Left better</button><button class="vote" data-v="tie">Tie</button>' | |
| '<button class="vote" data-v="right">Right better</button></p></div>') | |
| out = Path(args.out or runs / f"gallery_{args.tag_a}_vs_{args.tag_b}.html") | |
| key = f"visual-ab:{args.tag_a}:{args.tag_b}:{args.seed}" | |
| out.write_text(PAGE.replace("__TASKS__", "\n".join(blocks)).replace("__KEY__", key), encoding="utf-8") | |
| print(f"wrote {out} ({len(blocks)} pairs)") | |
| if __name__ == "__main__": | |
| main() | |