Download explore/app.js from Babu09/Mat2Bot: direct link, hf CLI and curl.
- Browser
- Download file 15.6 kB
-
https://huggingface.co/spaces/Babu09/Mat2Bot/resolve/main/explore/app.js
- Command line
-
hf download hf://spaces/Babu09/Mat2Bot/explore/app.js
-
curl -L -o app.js https://huggingface.co/spaces/Babu09/Mat2Bot/resolve/main/explore/app.js
15.6 kB
| // The explorer's page logic: the seven-step walkthrough, the test cases, the honesty chart and the glossary, all | |
| // filled from data/site.json, which was computed by Mat2Bot when this site was built. | |
| import { Beaker, CrystalView, reducedMotion, webglOK } from "./viewer.js"; | |
| const $ = (s, el = document) => el.querySelector(s); | |
| const $$ = (s, el = document) => [...el.querySelectorAll(s)]; | |
| const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]); | |
| const KEY = { strong: "k-strong", partial: "k-partial", hypo: "k-hypo", blocked: "k-blocked", none: "k-none" }; | |
| const MARK = { eliminated: "✓", open: "?", sustained: "!" }; | |
| const WHO = { robot: "Robot", person: "A person", outside: "Outside lab", missing: "Missing equipment" }; | |
| const ICON = { | |
| robot: '<svg viewBox="0 0 24 24"><rect x="5" y="8" width="14" height="10" rx="2"/><path d="M12 4v4M9.5 13h.01M14.5 13h.01M3 13h2M19 13h2"/></svg>', | |
| person: '<svg viewBox="0 0 24 24"><circle cx="12" cy="7" r="3.5"/><path d="M5 20c1-4 4-6 7-6s6 2 7 6"/></svg>', | |
| outside: '<svg viewBox="0 0 24 24"><path d="M4 20V9l8-5 8 5v11"/><path d="M9 20v-6h6v6"/></svg>', | |
| missing: '<svg viewBox="0 0 24 24"><circle cx="12" cy="12" r="8"/><path d="M8 8l8 8"/></svg>', | |
| }; | |
| const AUDIT = { | |
| crate: "Files intact, nothing added", documents: "Records well formed", safety: "Safety verdict anchored", evidence: "Sources read again", | |
| sources: "Every source accounted for", parameters: "Conditions match their sources", science: "Chemistry recomputed", | |
| categories: "Certainty worked out again", execution: "Lab verdicts worked out again", rules: "Every rule on the list", | |
| explanations: "Every sentence backed by the record", report: "Report matches the record", amounts: "Amounts recomputed", | |
| procedures: "Recipe values checked", ledger: "Tamper-evident log unbroken", summary: "Summary matches", outcome: "Open doubts left open", | |
| authorization: "Nothing started without a person", | |
| }; | |
| let site; | |
| try { | |
| site = await (await fetch("data/site.json")).json(); | |
| } catch (err) { | |
| document.body.insertAdjacentHTML("afterbegin", '<p class="noscript">The explorer could not load its data. Please reload the page.</p>'); | |
| throw err; | |
| } | |
| const gl = webglOK(); | |
| function fallback(canvas, crystal, text) { | |
| const h = canvas.clientHeight; | |
| const div = document.createElement("div"); | |
| div.className = "fallback"; | |
| div.style.minHeight = `${Math.max(h, 220)}px`; | |
| div.innerHTML = crystal ? `<img src="data/thumb-${crystal}.svg" alt="">` : `<p>${esc(text)}</p>`; | |
| canvas.replaceWith(div); | |
| } | |
| function view(canvas, crystal, opts) { | |
| if (!gl) { fallback(canvas, crystal); return null; } | |
| try { | |
| const v = new CrystalView(canvas, opts); | |
| v.show(crystal); | |
| return v; | |
| } catch { | |
| fallback(canvas, crystal); | |
| return null; | |
| } | |
| } | |
| // ---------------------------------------------------------------- the hero | |
| view($("#hero-canvas"), "zincite", { speed: 0.16, zoom: 1.08 }); | |
| // ---------------------------------------------------------------- the seven steps | |
| const caseById = Object.fromEntries(site.cases.map((c) => [c.id, c])); | |
| const calcite = caseById.b2_caco3_calcite; | |
| const order = { eliminated: 0, open: 1, sustained: 2 }; | |
| const shown = [...calcite.doubts].sort((a, b) => order[a.status] - order[b.status]); | |
| const pick = [...shown.filter((d) => d.status === "eliminated").slice(0, 3), ...shown.filter((d) => d.status !== "eliminated").slice(0, 3)]; | |
| $("#stage-doubts").innerHTML = pick.map((d, i) => | |
| `<li style="--i:${i}"><span class="mark ${d.status}" aria-hidden="true">${MARK[d.status]}</span><span>${esc(d.text)}<small>${esc(d.words)}</small></span></li>`).join(""); | |
| const robotLab = (caseById.b1c_kcl_antisolvent_vcw001.labs || []).find((l) => l.id === "vialbot"); | |
| $("#stage-recipe").innerHTML = (robotLab?.steps || []).slice(0, 8).map((s, i) => | |
| `<li style="--i:${i}"><span class="who ${s.who}" title="${WHO[s.who]}">${ICON[s.who]}</span><span>${esc(s.step)}<small>${WHO[s.who]}</small></span></li>`).join(""); | |
| const seen = new Map(); | |
| for (const c of calcite.audit.checks) { | |
| const prev = seen.get(c.name); | |
| if (!prev || (prev === "PASS" && c.verdict !== "PASS")) seen.set(c.name, c.verdict); | |
| } | |
| $("#stage-audit").innerHTML = [...seen].filter(([n]) => AUDIT[n]).slice(0, 10).map(([n, v], i) => | |
| `<li style="--i:${i}" class="${v === "PASS" ? "" : "unknown"}">${esc(AUDIT[n])}${v === "PASS" ? "" : v === "NOT_RUN" ? " (not applicable here)" : " (not re-run here)"}</li>`).join(""); | |
| const ANGLE = { blocked: -67.5, hypo: -22.5, partial: 22.5, strong: 67.5 }; | |
| function needle() { | |
| const n = $("#stage-gauge .needle"); | |
| n.style.transform = "rotate(-90deg)"; | |
| requestAnimationFrame(() => requestAnimationFrame(() => { n.style.transform = `rotate(${ANGLE[calcite.verdict.key] ?? 0}deg)`; })); | |
| $("#gauge-verdict").innerHTML = `Test case 4, calcite: <b>${esc(calcite.verdict.words)}</b>. ${esc(calcite.verdict.why)}`; | |
| } | |
| let stageCrystal = null; | |
| let beaker = null; | |
| let stepNow = -1; | |
| const steps = $$(".step"); | |
| const layers = $$(".layer"); | |
| function setStep(i) { | |
| if (i === stepNow) return; | |
| stepNow = i; | |
| steps.forEach((s, k) => s.classList.toggle("on", k === i)); | |
| layers.forEach((l, k) => l.classList.toggle("on", k === i)); | |
| if (i === 0) { | |
| stageCrystal ||= view($("#stage-crystal"), "zincite", { speed: 0.3 }); | |
| stageCrystal?.play(); | |
| } else stageCrystal?.pause(); | |
| if (i === 2) { | |
| if (!beaker && gl) { | |
| try { beaker = new Beaker($("#stage-beaker"), { onTemperature: thermo }); } catch { fallback($("#stage-beaker"), null, "This browser cannot show the 3D beaker."); } | |
| } else if (!gl && $("#stage-beaker")) fallback($("#stage-beaker"), null, "This browser cannot show the 3D beaker."); | |
| beaker?.play(); | |
| } else beaker?.pause(); | |
| if (i === 4) needle(); | |
| } | |
| const watch = new IntersectionObserver((es) => { | |
| for (const e of es) if (e.isIntersecting) setStep(Number(e.target.dataset.step)); | |
| }, { rootMargin: "-42% 0px -42% 0px" }); | |
| steps.forEach((s) => watch.observe(s)); | |
| setStep(0); | |
| const NOTE = { | |
| zincite: "Wurtzite: each zinc atom sits among four oxygen atoms.", | |
| "zno-rocksalt": "Rock salt: each zinc atom sits among six oxygen atoms, about 20 % denser.", | |
| }; | |
| $$("[data-crystal]").forEach((b) => b.addEventListener("click", () => { | |
| $$("[data-crystal]").forEach((x) => x.setAttribute("aria-pressed", String(x === b))); | |
| stageCrystal?.show(b.dataset.crystal); | |
| $("#stage-crystal-caption").textContent = NOTE[b.dataset.crystal]; | |
| })); | |
| $("#stage-crystal-caption").textContent = NOTE.zincite; | |
| function thermo(T) { | |
| $("#thermo-value").textContent = `${Math.round(T)} °C`; | |
| $("#thermo-fill").style.width = `${Math.max(0, Math.min(100, ((T - 15) / 50) * 100))}%`; | |
| $("#beaker-cool").disabled = T < 21; | |
| $("#beaker-heat").disabled = T > 59; | |
| } | |
| $("#beaker-cool").addEventListener("click", () => beaker?.cool()); | |
| $("#beaker-heat").addEventListener("click", () => beaker?.heat()); | |
| thermo(60); | |
| // ---------------------------------------------------------------- the test cases | |
| $("#case-grid").innerHTML = site.cases.map((c, i) => ` | |
| <button type="button" class="tile" data-case="${c.id}" aria-haspopup="dialog"> | |
| <span class="pic"><img src="data/thumb-${c.crystal}.svg" alt="" loading="lazy" width="150" height="150"></span> | |
| <span class="body"><span class="num">Test case ${i + 1}${c.family ? ` · ${esc(c.family)}` : ""}</span> | |
| <span class="t"><b>${esc(c.title)}</b></span><span class="p">${esc(c.what)}</span> | |
| <span class="chip ${KEY[c.verdict.key]}">${esc(c.verdict.words)}</span></span> | |
| </button>`).join(""); | |
| $$(".tile").forEach((t) => t.addEventListener("click", () => openCase(t.dataset.case))); | |
| const dialog = $("#case-dialog"); | |
| let caseView = null; | |
| function plural(n, one, many) { return `${n} ${n === 1 ? one : many}`; } | |
| function caseHTML(c, n) { | |
| const doubts = c.doubts || []; | |
| const notOut = doubts.filter((d) => d.status !== "eliminated"); | |
| const out = doubts.filter((d) => d.status === "eliminated"); | |
| const li = (d) => `<li><span class="mark ${d.status}" aria-hidden="true">${MARK[d.status]}</span><span>${esc(d.text)} <em>(${esc(d.words)})</em></span></li>`; | |
| const labs = (c.labs || []).map((l) => ` | |
| <div class="lab"><b>${esc(l.lab)}</b><span class="chip ${KEY[l.key]}">${esc(l.words)}</span><small>${esc(l.why)}</small> | |
| <details><summary>Who does each step</summary><ol class="recipe light">${l.steps.map((s) => | |
| `<li><span class="who ${s.who}" title="${WHO[s.who]}">${ICON[s.who]}</span><span>${esc(s.step)}<small>${WHO[s.who]}</small></span></li>`).join("")}</ol></details> | |
| </div>`).join(""); | |
| return ` | |
| <p class="eyebrow">Test case ${n}${c.family ? ` · ${esc(c.family)}` : ""}</p> | |
| <h2 id="case-title">${esc(c.title)}</h2> | |
| <p class="what">${esc(c.what)}</p> | |
| <h3>The question</h3><p>${esc(c.question)}</p> | |
| <h3>What Mat2Bot found</h3><p class="story">${esc(c.story)}</p> | |
| <h3>How sure it is</h3><span class="chip ${KEY[c.verdict.key]}">${esc(c.verdict.words)}</span><p class="why">${esc(c.verdict.why)}</p> | |
| ${c.safety && !c.family ? `<h3>Handling it</h3><p class="why">${esc(c.safety)}</p>` : ""} | |
| ${doubts.length ? `<h3>Doubts it checked</h3> | |
| <div class="counts"><span class="chip k-strong">${plural(c.counts.eliminated, "ruled out", "ruled out")}</span> | |
| <span class="chip k-partial">${plural(c.counts.open, "still open", "still open")}</span> | |
| <span class="chip k-blocked">${plural(c.counts.sustained, "confirmed", "confirmed")}</span></div> | |
| <ul class="dlist">${notOut.map(li).join("")}</ul> | |
| ${out.length ? `<details><summary>The ${out.length} doubts it ruled out</summary><ul class="dlist">${out.map(li).join("")}</ul></details>` : ""}` : ""} | |
| ${(c.steps || []).length ? `<h3>The recipe, in short</h3><ol class="steps-list">${c.steps.map((s) => `<li>${esc(s)}</li>`).join("")}</ol>` : ""} | |
| ${labs ? `<h3>Who could make it</h3><div class="labs">${labs}</div>` : ""} | |
| ${(c.sources || []).length ? `<h3>Where the evidence comes from</h3><ul class="sources">${c.sources.map((s) => `<li>${esc(s)}</li>`).join("")}</ul>` : ""} | |
| ${c.links.trace ? `<h3>How Mat2Bot got there</h3><div class="trail" data-trace="${esc(c.links.trace_json)}"><p class="muted">Loading the trail…</p></div> | |
| <p class="why">Each box is a stage Mat2Bot ran, from its own records. Open the full trail to click through every route, doubt, number and source.</p>` : ""} | |
| <h3>Checked by the independent auditor</h3> | |
| <p class="audit-line"><span class="chip ${{ PASS: "k-strong", UNKNOWN: "k-partial", FAIL: "k-blocked" }[c.audit.verdict] || "k-partial"}">${{ PASS: "consistent", UNKNOWN: "partly re-checked", FAIL: "inconsistent" }[c.audit.verdict] || esc(c.audit.verdict)}</span> | |
| ${plural(c.audit.passed, "check passed", "checks passed")}${c.audit.unknown ? `, ${c.audit.unknown} could not be checked here` : ""}${c.audit.not_applicable ? `, ${c.audit.not_applicable} not applicable` : ""}. | |
| The audit re-derives the package from its own files; it does not check that the material's form is right or that the recipe works.</p> | |
| <div class="links">${c.links.trace ? `<a class="primary" href="${c.links.trace}" target="_blank" rel="noopener">How Mat2Bot got there ↗</a>` : ""}<a href="${c.links.report}" target="_blank" rel="noopener">Full technical report ↗</a> | |
| <a href="${c.links.package}" download>Download the sealed package</a></div>`; | |
| } | |
| function crystalCaption(name) { | |
| const k = site.crystals[name]; | |
| const dots = Object.values(k.style).map((s) => `<span class="dot" style="--c:${s.color}"></span>${esc(s.name)}`).join(" "); | |
| const src = String(k.cod).startsWith("file:") ? "a generated cell, not a measured structure" : `measured structure, Crystallography Open Database ${esc(k.cod)}`; | |
| return `${dots}<br>${esc(k.label)}: ${src} · drag to turn`; | |
| } | |
| const STAGE_WORDS = { M01: "Target", M02: "Evidence", M04: "Routes", M03: "By analogy", M05: "Science", M07: "The case", M08: "Recipe", LAB: "Labs", AUDIT: "Audit" }; | |
| const EXEC_WORDS = { completed: ["ran", "ok"], skipped: ["skipped", "off"], refused: ["stopped", "warn"], failed: ["failed", "bad"], not_run: ["see below", "off"] }; | |
| function fillTrail(box) { | |
| fetch(box.dataset.trace).then((r) => (r.ok ? r.json() : null)).then((t) => { | |
| if (!t) { box.innerHTML = '<p class="muted">The trail could not be loaded.</p>'; return; } | |
| const stages = t.nodes.filter((n) => n.kind === "stage"); | |
| box.innerHTML = `<ol class="trail-strip">${stages.map((s) => { | |
| const w = EXEC_WORDS[s.execution] || [s.execution, "off"]; | |
| const kids = s.children.length; | |
| return `<li class="${w[1]}" title="${esc(s.detail.message || "")}"><b>${esc(STAGE_WORDS[s.stage] || s.stage)}</b><span>${esc(w[0])}${kids ? ` · ${kids}` : ""}</span></li>`; | |
| }).join("")}</ol>`; | |
| }).catch(() => { box.innerHTML = '<p class="muted">The trail could not be loaded.</p>'; }); | |
| } | |
| function openCase(id) { | |
| const i = site.cases.findIndex((c) => c.id === id); | |
| if (i < 0) return; | |
| const c = site.cases[i]; | |
| $("#case-body").innerHTML = caseHTML(c, i + 1); | |
| const trail = $("#case-body .trail"); | |
| if (trail) fillTrail(trail); | |
| $("#case-crystal").innerHTML = crystalCaption(c.crystal); | |
| if (!dialog.open) dialog.showModal(); | |
| $("#case-body").scrollTop = 0; | |
| if (gl) { | |
| if (!caseView) { | |
| try { caseView = new CrystalView($("#case-canvas"), { speed: 0.22 }); } catch { fallback($("#case-canvas"), c.crystal); } | |
| } | |
| caseView?.show(c.crystal); | |
| caseView?.play(); | |
| } | |
| history.replaceState(null, "", `#case-${id}`); | |
| } | |
| dialog.addEventListener("close", () => { | |
| caseView?.pause(); | |
| if (location.hash.startsWith("#case-")) history.replaceState(null, "", "#cases"); | |
| }); | |
| dialog.addEventListener("click", (e) => { if (e.target === dialog) dialog.close(); }); | |
| if (location.hash.startsWith("#case-")) openCase(location.hash.slice(6)); | |
| window.addEventListener("hashchange", () => { if (location.hash.startsWith("#case-")) openCase(location.hash.slice(6)); }); | |
| // ---------------------------------------------------------------- honesty | |
| const COLORS = [["met", "Met", "#16a34a"], ["met (otherwise)", "Met by the stated fallback", "#4ade80"], ["partial", "Partly met", "#f59e0b"], | |
| ["deviation", "Missed, and explained", "#ef4444"], ["not run", "Not run (no data access)", "#64748b"]]; | |
| const counts = site.evaluation.counts; | |
| $("#eval-total").textContent = site.evaluation.total; | |
| $("#review-total").textContent = site.review_rounds.reduce((a, b) => a + b, 0); | |
| $("#eval-legend").innerHTML = COLORS.filter(([k]) => counts[k]).map(([k, w, c]) => `<li><i style="--c:${c}"></i><b>${counts[k]}</b> ${esc(w)}</li>`).join(""); | |
| const cells = COLORS.flatMap(([k, , c]) => Array(counts[k] || 0).fill(c)); | |
| const waffle = $("#eval-waffle"); | |
| waffle.setAttribute("aria-label", COLORS.filter(([k]) => counts[k]).map(([k, w]) => `${counts[k]} ${w.toLowerCase()}`).join(", ")); | |
| waffle.innerHTML = cells.map((c, i) => `<span style="--c:${c};--i:${i}"></span>`).join(""); | |
| new IntersectionObserver((es, o) => { if (es.some((e) => e.isIntersecting)) { waffle.classList.add("on"); o.disconnect(); } }, { threshold: 0.3 }).observe(waffle); | |
| if (reducedMotion) waffle.classList.add("on"); | |
| // ---------------------------------------------------------------- glossary and footer | |
| $("#glossary").innerHTML = site.glossary.map((g) => `<div><dt>${esc(g.term)}</dt><dd>${esc(g.text)}</dd></div>`).join(""); | |
| $("#foot-build").textContent = `Results computed by Mat2Bot ${site.version} (build ${site.commit}, ${site.built}).`; | |