import { Client, handle_file, } from "https://cdn.jsdelivr.net/npm/@gradio/client@1.15.0/dist/index.min.js"; const $ = (selector, root = document) => root.querySelector(selector); const $$ = (selector, root = document) => [...root.querySelectorAll(selector)]; const dims = [64, 128, 256, 512, 1024, 2048, 4096]; const colors = ["#d9ff63", "#66e3cf", "#ff9c62", "#a993ff"]; const state = { client: null, connecting: null, searchImage: null, searchVideo: null, candidateMedia: [], pairQueryImage: null, pairQueryVideo: null, pairCandidateImage: null, pairCandidateVideo: null, searchDimension: 1024, compareDimension: 1024, objectUrls: new Map(), jobTimer: null, jobStarted: 0, }; const examples = { llama: { text: "Which Llama 4 model variants are available?", dimension: 512, }, tofu: { text: "How is mapo tofu prepared?", dimension: 1024, }, safety: { text: "Find the environmental assessment page about driver training and temporary road closures.", dimension: 256, }, image: { text: "Match this screenshot to the most relevant description.", asset: "/assets/llama4_hgf.png", filename: "llama4_hgf.png", type: "image/png", custom: "Llama family :: Scout and Maverick are multimodal mixture-of-experts model variants.\nRecipe :: Soft tofu simmered in spicy chili-bean sauce.", include: false, dimension: 256, }, video: { text: "What dish is being prepared in this clip?", asset: "/assets/mapo_tofu.mp4", filename: "mapo_tofu.mp4", type: "video/mp4", custom: "Sichuan classic :: Mapo tofu combines soft tofu with a spicy, numbing bean-paste sauce.\nSpaceflight :: A launch vehicle carries a satellite into orbit.", include: false, dimension: 512, }, }; function escapeHtml(value) { return String(value ?? "") .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); } function formatScore(value) { const number = Number(value); return `${number >= 0 ? "+" : ""}${number.toFixed(3)}`; } function formatSeconds(value) { return `${Number(value).toFixed(1)}s`; } function clearObjectUrls() { for (const value of state.objectUrls.values()) URL.revokeObjectURL(value); state.objectUrls.clear(); } async function connectClient() { if (state.client) return state.client; if (state.connecting) return state.connecting; const status = $("#model-status"); state.connecting = (async () => { try { const client = await Client.connect(window.location.origin); await client.view_api(); state.client = client; status.className = "model-status ready"; $("span", status).textContent = "Model ready"; return client; } catch (error) { status.className = "model-status error"; $("span", status).textContent = "Connection failed"; state.connecting = null; throw error; } })(); return state.connecting; } function switchMode(mode) { $$(".nav-pill").forEach((button) => button.classList.toggle("active", button.dataset.mode === mode)); $$("[data-panel]").forEach((panel) => { panel.hidden = panel.dataset.panel !== mode; }); $(mode === "search" ? '[data-panel="search"]' : '[data-panel="compare"]').scrollIntoView({ behavior: "smooth", block: "start" }); } function setDimension(group, value) { const dimension = Number(value); state[`${group}Dimension`] = dimension; $(`[data-dimension-group="${group}"]`).querySelectorAll("button").forEach((button) => { button.classList.toggle("active", Number(button.dataset.value) === dimension); }); $(`#${group}-dimension`).value = String(dimension); const output = $(`#${group}-dimension-output`); if (output) output.textContent = `${dimension.toLocaleString()}D`; } function fileLabel(drop, file) { const chip = $(".file-chip", drop); if (!chip) return; chip.hidden = !file; if (file) $("span", chip).textContent = file.name; } function configureDropzone(dropSelector, inputSelector, stateKey, otherStateKey, otherDropSelector) { const drop = $(dropSelector); const input = $(inputSelector); const acceptFile = (file) => { if (!file) return; state[stateKey] = file; fileLabel(drop, file); if (otherStateKey) { state[otherStateKey] = null; fileLabel($(otherDropSelector), null); const otherInput = $(`${otherDropSelector} input`); if (otherInput) otherInput.value = ""; } }; input.addEventListener("change", () => acceptFile(input.files[0])); ["dragenter", "dragover"].forEach((eventName) => drop.addEventListener(eventName, (event) => { event.preventDefault(); drop.classList.add("dragover"); })); ["dragleave", "drop"].forEach((eventName) => drop.addEventListener(eventName, (event) => { event.preventDefault(); drop.classList.remove("dragover"); })); drop.addEventListener("drop", (event) => acceptFile(event.dataTransfer.files[0])); const remove = $(".file-chip button", drop); if (remove) remove.addEventListener("click", (event) => { event.preventDefault(); event.stopPropagation(); input.value = ""; state[stateKey] = null; fileLabel(drop, null); }); } function renderCandidateChips() { const root = $("#candidate-chips"); root.innerHTML = state.candidateMedia.map((file, index) => ` ${escapeHtml(file.name)} `).join(""); $$('[data-remove-media]', root).forEach((button) => button.addEventListener("click", () => { state.candidateMedia.splice(Number(button.dataset.removeMedia), 1); renderCandidateChips(); })); } function bindPairFile(inputSelector, stateKey, otherKey, labelSelector) { const input = $(inputSelector); input.addEventListener("change", () => { const file = input.files[0] ?? null; state[stateKey] = file; if (file && otherKey) { state[otherKey] = null; const side = input.closest(".compare-side"); const otherInput = input.accept.includes("image") ? $('input[accept="video/*"]', side) : $('input[accept="image/*"]', side); if (otherInput) otherInput.value = ""; } const label = $(labelSelector); label.hidden = !file; label.textContent = file ? file.name : ""; }); } function startJob(mode, heavy = false) { const toast = $("#job-toast"); const phase = $("#job-phase"); const elapsed = $("#job-elapsed"); const progress = $("#job-progress-bar"); $("#job-label").textContent = mode === "search" ? "RESOLVING SEMANTIC FIELD" : "MEASURING VECTOR ALIGNMENT"; phase.textContent = "Waiting for the ZeroGPU allocator…"; progress.style.width = "3%"; toast.hidden = false; state.jobStarted = Date.now(); clearInterval(state.jobTimer); state.jobTimer = setInterval(() => { const seconds = Math.floor((Date.now() - state.jobStarted) / 1000); elapsed.textContent = `${String(Math.floor(seconds / 60)).padStart(2, "0")}:${String(seconds % 60).padStart(2, "0")} elapsed`; const first = heavy ? 13 : 7; if (seconds < 3) { phase.textContent = "Waiting for the ZeroGPU allocator…"; progress.style.width = "8%"; } else if (seconds < first) { phase.textContent = "Encoding inputs into 4,096 dimensions…"; progress.style.width = `${Math.min(52, 18 + seconds * 4)}%`; } else { phase.textContent = mode === "search" ? "Ranking the multimodal universe…" : "Tracing the Matryoshka curve…"; progress.style.width = `${Math.min(92, 55 + seconds)}%`; } }, 500); } function finishJob() { clearInterval(state.jobTimer); $("#job-phase").textContent = "Semantic field resolved."; $("#job-progress-bar").style.width = "100%"; setTimeout(() => { $("#job-toast").hidden = true; }, 900); } function failJob(error) { clearInterval(state.jobTimer); $("#job-toast").hidden = true; const toast = $("#error-toast"); $("span", toast).textContent = error?.message || String(error) || "The request could not be completed."; toast.hidden = false; } function unwrapResult(result) { let data = result?.data ?? result; if (Array.isArray(data) && data.length === 1) data = data[0]; if (typeof data === "string") { try { return JSON.parse(data); } catch { return data; } } return data; } function mediaUrl(item) { if (item.media_url) return item.media_url; return state.objectUrls.get(item.key) ?? null; } function mediaMarkup(item, className = "rank-media") { const url = mediaUrl(item); if (!url) return `Aa`; if (item.kind === "video") return ``; return ``; } function rankingMarkup(items) { return `
${items.map((item) => { const fill = Math.max(2, Math.min(100, Math.max(0, Number(item.score)) * 100)); return `
${String(item.rank).padStart(2, "0")} ${mediaMarkup(item)}
${escapeHtml(item.title)}${escapeHtml(item.kind)}

${escapeHtml(item.description)}

${formatScore(item.score)}COSINE
`; }).join("")}
`; } function visualMarkup(items) { const visual = items.filter((item) => mediaUrl(item)); if (!visual.length) return `

No visual candidates in this result set.

`; return `
${visual.map((item) => `
${item.kind === "video" ? `` : `${escapeHtml(item.title)}`}
#${item.rank} · ${escapeHtml(item.title)}${formatScore(item.score)}
`).join("")}
`; } function lineChart(dimensions, series, title) { const width = 720; const height = 260; const pad = { l: 46, r: 15, t: 20, b: 36 }; const values = series.flatMap((item) => item.values); let low = Math.max(-1, Math.min(...values) - 0.08); let high = Math.min(1, Math.max(...values) + 0.08); if (high - low < 0.2) { const mid = (high + low) / 2; low = Math.max(-1, mid - 0.1); high = Math.min(1, mid + 0.1); } const x = (index) => pad.l + (index * (width - pad.l - pad.r)) / (dimensions.length - 1); const y = (value) => pad.t + ((high - value) * (height - pad.t - pad.b)) / Math.max(0.0001, high - low); const grid = Array.from({ length: 5 }, (_, index) => { const value = high - (index * (high - low)) / 4; return `${formatScore(value).slice(0,-1)}`; }).join(""); const labels = dimensions.map((value, index) => `${value}`).join(""); const paths = series.map((item, seriesIndex) => { const color = colors[seriesIndex % colors.length]; const points = item.values.map((value, index) => `${x(index)},${y(value)}`).join(" "); const dots = item.values.map((value, index) => ``).join(""); return `${dots}`; }).join(""); const legend = series.map((item, index) => `${escapeHtml(item.title.slice(0, 40))}`).join(""); return `
MATRYOSHKA SCOPE${escapeHtml(title)}
64 → 4096D
${grid}${labels}${paths}
${legend}
`; } function fingerprintChart(values, title, dimension) { const width = 720; const height = 155; const center = 78; const scale = Math.max(...values.map((value) => Math.abs(value)), 0.000001); const barWidth = (width - 16) / values.length; const bars = values.map((value, index) => { const magnitude = Math.min(64, (Math.abs(value) / scale) * 64); const y = value >= 0 ? center - magnitude : center; return ``; }).join(""); return `
VECTOR FINGERPRINT${escapeHtml(title)}
${Number(dimension).toLocaleString()} values
${bars}
POSITIVENEGATIVE96 POOLED SLICES
`; } function telemetryMarkup(data) { const compact = { ...data }; delete compact.rankings; delete compact.dimension_series; delete compact.query_fingerprint; delete compact.candidate_fingerprint; return `
EMBEDDING TELEMETRYINSPECT JSON +
${escapeHtml(JSON.stringify(compact, null, 2))}
`; } function analysisMarkup(data) { return `
${lineChart(data.dimensions, data.dimension_series, "Does the ranking survive compression?")}${fingerprintChart(data.query_fingerprint, `Query · ${data.query_kind}`, data.dimension)}${telemetryMarkup(data)}
`; } function bindResultTabs(root) { $$('[data-result-tab]', root).forEach((button) => button.addEventListener("click", () => { $$('[data-result-tab]', root).forEach((item) => item.classList.toggle("active", item === button)); $$('[data-result-view]', root).forEach((view) => { view.hidden = view.dataset.resultView !== button.dataset.resultTab; }); })); } function renderSearchResults(data) { const root = $("#search-results"); root.innerHTML = `
FIELD RESOLVEDtencent / WeMM-Embedding-9B
${data.candidate_count} CANDIDATES · ${Number(data.dimension).toLocaleString()}D
TOP SEMANTIC MATCH

${escapeHtml(data.top_match.title)}

${escapeHtml(data.query_kind)} query · ${formatSeconds(data.elapsed_seconds)} GPU pass · ${escapeHtml(data.cache_state)} cache

${formatScore(data.top_match.score)}COSINE
${rankingMarkup(data.rankings)}
`; bindResultTabs(root); root.scrollIntoView({ behavior: "smooth", block: "start" }); } function renderCompareResults(data) { const ring = Math.max(0, Math.min(100, (Number(data.selected_score) + 1) * 50)); const series = [{ title: `${data.query_kind} → ${data.candidate_kind}`, values: data.scores }]; $("#compare-results").innerHTML = `
${formatScore(data.selected_score)}COSINE
PAIRWISE READOUT

${escapeHtml(data.label)}

${escapeHtml(data.explanation)}

${escapeHtml(data.query_kind)}${escapeHtml(data.candidate_kind)}${Number(data.dimension).toLocaleString()}D${formatSeconds(data.elapsed_seconds)}
${lineChart(data.dimensions, series, "Semantic alignment under compression?")}
${fingerprintChart(data.query_fingerprint, `A · ${data.query_kind}`, data.dimension)}${fingerprintChart(data.candidate_fingerprint, `B · ${data.candidate_kind}`, data.dimension)}
${telemetryMarkup(data)}
`; $("#compare-results").scrollIntoView({ behavior: "smooth", block: "start" }); } async function submitSearch() { const button = $("#search-button"); clearObjectUrls(); state.candidateMedia.forEach((file, index) => state.objectUrls.set(`custom-media-${index + 1}`, URL.createObjectURL(file))); const payload = { query_text: $("#query-text").value, query_image: state.searchImage ? handle_file(state.searchImage) : null, query_video: state.searchVideo ? handle_file(state.searchVideo) : null, custom_texts: $("#custom-texts").value, candidate_media: state.candidateMedia.map((file) => handle_file(file)), include_showcase: $("#include-showcase").checked, dimension: state.searchDimension, }; button.disabled = true; startJob("search", Boolean(state.searchVideo || state.candidateMedia.some((file) => file.type.startsWith("video/")) || payload.include_showcase)); try { const client = await connectClient(); const result = await client.predict("/search", payload); const data = unwrapResult(result); if (!data || typeof data !== "object") throw new Error("The server returned an unexpected search result."); renderSearchResults(data); finishJob(); } catch (error) { failJob(error); } finally { button.disabled = false; } } async function submitCompare() { const button = $("#compare-button"); const payload = { query_text: $("#pair-query-text").value, query_image: state.pairQueryImage ? handle_file(state.pairQueryImage) : null, query_video: state.pairQueryVideo ? handle_file(state.pairQueryVideo) : null, candidate_text: $("#pair-candidate-text").value, candidate_image: state.pairCandidateImage ? handle_file(state.pairCandidateImage) : null, candidate_video: state.pairCandidateVideo ? handle_file(state.pairCandidateVideo) : null, dimension: state.compareDimension, }; button.disabled = true; startJob("compare", Boolean(state.pairQueryVideo || state.pairCandidateVideo)); try { const client = await connectClient(); const result = await client.predict("/compare", payload); const data = unwrapResult(result); if (!data || typeof data !== "object") throw new Error("The server returned an unexpected comparison result."); renderCompareResults(data); finishJob(); } catch (error) { failJob(error); } finally { button.disabled = false; } } async function loadAssetAsFile(url, filename, type) { const response = await fetch(url); if (!response.ok) throw new Error(`Could not load the curated asset ${filename}.`); return new File([await response.blob()], filename, { type }); } async function runExample(name) { const example = examples[name]; switchMode("search"); $("#query-text").value = example.text; $("#custom-texts").value = example.custom ?? ""; $("#include-showcase").checked = example.include ?? true; state.searchImage = null; state.searchVideo = null; fileLabel($("#query-image-drop"), null); fileLabel($("#query-video-drop"), null); $("#query-image").value = ""; $("#query-video").value = ""; setDimension("search", example.dimension); if (example.asset) { try { const file = await loadAssetAsFile(example.asset, example.filename, example.type); if (example.type.startsWith("image")) { state.searchImage = file; fileLabel($("#query-image-drop"), file); } else { state.searchVideo = file; fileLabel($("#query-video-drop"), file); } } catch (error) { failJob(error); return; } } setTimeout(submitSearch, 350); } function resetSearch() { state.searchImage = null; state.searchVideo = null; state.candidateMedia = []; setDimension("search", 1024); setTimeout(() => { fileLabel($("#query-image-drop"), null); fileLabel($("#query-video-drop"), null); renderCandidateChips(); }); } function bindEvents() { $$(".nav-pill").forEach((button) => button.addEventListener("click", () => switchMode(button.dataset.mode))); $$("[data-dimension-group]").forEach((group) => $$('button', group).forEach((button) => button.addEventListener("click", () => setDimension(group.dataset.dimensionGroup, button.dataset.value)))); $$(".expedition").forEach((button) => button.addEventListener("click", () => runExample(button.dataset.example))); configureDropzone("#query-image-drop", "#query-image", "searchImage", "searchVideo", "#query-video-drop"); configureDropzone("#query-video-drop", "#query-video", "searchVideo", "searchImage", "#query-image-drop"); $("#candidate-media").addEventListener("change", (event) => { state.candidateMedia = [...event.target.files].slice(0, 6); renderCandidateChips(); }); bindPairFile("#pair-query-image", "pairQueryImage", "pairQueryVideo", "#pair-query-file"); bindPairFile("#pair-query-video", "pairQueryVideo", "pairQueryImage", "#pair-query-file"); bindPairFile("#pair-candidate-image", "pairCandidateImage", "pairCandidateVideo", "#pair-candidate-file"); bindPairFile("#pair-candidate-video", "pairCandidateVideo", "pairCandidateImage", "#pair-candidate-file"); $("#search-form").addEventListener("submit", (event) => { event.preventDefault(); submitSearch(); }); $("#search-form").addEventListener("reset", resetSearch); $("#compare-form").addEventListener("submit", (event) => { event.preventDefault(); submitCompare(); }); $("#query-text").addEventListener("keydown", (event) => { if ((event.metaKey || event.ctrlKey) && event.key === "Enter") { event.preventDefault(); submitSearch(); } }); $("#job-close").addEventListener("click", () => { $("#job-toast").hidden = true; }); $("#error-toast button").addEventListener("click", () => { $("#error-toast").hidden = true; }); } bindEvents(); connectClient().catch(() => {});