"""The example gallery: one card per ability of the decider, click to fill the ask bar and run. Data lives in examples/examples.json (+ images); the look in static/examples.css; the click handling in static/examples.js. `app.py` renders `gallery_html()` above the status line and listens on a hidden textbox for the clicked id. Nothing here touches answer(). Public API load_examples() -> dict (the parsed examples.json, measured blocks included) gallery_html() -> str (server-rendered gallery; static/examples.js makes it clickable) example_by_id(id) -> dict | None fill_values(ex, registry) -> dict with everything the ask bar needs to be filled from one example: state, questions[{type, instructions, options}], image (str path|None -- a path, not PIL: see fill_values), models (list[str] -- ONLY the vision models for vision_only examples) CSS / JS -> file contents for app.py to append to its launch(css=, js=) Standalone preview: python examples.py --demo [--dark|--light] > demo.html """ from __future__ import annotations import base64 import html import json import re import sys from pathlib import Path from typing import Any HERE = Path(__file__).resolve().parent EX_DIR = HERE / "examples" EX_JSON = EX_DIR / "examples.json" STATIC = HERE / "static" _CACHE: dict | None = None def load_examples() -> dict: """Parsed examples.json (cached).""" global _CACHE if _CACHE is None: _CACHE = json.loads(EX_JSON.read_text()) return _CACHE def example_by_id(ex_id: str) -> dict | None: """The example record with this id, or None.""" return next((e for e in load_examples()["examples"] if e["id"] == ex_id), None) def image_path(ex: dict) -> Path | None: """Absolute path of the example's image, if it has one.""" return (EX_DIR / ex["image"]) if ex.get("image") else None def fill_values(ex: dict, registry: list[Any] | None = None) -> dict: """Everything the ask bar needs. `registry` = app.REGISTRY (ModelInfo with .name/.vision).""" # The image goes to the gr.Image component as a FILE PATH, never as a PIL object: gr.Image's # default output format is lossy WebP (gradio 6.30: Image().format == "webp"), and a PIL value # is re-encoded through it before the model sees it. MEASURED: form_signed.png as PIL -> model # read P(true) 0.5510; the precomputed answer on the original bytes is 0.6868. A path is served as-is. img: str | None = None p = image_path(ex) if p is not None: img = str(p) models: list[str] | None = None if registry is not None: if ex.get("vision_only"): models = [m.name for m in registry if getattr(m, "vision", False)] else: models = [m.name for m in registry] return { "id": ex["id"], "state": ex.get("state", ""), "questions": [dict(q) for q in ex["questions"]], "image": img, "models": models, "vision_only": bool(ex.get("vision_only")), } # --------------------------------------------------------------------------------- # rendering # --------------------------------------------------------------------------------- E = html.escape _TYPE_LABEL = {"noul": "yes/no", "choice": "choice", "score": "score"} def _thumb(ex: dict) -> str: """Inline 96 px thumbnail of the example image (data URI), or an empty string.""" p = image_path(ex) if p is None or not p.exists(): return "" b64 = base64.b64encode(p.read_bytes()).decode("ascii") return (f'{E(ex[') def _fmt(v: Any, nd: int = 3) -> str: """Fixed-point text; non-numbers are escaped as-is.""" try: return f"{float(v):.{nd}f}" except (TypeError, ValueError): return E(str(v)) def _answer_html(qid: str, a: dict, q: dict | None) -> str: """One measured answer -> value line + mini distribution. Numbers are read, never computed.""" t = a.get("type") or (q or {}).get("type") or "" conf = a.get("confidence") probs = a.get("probs") or {} display = a.get("display", a.get("value")) if t == "noul": p = probs.get("true", probs.get("yes")) if p is None: try: p = float(display) except (TypeError, ValueError): p = None pct = f"{p * 100:.1f}" if isinstance(p, float) else "0" head = f'P(true){_fmt(p, 4) if p is not None else E(str(display))}' bar = (f'' if p is not None else "") return f'
{head}
{bar}
' if t == "choice": head = (f'→{E(str(display))}' + (f'confidence {_fmt(conf)}' if conf is not None else "")) rows = "".join( f'
{E(str(k))}' f'{_fmt(v)}
' for k, v in probs.items() ) return f'
{head}
{rows}
' if t == "score": head = (f'score{E(str(display))}' + (f'confidence {_fmt(conf)}' if conf is not None else "")) rows = "".join( f'
{i} {E(str(k))}' f'{_fmt(v)}
' for i, (k, v) in enumerate(probs.items()) ) return f'
{head}
{rows}
' return f'
{E(json.dumps(a))}
' def _card(ex: dict) -> str: """One gallery card: title, type chips, request, the measured answer (if any), what it teaches.""" qs = ex["questions"] chips = "".join(f'{E(_TYPE_LABEL.get(q["type"], q["type"]))}' for q in qs) if ex.get("golden"): chips += 'golden' if ex.get("vision_only"): chips += 'Qwen3.5 models only' if len(qs) > 1: chips += f'{len(qs)} questions' req = [] if ex.get("state"): req.append(f'
state
{E(ex["state"])}
') elif ex.get("image"): req.append('
state
(empty — the image is the state)
') for q in qs: opts = q.get("options") or [] req.append( f'
{E(_TYPE_LABEL.get(q["type"], q["type"]))}' f'{E(q["instructions"])}' + (f'{" · ".join(E(str(o)) for o in opts)}' if opts else "") + "
" ) m = ex.get("measured") or {} if m.get("answers"): qmap = {} for i, q in enumerate(qs): qmap[f'{q["type"]}_{sum(1 for p in qs[:i] if p["type"] == q["type"])}'] = q ans = "".join(_answer_html(qid, a, qmap.get(qid)) for qid, a in m["answers"].items()) # Caption: model + date + token count. Latency and device are left out on purpose: they # describe the machine the recording ran on, not the example. parts = ["measured", str(m.get("model") or "").replace("strands-decider-", "") or "decider"] day = re.search(r"\d{4}-\d{2}-\d{2}", str(m.get("label", ""))) if day: parts.append(day.group(0)) if m.get("input_tokens") is not None: parts.append(f'{m["input_tokens"]} input tokens') title = f'recorded through app.answer() on {m.get("model", "")}' measured = (f'
{E(" · ".join(parts))}' f'
{ans}
') elif m.get("error"): measured = f'
not measured
{E(m["error"])}
' else: measured = '
not yet measured
' thumb = _thumb(ex) headline = _headline_html(m, qs) return ( f'
' f'
{thumb}

{E(ex["title"])}

' f'
{chips}
' f'

{E(ex["shows"])}

' f'{headline}' f'
details' f'
' f'
{"".join(req)}
' f'{measured}' f'

teaches{E(ex["teaches"])}

' f'
' f'
run
' f'
' ) def _headline_html(m: dict, qs: list[dict]) -> str: """The one number a skimming visitor needs: the first measured answer as a single line. noul -> P(true) 0.8581 · choice -> → billing · confidence 0.659 · score -> score 1.03 · confidence 0.509. Unmeasured examples get the dim 'not yet measured' line instead. Numbers are read, never computed. """ answers = (m or {}).get("answers") or {} if not answers: return '
not yet measured — click to run it
' qid, a = next(iter(answers.items())) t = a.get("type") or (qs[0].get("type") if qs else "") conf = a.get("confidence") probs = a.get("probs") or {} display = a.get("display", a.get("value")) more = f' +{len(answers) - 1} more' if len(answers) > 1 else "" if t == "noul": p = probs.get("true", probs.get("yes")) if p is None: try: p = float(display) except (TypeError, ValueError): p = None pct = f"{p * 100:.1f}" if isinstance(p, float) else "0" bar = f'' if p is not None else "" return (f'
P(true){_fmt(p, 4) if p is not None else E(str(display))}' f'{bar}{more}
') k = "→" if t == "choice" else "score" c = f'confidence {_fmt(conf)}' if conf is not None else "" return f'
{k}{E(str(display))}{c}{more}
' def gallery_html() -> str: """The whole gallery grouped by family, as server-rendered HTML.""" data = load_examples() by_fam: dict[str, list[dict]] = {} for ex in data["examples"]: by_fam.setdefault(ex["family"], []).append(ex) n = len(data["examples"]) out = [f'") return "".join(out) def _read(name: str) -> str: """Text of a file under static/, empty if missing.""" p = STATIC / name return p.read_text() if p.exists() else "" def css() -> str: """static/examples.css for app.py's launch(css=...).""" return _read("examples.css") def js() -> str: """static/examples.js for app.py's launch(js=...).""" return _read("examples.js") # --------------------------------------------------------------------------------- # standalone preview # --------------------------------------------------------------------------------- def demo_html(scheme: str = "auto") -> str: """Standalone HTML of the gallery (scheme = auto | dark | light).""" tokens = _read("tokens.css") cls = {"dark": ' class="dark"', "light": "", "auto": ""}[scheme] return ( "examples — strands decider playground" '' f'" f'
{gallery_html()}
' f'' ) if __name__ == "__main__": if "--demo" in sys.argv: scheme = "dark" if "--dark" in sys.argv else ("light" if "--light" in sys.argv else "auto") sys.stdout.write(demo_html(scheme)) else: d = load_examples() for e in d["examples"]: m = e.get("measured") or {} flag = "measured" if m.get("answers") else ("ERROR" if m.get("error") else "unmeasured") print(f'{e["id"]:22s} {e["family"]:8s} {len(e["questions"])}q {"img " if e.get("image") else " "}{flag}')