/* Next Match Space: pick a reference product (or upload a photo), type an edit, rank the catalog live. */ const { $, h, fmt } = NLUI; let ctx, sprite, refPixels = null, refId = null; const SUGGEST = ["bunun kırmızı olanı", "çizgili olanı", "aynısı ama kupa", "mavi ve puantiyeli olsun", "the black one", "same but a hat", "make it striped", "sarı renklisi"]; function thumbCanvas(id, scale) { const c = document.createElement("canvas"), n = ctx.cat.cols, s = 48; c.width = s; c.height = s; c.style.width = c.style.height = s * scale + "px"; c.getContext("2d").drawImage(sprite, (id % n) * s, Math.floor(id / n) * s, s, s, 0, 0, s, s); return c; } function pixelsOf(canvas) { const d = canvas.getContext("2d").getImageData(0, 0, 48, 48).data, px = new Float64Array(3 * 48 * 48); for (let i = 0; i < 48 * 48; i++) for (let c = 0; c < 3; c++) px[c * 2304 + i] = d[4 * i + c] / 255; return px; } function pickRef(id) { refId = id; const c = thumbCanvas(id, 3); refPixels = pixelsOf(c); $("ref").innerHTML = ""; $("ref").append(c, h("div", { class: "small muted" }, describe(id))); document.querySelectorAll("#gallery canvas").forEach((el, i) => el.style.outline = i === id ? "3px solid #c0392b" : ""); search(); } function describe(id) { const a = ctx.cat.items[id]; return `${a.color} · ${a.pattern} · ${a.category}`; } function search() { if (!refPixels) return null; const text = $("q").value, toks = Core.tokenize(text, ctx.vocab); const inv = Object.fromEntries(Object.entries(ctx.vocab).map(([k, v]) => [v, k])); $("toks").textContent = toks.length ? "understood words: " + toks.map(i => inv[i]).join(", ") : "no known words (the text-only and combined models fall back to the image)"; const t0 = performance.now(), out = {}; for (const mode of ["combined", "image", "text"]) { const ranked = Core.rank(ctx, Core.encodeQuery(ctx, refPixels, text, mode), mode).slice(0, 8); out[mode] = ranked; const box = $("r_" + mode); box.innerHTML = ""; ranked.forEach(([id, s], k) => box.append(h("div", { style: "text-align:center" }, thumbCanvas(id, 1.25), h("div", { class: "small", style: k === 0 ? "font-weight:700" : "" }, `${k + 1}. ${fmt(s, 2)}`)))); } $("ms").textContent = fmt(performance.now() - t0, 1); return out; } async function upload(file) { const img = new Image(); img.src = URL.createObjectURL(file); await img.decode(); const c = document.createElement("canvas"); c.width = c.height = 48; const g = c.getContext("2d"); g.fillStyle = "#e6e4e2"; g.fillRect(0, 0, 48, 48); const s = Math.min(44 / img.width, 44 / img.height); g.drawImage(img, (48 - img.width * s) / 2, (48 - img.height * s) / 2, img.width * s, img.height * s); refPixels = pixelsOf(c); refId = null; c.style.width = c.style.height = "144px"; $("ref").innerHTML = ""; $("ref").append(c, h("div", { class: "small muted" }, "your image (outside the training distribution)")); search(); } window.__liveTest = function () { const target = ctx.cat.items.findIndex(a => a.category === "mug" && a.color === "red" && a.pattern === "striped"); const ref = ctx.cat.items.findIndex(a => a.category === "mug" && a.color === "blue" && a.pattern === "striped"); document.querySelectorAll("#gallery canvas")[ref].click(); $("q").value = "bunun kırmızı olanı"; $("q").dispatchEvent(new Event("input")); const out = search(); const rankOf = mode => out[mode].findIndex(r => r[0] === target); return { ok: out.combined.length === 8 && $("r_combined").children.length === 8, target_rank_combined: rankOf("combined"), target_rank_image: rankOf("image"), target_rank_text: rankOf("text"), top1: describe(out.combined[0][0]) }; }; NLUI.boot(Core, async c => { ctx = c; sprite = new Image(); sprite.src = "catalog.png"; await sprite.decode(); const g = $("gallery"); ctx.cat.items.forEach((a, id) => { const t = thumbCanvas(id, 1); t.title = describe(id); t.style.cursor = "pointer"; t.addEventListener("click", () => pickRef(id)); g.append(t); }); for (const s of SUGGEST) $("chips").append(h("button", { class: "chip", onclick: () => { $("q").value = s; search(); } }, s)); $("q").addEventListener("input", search); $("file").addEventListener("change", e => e.target.files[0] && upload(e.target.files[0])); pickRef(ctx.cat.items.findIndex(a => a.category === "tshirt" && a.color === "blue" && a.pattern === "plain")); $("q").value = "bunun kırmızı olanı"; search(); });