// Decima · Live city dispatch. Invented city reports stream in; for each one the model answers three // Jev-style questions (which service? how severe? is someone in danger?) in this browser tab. // ?video=1 plays one pass with a title card and an end card (used to record the demo video). import { detectLang } from "./decima.js"; const MODEL_URL = "https://huggingface.co/amyrmahdy/decima-small/resolve/main/onnx/int8/"; const params = new URLSearchParams(location.search); const VIDEO = params.has("video"); const $ = (s) => document.querySelector(s); // ───────────────────────── the model, in a worker (same protocol as the Playground) ───────────────────────── 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); }; const call = (op, args = {}, onProgress) => new Promise((resolve, reject) => { const id = ++seq; pending.set(id, { resolve, reject, onProgress }); worker.postMessage({ id, op, ...args }); }); // ───────────────────────── services, severity, districts ───────────────────────── const SERVICE = { fire: { icon: "🔥", name: "Fire" }, police: { icon: "🚓", name: "Police" }, medical: { icon: "🚑", name: "Ambulance" }, traffic: { icon: "🚦", name: "Traffic" }, utilities: { icon: "⚡", name: "Utilities" }, city_services: { icon: "🏙️", name: "City services" }, }; const SEV_NAME = ["low", "moderate", "high", "critical"]; const SEV_VAR = ["--sev0", "--sev1", "--sev2", "--sev3"]; const DISTRICTS = { "Northside": [520, 95], "University": [185, 150], "Old Town": [420, 250], "Market": [640, 255], "Eastgate": [870, 190], "Central Station": [290, 385], "Riverside": [560, 420], "Parkview": [150, 520], "Harbor": [800, 520], }; const REVIEW_SERVICE = 0.6; // below this, the routing goes to a person const REVIEW_DANGER = [0.35, 0.65]; // a danger probability in this band goes to a person too // ───────────────────────── map ───────────────────────── const NS = "http://www.w3.org/2000/svg"; const svg = $("#map"); const el = (tag, attrs = {}, parent = svg) => { const e = document.createElementNS(NS, tag); for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, v); parent.appendChild(e); return e; }; function rng(seed) { // small deterministic PRNG so the city looks the same every time let s = seed >>> 0; return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 2 ** 32); } function drawMap() { el("rect", { x: 0, y: 0, width: 1000, height: 640, class: "m-land" }); const r = rng(7); for (let x = 20; x < 1000; x += 62) { for (let y = 18; y < 640; y += 54) { if (r() < 0.12) continue; el("rect", { x: x + r() * 4, y: y + r() * 4, width: 50 - r() * 12, height: 42 - r() * 10, rx: 3, class: "m-block" }); } } el("path", { class: "m-park", d: "M40 450 C 90 420 230 430 260 470 C 290 520 250 600 170 610 C 90 620 30 560 40 450 Z" }); el("path", { class: "m-park", d: "M860 60 C 930 50 980 90 975 140 C 940 150 880 140 860 60 Z" }); el("path", { class: "m-water", d: "M650 640 C 700 560 820 560 1000 575 L 1000 640 Z" }); el("path", { class: "m-river", d: "M-20 300 C 150 320 250 250 360 320 C 470 390 560 330 660 360 C 760 390 760 520 700 640" }); for (const d of ["M0 205 L1000 175", "M470 0 L520 640", "M0 470 C 300 450 600 480 1000 430"]) el("path", { class: "m-road major", d }); for (const d of ["M230 0 L260 640", "M760 0 L720 640", "M0 95 L1000 60", "M0 360 L1000 330", "M880 0 L940 640"]) el("path", { class: "m-road", d }); for (const [name, [x, y]] of Object.entries(DISTRICTS)) { const t = el("text", { x, y: y - 22, class: "m-label", "text-anchor": "middle" }); t.textContent = name; } } const pinLayer = () => svg.querySelector("#pins") || el("g", { id: "pins" }); function addPin(item) { const [cx, cy] = DISTRICTS[item.district] || [500, 320]; const jr = rng(item.n * 97 + 13); const x = cx + (jr() - 0.5) * 130, y = cy + 4 + jr() * 58; const color = getComputedStyle(document.documentElement).getPropertyValue(SEV_VAR[item.sev]).trim(); const g = el("g", { class: "pin new", "data-n": item.n }, pinLayer()); if (item.danger >= 0.5) el("circle", { cx: x, cy: y, r: 15, class: "pulse" }, g); el("circle", { cx: x, cy: y, r: 15, fill: color, class: "ring", stroke: item.danger >= 0.5 ? "var(--sev3)" : "rgba(0,0,0,0.35)" }, g); const t = el("text", { x, y: y + 1 }, g); t.textContent = SERVICE[item.service]?.icon || "•"; g.addEventListener("mouseenter", () => highlight(item.n, true)); g.addEventListener("mouseleave", () => highlight(item.n, false)); requestAnimationFrame(() => requestAnimationFrame(() => g.classList.remove("new"))); const pins = [...pinLayer().children]; pins.slice(0, Math.max(0, pins.length - 12)).forEach((p) => p.classList.add("old")); if (pins.length > 40) pins[0].remove(); } function highlight(n, on) { document.querySelectorAll(`[data-n="${n}"]`).forEach((e) => e.classList.toggle("hl", on)); } // ───────────────────────── feed ───────────────────────── const feed = $("#feed"); const stats = { reports: 0, decisions: 0, review: 0, ms: [] }; function renderStats() { $("#st-reports").textContent = stats.reports; $("#st-decisions").textContent = stats.decisions; $("#st-review").textContent = stats.review; if (stats.ms.length) { const s = [...stats.ms].sort((a, b) => a - b); $("#st-ms").textContent = Math.round(s[Math.floor(s.length / 2)]); } } function card(item) { const li = document.createElement("li"); li.className = "card"; li.dataset.n = item.n; const now = new Date(); li.innerHTML = `