// Decima Playground — static version. The model runs in this browser tab (ONNX Runtime Web, WebAssembly); // the page talks to it through a Web Worker (worker.js) so the interface stays responsive. // Where the int8 export lives: encoder.onnx, scorer.onnx, decima.json, tokenizer/{tokenizer.json, tokenizer_config.json}. // Override for local testing with ?model=. const MODEL_URL = "https://huggingface.co/amyrmahdy/decima-small/resolve/main/onnx/int8/"; import { detectLang } from "./decima.js"; const $ = (s, el = document) => el.querySelector(s); const $$ = (s, el = document) => [...el.querySelectorAll(s)]; const params = new URLSearchParams(location.search); const modelUrl = params.get("model") || MODEL_URL; // ───────────────────────────── the model, in a worker ───────────────────────────── const worker = new Worker(new URL("./worker.js", import.meta.url), { type: "module" }); let seq = 0; const pending = new Map(); worker.onmessage = ({ data }) => { const p = pending.get(data.id); if (!p) return; if (data.progress) return p.onProgress?.(data.progress); pending.delete(data.id); data.error ? p.reject(new Error(data.error)) : p.resolve(data.result); }; worker.onerror = (e) => { for (const p of pending.values()) p.reject(new Error(e.message || "worker failed")); pending.clear(); }; const call = (op, args = {}, onProgress) => new Promise((resolve, reject) => { const id = ++seq; pending.set(id, { resolve, reject, onProgress }); worker.postMessage({ id, op, ...args }); }); const mb = (b) => (b / 1e6).toFixed(1); let loadStats = null; let fromCache = false; const tLoad0 = performance.now(); const ready = call("load", { url: modelUrl }, (p) => { fromCache = p.cached; const bar = $("#load-bar"), txt = $("#load-text"), num = $("#load-num"); if (p.cached || (p.total && p.loaded >= p.total)) { bar.classList.add("is-indeterminate"); bar.firstElementChild.style.width = ""; bar.removeAttribute("aria-valuenow"); } if (p.cached) { txt.innerHTML = "Loading the model from this device — it was saved on your first visit."; num.textContent = `${mb(p.total)} MB`; return; } const frac = p.total ? Math.min(1, p.loaded / p.total) : 0; if (frac >= 1) { txt.innerHTML = "Preparing the model — compiling it for this browser."; num.textContent = `${mb(p.total)} MB`; return; } bar.classList.remove("is-indeterminate"); bar.firstElementChild.style.width = `${(frac * 100).toFixed(1)}%`; bar.setAttribute("aria-valuenow", Math.round(frac * 100)); txt.innerHTML = "Downloading the model — once; after that it is cached on this device."; num.textContent = `${mb(p.loaded)} / ${mb(p.total)} MB · ${Math.round(frac * 100)}%`; }).then((s) => { loadStats = { ...s, fromCache, total_ms: performance.now() - tLoad0 }; const L = $("#loader"); L.classList.add("is-ready"); const secs = (loadStats.total_ms / 1000).toFixed(1); $("#load-text").innerHTML = fromCache ? `Model ready in ${secs} s · loaded from this device, running in this tab` : `Model ready in ${secs} s · downloaded ${mb(s.bytes)} MB once, now cached on this device`; $("#load-num").textContent = s.threads > 1 ? `${s.threads} threads` : ""; $("#load-foot").textContent = `This visit: model ready in ${(loadStats.total_ms / 1000).toFixed(1)} s.`; return s; }).catch((e) => { const L = $("#loader"); L.classList.add("is-error"); $("#load-bar").hidden = true; $("#load-text").innerHTML = `The model could not be loaded. ${esc(e.message)} — try reloading, or a recent Chrome, Edge, Firefox or Safari.`; throw e; }); ready.catch(() => {}); async function decide(state, q) { await ready; return call("decide", { state, q }); } // ───────────────────────────── data ───────────────────────────── const TEAMS = ["billing", "technical support", "sales", "account security"]; const TEAM_Q = "Which team should handle this request?"; const FEATURED = ["route-fa-informal", "route-ar", "route-zh", "route-ru", "route-es", "route-de", "clinc-reminder", "massive-fa", "urgency", "rank", "verify", "news"]; const MAX_OPTIONS = 200; const X86_SCALING = { 4: 16, 20: 26, 77: 75, 150: 130 }; // docs/BENCH-x86.md, short labels, p50 ms, one P-core const LANG_NAME = { auto: "auto", latn: "Latin script", en: "English", fa: "Persian", ar: "Arabic", ru: "Russian", zh: "Chinese", es: "Spanish", de: "German", fr: "French", tr: "Turkish", hi: "Hindi", ja: "Japanese", ko: "Korean", ur: "Urdu", xx: "other", el: "Greek", th: "Thai" }; const DOMAIN_NAME = { ecommerce: "E-commerce", legal_contract: "Legal · contracts", compliance_privacy: "Privacy & compliance", it_ops: "IT operations", product_feedback: "Product feedback", rag_relevance: "Search relevance", invoice_ap: "Accounts payable", procurement: "Procurement", fraud: "Fraud review", sales_crm: "Sales CRM", insurance: "Insurance", real_estate: "Real estate", code_review: "Code review", it_helpdesk: "IT helpdesk", email_ops: "Email operations", travel_expense: "Travel & expenses", banking: "Banking", support_triage: "Support triage", citation_support: "Citation check", logistics: "Logistics", hr: "HR", data_quality: "Data quality", public_services: "Public services", education: "Education" }; const LN = { en: "English", fa: "Persian", ar: "Arabic", ru: "Russian" }; const getJSON = (p) => fetch(p).then((r) => r.json()); const getLines = (p) => fetch(p).then((r) => r.text()).then((t) => { const l = t.split(/\r\n|\n|\r/); if (l.at(-1) === "") l.pop(); return l; }); const dataReady = (async () => { const [gallery, business, tickets, clinc, massive, banking] = await Promise.all([ getJSON("examples/gallery.json"), getJSON("examples/business_cases.json"), getJSON("examples/tickets.json"), getLines("examples/clinc150_intents.txt"), getLines("examples/massive60_intents.txt"), getLines("examples/banking77_intents.txt")]); const labels = { "clinc150_intents.txt": clinc, "massive60_intents.txt": massive, "banking77_intents.txt": banking }; for (const g of gallery.gallery) if (g.choices == null) g.choices = labels[g.choices_file]; const byId = Object.fromEntries(gallery.gallery.map((g) => [g.id, g])); return { gallery, business, tickets, labels, byId, cards: FEATURED.map((id) => byId[id]).filter(Boolean), speedSets: { 4: TEAMS, 20: clinc.slice(0, 20), 77: banking, 150: clinc } }; })(); // ───────────────────────────── HTML builders (ported from app.py) ───────────────────────────── function esc(s) { return String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); } function pct(p) { if (p >= 0.9995) return p < 1 ? "99.9%+" : "100%"; return p >= 0.001 ? `${(p * 100).toFixed(1)}%` : "<0.1%"; } const argmax = (p) => p.reduce((b, v, i) => (v > p[b] ? i : b), 0); const top = (r) => r.choices[argmax(r.probs)]; const confidence = (r) => Math.max(...r.probs); function confBadge(p, kind) { if (kind === "rank") return 'independent scores'; const [c, t] = p >= 0.8 ? ["high", "High"] : p >= 0.5 ? ["mid", "Medium"] : ["low", "Low"]; return `${t} confidence · ${pct(p)}`; } function scaleHtml() { const ticks = [0, 25, 50, 75, 100].map((t) => `${t}`).join(""); return ``; } function row(c, p, isTop) { const w = Math.max(p * 100, 0.35); return `
${esc(c)}
` + `
` + `
${pct(p)}
`; } function rowsHtml(pairs, topC, limit) { const out = []; for (let i = 0; i < pairs.length; i++) { const [c, p] = pairs[i]; if (limit && i === limit) { const tail = pairs.slice(limit).map(([c2, p2]) => row(c2, p2, false)).join(""); out.push(`
Show the other ${pairs.length - limit} options (all ≤ ${pct(pairs[limit][1])})${tail}
`); break; } out.push(row(c, p, c === topC)); } return out.join(""); } function resultHtml(r, kind, lang, detected) { let pairs = r.choices.map((c, i) => [c, r.probs[i]]); if (kind !== "score") pairs = pairs.sort((a, b) => b[1] - a[1]); const n = pairs.length, t = top(r); const orderNote = { score: "levels in the order you gave them (low → high)", rank: "each option scored on its own — they need not add up to 100%" }[kind] || "sorted by probability"; const langNote = `${LANG_NAME[lang] || lang}${detected ? " (detected)" : ""}`; const coldNote = r.cold ? ' — includes encoding this new option set once; it is cached now, run it again to see the steady-state time' : ""; return `
Decision · ${esc(kind)} · ${n} option${n !== 1 ? "s" : ""} · ${esc(langNote)}
${esc(t)}
${confBadge(confidence(r), kind)} decided in ${Math.round(r.latency_ms)} ms in your browser${coldNote}
${scaleHtml()}${rowsHtml(pairs, t, n > 15 ? 12 : 0)}
${esc(orderNote)}
`; } const messageHtml = (title, body) => `
${esc(title)}

${esc(body)}

`; const busyHtml = (text) => `
${esc(text)}
`; const waitText = () => (loadStats ? "Deciding…" : "Waiting for the model to finish loading…"); // ───────────────────────────── tab 1 · try it ───────────────────────────── let tryToken = 0; async function decideUi() { const state = $("#in-state").value.trim(), question = $("#in-question").value.trim(); const kind = $('input[name="kind"]:checked').value, langSel = $("#in-lang").value; const choices = [...new Set($("#in-options").value.split(/\r\n|\n|\r/).map((c) => c.trim()).filter(Boolean))]; const out = $("#out"); if (!state) return (out.innerHTML = messageHtml("Missing situation", "Describe the situation: a message, a ticket, a document excerpt.")); if (!question) return (out.innerHTML = messageHtml("Missing question", "Ask a question about the situation, e.g. “Which team should handle this?”.")); if (choices.length < 2) return (out.innerHTML = messageHtml("Need options", "Give at least two options, one per line.")); if (choices.length > MAX_OPTIONS) return (out.innerHTML = messageHtml("Too many options", `This demo takes up to ${MAX_OPTIONS} options (cost grows with every option).`)); if (kind === "verify" && choices.length !== 2) return (out.innerHTML = messageHtml("verify takes exactly two options", "Use two options such as “yes” and “no”, or switch to choose.")); const detected = langSel === "auto"; const lang = detected ? detectLang(state) : langSel; const token = ++tryToken; const go = $("#go"); go.setAttribute("aria-busy", "true"); if (!out.querySelector(".dz-result") || !loadStats) out.innerHTML = busyHtml(waitText()); try { const r = await decide(state, { text: question, choices, kind, lang }); if (token === tryToken) out.innerHTML = resultHtml(r, kind, lang, detected); } catch (e) { if (token === tryToken) out.innerHTML = messageHtml("Could not decide", e.message); } finally { if (token === tryToken) go.removeAttribute("aria-busy"); } } function galleryHtml(cards) { return cards.map((g) => { const rec = g.recorded; return ` `; }).join(""); } function loadCard(g) { $("#in-state").value = g.state; $("#in-question").value = g.question; $("#in-options").value = g.choices.join("\n"); $(`input[name="kind"][value="${g.kind}"]`).checked = true; $("#in-lang").value = g.lang; $$(".dz-ex").forEach((c) => c.classList.toggle("is-active", c.dataset.id === g.id)); const out = $("#try-form"); if (out.getBoundingClientRect().top < 0) out.scrollIntoView({ behavior: matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth", block: "start" }); return decideUi(); } // ───────────────────────────── tab 2 · business cases ───────────────────────────── function caseLabel(c) { const dom = DOMAIN_NAME[c.domain] || c.domain.replace(/_/g, " ").replace(/\b\w/g, (m) => m.toUpperCase()); const langs = LN[c.state_lang] + (c.choice_lang === c.state_lang ? "" : ` → ${LN[c.choice_lang]} options`); return [dom, langs]; } function casesHtml(cases) { return cases.map((c, i) => { const [dom, langs] = caseLabel(c); return ` `; }).join(""); } const lines = (text, cls = "") => text.split("\n").map((l) => `
${esc(l) || " "}
`).join(""); function docHtml(state, cut) { if (!cut || cut >= state.length) return lines(state); return lines(state.slice(0, cut)) + '' + lines(state.slice(cut), "dz-unread"); } function questionHtml(q, k, dec) { const td = argmax(q.teacher), dd = dec ? argmax(dec) : -1; const idx = q.choices.map((_, j) => j); const order = q.kind === "score" ? idx : idx.sort((a, b) => q.teacher[b] - q.teacher[a]); const rows = order.map((j) => { const pt = q.teacher[j], pd = dec ? dec[j] : 0; return `
${esc(q.choices[j])}
${dec ? `
` : ""}
${dec ? pct(pd) : "…"}${pct(pt)}
`; }).join(""); const mark = !dec ? 'Decima is reading…' : dd === td ? '✓ same answer' : '✗ different answer'; const kindNote = q.kind === "score" ? " · levels in order" : ""; return `
Q${k + 1}${q.kind}${kindNote}${mark}
${esc(q.question)}
${rows}
`; } function caseDetailHtml(c) { const [dom, langs] = caseLabel(c); return `
${esc(dom)} · ${esc(langs)} · ${esc(c.format)}
${docHtml(c.state, c.window_end_char)}
Decima-small · 122M · int8, live in your browser Teacher · Gemma-4-26B-A4B (recorded) rows sorted by the teacher; bold = Decima’s pick
${c.questions.map((q, k) => questionHtml(q, k, null)).join("")}
`; } let caseToken = 0; const caseResults = new Map(); // "case:question" → live Decima probabilities async function showCase(D, i) { const c = D.business.cases[i]; const token = ++caseToken; $$(".dz-case").forEach((b) => b.classList.toggle("is-active", Number(b.dataset.id) === i)); $("#case-view").innerHTML = caseDetailHtml(c); for (let k = 0; k < c.questions.length; k++) { const q = c.questions[k]; const key = `${i}:${k}`; let probs = caseResults.get(key); if (!probs) { try { probs = (await decide(c.state, { text: q.question, choices: q.choices, kind: q.kind, lang: c.state_lang })).probs; caseResults.set(key, probs); } catch (e) { return; } } if (token !== caseToken) return; $(`.dz-q[data-q="${k}"]`).outerHTML = questionHtml(q, k, probs); } // The card counts were recorded with native ONNX Runtime; show the count from this browser's run instead. // (WebAssembly computes the int8 layers with float activations, so a near-tie can land the other way.) const agree = c.questions.filter((q, k) => argmax(caseResults.get(`${i}:${k}`)) === argmax(q.teacher)).length; const b = $(`.dz-case[data-id="${i}"] .dz-case-agree b`); if (b) b.textContent = `${agree}/${c.questions.length}`; } function businessSummaryHtml(B) { const s = B.shown, p = B.pool; return `

${s.cases} long cases, every question shown: Decima gives the teacher’s answer on ${s.agree} of ${s.questions}. We picked these cases because Decima mostly agrees on them. Across all ${p.cases.toLocaleString("en")} held-out cases it agrees on only ${p.agree.toLocaleString("en")} of ${p.questions.toLocaleString("en")} questions (${Math.round(p.agree / p.questions * 100)}%) — most of those documents are longer than the 512 tokens Decima reads. Long, multi-fact decisions are this model’s weakest area; the teacher, a 26B model, is not always right either.

The model never trained on these cases: they come from rows of the synthetic set that were generated after the training snapshot. ${esc(B.attribution)}

`; } // ───────────────────────────── tab 3 · shuffle ───────────────────────────── const shuf = { preset: null, order: [], tally: { n: 0, flips: 0, max: 0 }, token: 0 }; function orderedLedger(title, choices, probs, topC, permFrom) { const rows = choices.map((c, i) => { const p = probs[c]; let moved = ""; if (permFrom) { const j = permFrom.indexOf(c) + 1; moved = j !== i + 1 ? `was #${j}` : 'same place'; } return `
${i + 1}${esc(c)} ${moved}
${pct(p)}
`; }).join(""); return `
${esc(title)}
${rows}
`; } function fmtDelta(x) { if (x === 0) return "0 (identical)"; const [m, e] = x.toExponential(1).split("e"); return `${m} × 10${Number(e)}`; } const asDict = (r) => Object.fromEntries(r.choices.map((c, i) => [c, r.probs[i]])); const same = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]); async function shuffleRender(p, state, order, bump) { const token = ++shuf.token; const out = $("#shuf-out"); if (!out.firstElementChild || !loadStats) out.innerHTML = busyHtml(waitText()); const q = { text: p.question, kind: p.kind, lang: p.lang }; let r0, r1; try { r0 = await decide(state, { ...q, choices: p.choices }); r1 = await decide(state, { ...q, choices: order }); } catch (e) { out.innerHTML = messageHtml("Could not decide", e.message); return; } if (token !== shuf.token) return; const a = asDict(r0), b = asDict(r1); const delta = Math.max(...p.choices.map((c) => Math.abs(a[c] - b[c]))); const t0 = top(r0), t1 = top(r1), changed = t0 !== t1; if (bump) shuf.tally = { n: shuf.tally.n + 1, flips: shuf.tally.flips + (changed ? 1 : 0), max: Math.max(shuf.tally.max, delta) }; const t = shuf.tally; out.innerHTML = `
${orderedLedger("Options as listed", p.choices, a, t0)}${orderedLedger("Same options, shuffled", order, b, t1, p.choices)}
Answer
${changed ? "changed" : "unchanged"} — ${esc(t1)}
Largest probability change
${fmtDelta(delta)}
This session
${t.n} shuffle${t.n !== 1 ? "s" : ""} · ${t.flips} answer change${t.flips !== 1 ? "s" : ""}
largest change seen: ${fmtDelta(t.max)}
`; } function shuffleReset(D) { const p = D.gallery.shuffle[Number($("#shuf-preset").value)]; shuf.preset = p; shuf.order = [...p.choices]; shuf.tally = { n: 0, flips: 0, max: 0 }; $("#shuf-state").value = p.state; return shuffleRender(p, p.state, shuf.order, false); } function shuffleClick() { const p = shuf.preset; const state = $("#shuf-state").value.trim() || p.state; let next = [...p.choices]; const shuffleArr = (a) => { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } }; if (next.length > 1) while (same(next, p.choices) || same(next, shuf.order)) shuffleArr(next); shuf.order = next; return shuffleRender(p, state, next, true); } // ───────────────────────────── charts (inline SVG) ───────────────────────────── const tip = document.createElement("div"); tip.className = "dz-tip"; tip.hidden = true; tip.setAttribute("role", "tooltip"); document.body.appendChild(tip); function bindTips(svg) { svg.addEventListener("pointermove", (e) => { const t = e.target.closest("[data-tip]"); if (!t) { tip.hidden = true; return; } tip.innerHTML = t.dataset.tip; tip.hidden = false; const w = tip.offsetWidth, h = tip.offsetHeight; tip.style.left = `${Math.min(innerWidth - w - 8, e.clientX + 12)}px`; tip.style.top = `${Math.max(8, e.clientY - h - 12)}px`; }); svg.addEventListener("pointerleave", () => (tip.hidden = true)); } function routeChart(rs, th, width) { const W = Math.max(300, width), H = 250, L = 14, R = 14, T = 14, B = 44; const x = (v) => L + ((Math.max(0.3, Math.min(1, v)) - 0.3) / 0.7) * (W - L - R); const y = (v) => T + ((0.62 - v) / 1.24) * (H - T - B); const ticks = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]; let g = ticks.map((t) => `${t.toFixed(1)}`).join(""); g += ``; g += `escalate to a person`; g += `route automatically`; g += ``; const anchorEnd = th > 0.85; g += `threshold ${th.toFixed(2)}`; // spread marks over five rows by confidence rank, so neighbours on the x axis never share a row const rank = new Map([...rs].sort((a, b) => a.conf - b.conf).map((r, i) => [r, i])); rs.forEach((r) => { const cx = x(r.conf), cy = y(((rank.get(r) % 5) - 2) * 0.16); const tipText = `${esc(r.text.slice(0, 70))}${r.text.length > 70 ? "…" : ""}
→ ${esc(r.top)} (${r.conf.toFixed(3)})
expected: ${esc(r.gold)}`; g += r.ok ? `` : ``; g += ``; }); g += `model confidence (top probability)`; return `${g}`; } function speedChart(live, width) { const W = Math.max(300, width), H = 300, L = 48, R = 96, T = 14, B = 46; const xs = [4, 20, 77, 150]; const ymax = Math.max(140, ...Object.values(live || {}).map((v) => v * 1.12)); const step = ymax > 600 ? 200 : ymax > 300 ? 100 : 50; const x = (v) => L + (v / 150) * (W - L - R); const y = (v) => T + (1 - v / ymax) * (H - T - B); let g = ""; for (let t = 0; t <= ymax; t += step) g += `${t}`; g += xs.map((v) => `${v}`).join(""); g += `number of options`; g += `ms per decision`; const series = (vals, cls, name) => { const pts = xs.filter((v) => vals[v] != null).map((v) => [x(v), y(vals[v]), v, vals[v]]); let s = ``; for (const [px, py, v, ms] of pts) { s += ``; s += ``; } const last = pts.at(-1); if (last) s += `${Math.round(last[3])} ms`; return s; }; g += series(X86_SCALING, "ref", "x86 laptop core, reference"); if (live) g += series(live, "live", "your browser, live (p50)"); return `${g}`; } // ───────────────────────────── tab 4 · auto-route ───────────────────────────── let routeResults = null; async function scoreTickets(D) { if (routeResults) return routeResults; const out = $("#route-out"); out.innerHTML = busyHtml(loadStats ? `Scoring ${D.tickets.length} tickets in your browser…` : waitText()); await ready; out.innerHTML = busyHtml(`Scoring ${D.tickets.length} tickets in your browser…`); const items = D.tickets.map((t) => ({ state: t.text, q: { text: TEAM_Q, choices: TEAMS, kind: "choose", lang: t.lang } })); const t0 = performance.now(); const rs = await call("decideMany", { items }); const ms = performance.now() - t0; routeResults = D.tickets.map((t, i) => ({ ...t, top: top(rs[i]), conf: confidence(rs[i]), ok: top(rs[i]) === t.gold, probs: rs[i].probs })); routeResults.ms = ms; return routeResults; } function routeUpdate() { const rs = routeResults; if (!rs) return; const th = Number($("#th").value); $("#th-val").textContent = th.toFixed(2); const auto = rs.filter((r) => r.conf >= th); const right = auto.filter((r) => r.ok).length; const escN = rs.length - auto.length; const caught = rs.filter((r) => !r.ok && r.conf < th).length; const wrong = rs.filter((r) => !r.ok).length; const tiles = `
${auto.length}/${rs.length}
routed automatically
${right}/${auto.length}
of those went to the right team
${escN}
escalated to a person — ${caught} of the ${wrong} wrong answer${wrong !== 1 ? "s" : ""} among them
`; const rows = [...rs].sort((a, b) => b.conf - a.conf).map((r) => { const a = r.conf >= th; return `${a ? "auto" : "escalate"} ${r.conf.toFixed(3)}${esc(r.text)}${esc(r.lang)}${esc(r.top)} ${r.ok ? '✓' : `✗ expected ${esc(r.gold)}`}`; }).join(""); const out = $("#route-out"); const width = out.clientWidth - 26; out.innerHTML = `${tiles}
right teamwrong team scored in your browser in ${(rs.ms / 1000).toFixed(1)} s · hover a mark for the ticket
${routeChart(rs, th, width)}
${rows}
decisionconfidenceticketlang model’s teamright?
`; bindTips($("#route-out svg")); } // ───────────────────────────── tab 5 · speed ───────────────────────────── let speedLive = null; function drawSpeed() { const el = $("#sp-plot"); const legend = `
x86 laptop core, reference (BENCH-x86)${speedLive ? 'your browser, live (p50)' : ""}
`; el.innerHTML = legend + speedChart(speedLive, el.clientWidth - 26); bindTips($("svg", el)); } async function speedRun(D) { const btn = $("#sp-go"); if (btn.getAttribute("aria-busy")) return; btn.setAttribute("aria-busy", "true"); const reps = 12; const rows = []; const live = {}; const table = $("#sp-table"); try { table.innerHTML = busyHtml(waitText()); await ready; for (const n of [4, 20, 77, 150]) { table.innerHTML = busyHtml(`Timing ${n} options… (${reps + 1} decisions)`); const q = { text: "What is the customer asking about?", choices: D.speedSets[n], kind: "choose", lang: "en" }; const { first, lat } = await call("bench", { state: "I was charged twice for my subscription this month.", q, reps }); lat.sort((a, b) => a - b); const p50 = lat.length % 2 ? lat[(lat.length - 1) / 2] : (lat[lat.length / 2 - 1] + lat[lat.length / 2]) / 2; const p95 = lat[Math.min(lat.length - 1, Math.round(0.95 * (lat.length - 1)))]; live[n] = p50; speedLive = { ...live }; drawSpeed(); rows.push(`${n}${p50.toFixed(1)}${p95.toFixed(1)}` + `${X86_SCALING[n]}${Math.round(first)}`); } const ua = navigator.userAgentData?.brands?.filter((b) => !/Not.A.Brand/i.test(b.brand)).map((b) => b.brand).at(-1) || ""; table.innerHTML = `
${rows.join("")}
optionsp50 ms · your browserp95 ms · your browser p50 ms · x86 referencefirst call ms · your browser

Live: this tab${ua ? ` (${esc(ua)})` : ""}, ${navigator.hardwareConcurrency || "?"} logical cores reported, ${loadStats.threads} WebAssembly thread${loadStats.threads > 1 ? "s" : ""}, int8 ONNX on ONNX Runtime Web, batch 1, ${reps} timed calls per size after the first, short state. The first call also encodes the option set (once per set). Background tabs and battery saving slow it down — run it twice. Reference: Intel Core Ultra 7 155H, one P-core, native ONNX Runtime, 40 calls per size (docs/BENCH-x86.md).

`; } catch (e) { table.innerHTML = messageHtml("The sweep stopped", e.message); } finally { btn.removeAttribute("aria-busy"); } } // ───────────────────────────── page wiring ───────────────────────────── const TABS = ["try", "cases", "shuffle", "route", "speed"]; const opened = new Set(); function selectTab(name, D, focus = false) { if (!TABS.includes(name)) name = "try"; for (const t of TABS) { const on = t === name; const b = $(`#tab-${t}`); b.setAttribute("aria-selected", on); b.tabIndex = on ? 0 : -1; $(`#p-${t}`).hidden = !on; if (on && focus) b.focus(); } if (location.hash.slice(1) !== name) history.replaceState(null, "", name === "try" ? location.pathname + location.search : `#${name}`); if (opened.has(name)) { if (name === "route") routeUpdate(); if (name === "speed") drawSpeed(); return; } opened.add(name); if (name === "cases") showCase(D, 0); if (name === "shuffle") shuffleReset(D); if (name === "route") scoreTickets(D).then(routeUpdate).catch((e) => ($("#route-out").innerHTML = messageHtml("Could not score the tickets", e.message))); if (name === "speed") drawSpeed(); } function initTheme() { const root = document.documentElement; $("#theme").addEventListener("click", () => { const dark = !root.classList.contains("dark"); root.classList.toggle("dark", dark); try { localStorage.setItem("dz-theme", dark ? "dark" : "light"); } catch {} }); matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => { let saved = null; try { saved = localStorage.getItem("dz-theme"); } catch {} if (!saved) root.classList.toggle("dark", e.matches); }); } async function main() { initTheme(); $("#out").innerHTML = busyHtml("Waiting for the model to finish loading…"); const D = await dataReady; // try it $("#gallery").innerHTML = galleryHtml(D.cards); $("#gallery").addEventListener("click", (e) => { const card = e.target.closest("[data-id]"); if (card) loadCard(D.byId[card.dataset.id]); }); $("#try-form").addEventListener("submit", (e) => { e.preventDefault(); decideUi(); }); $("#in-state").addEventListener("keydown", (e) => { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); decideUi(); } }); // business cases $("#cases-summary").innerHTML = businessSummaryHtml(D.business); $("#case-list").innerHTML = casesHtml(D.business.cases); $("#case-list").addEventListener("click", (e) => { const b = e.target.closest("[data-id]"); if (b) showCase(D, Number(b.dataset.id)); }); // shuffle $("#shuf-preset").innerHTML = D.gallery.shuffle.map((p, i) => ``).join(""); $("#shuf-preset").addEventListener("change", () => shuffleReset(D)); $("#shuf-go").addEventListener("click", () => shuffleClick()); $("#shuf-state").addEventListener("keydown", (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); shuffleRender(shuf.preset, $("#shuf-state").value.trim() || shuf.preset.state, shuf.order, false); } }); // auto-route + speed $("#th").addEventListener("input", routeUpdate); $("#sp-go").addEventListener("click", () => speedRun(D)); let rt; addEventListener("resize", () => { clearTimeout(rt); rt = setTimeout(() => { if (!$("#p-route").hidden) routeUpdate(); if (!$("#p-speed").hidden) drawSpeed(); }, 150); }); // tabs $$(".dz-tab").forEach((b, i) => { b.addEventListener("click", () => selectTab(TABS[i], D)); b.addEventListener("keydown", (e) => { const d = { ArrowRight: 1, ArrowLeft: -1 }[e.key]; if (d) { e.preventDefault(); selectTab(TABS[(i + d + TABS.length) % TABS.length], D, true); } }); }); addEventListener("hashchange", () => selectTab(location.hash.slice(1), D)); selectTab(location.hash.slice(1), D); $("#forget").addEventListener("click", async () => { const ok = await call("clearCache").catch(() => false); $("#forget").textContent = ok ? "Removed — the next visit downloads it again." : "Nothing cached on this device."; }); // first decision, like the Gradio version's on-load run ready.then(() => decideUi(), () => ($("#out").innerHTML = messageHtml("The model could not be loaded", "See the message at the top of the page."))); } // hook for automated checks (parity tests, screenshots) window.DZ = { ready, decide: (state, q) => decide(state, q), data: dataReady, stats: () => loadStats, call }; main();