Spaces:
Running
Running
Download app.js from thelamapi/Next-Shift: direct link, hf CLI and curl.
- Browser
- Download file 4.85 kB
-
https://huggingface.co/spaces/thelamapi/Next-Shift/resolve/main/app.js
- Command line
-
hf download hf://spaces/thelamapi/Next-Shift/app.js
-
curl -L -o app.js https://huggingface.co/spaces/thelamapi/Next-Shift/resolve/main/app.js
4.85 kB
| /* Next Shift Space: label a few target-environment digits; the model adapts in the browser. */ | |
| const { $, h, fmt, grayImage, lineChart, legend } = NLUI; | |
| let ctx, curves, labels = {}, active = null, result = null; | |
| const COL = { next_shift: "#0b6e4f", frozen: "#2f5fb3", random_init: "#8a8f98", you: "#c0392b" }; | |
| function poolGrid() { | |
| const box = $("pool"); box.innerHTML = ""; | |
| ctx.d.pool_images.forEach((im, i) => { | |
| const c = h("canvas", { title: "pool item " + i }); | |
| grayImage(c, im, 16, 16, 3, { lo: 0, hi: 255 }); | |
| const tag = labels[i] != null ? String(labels[i]) : ""; | |
| const cell = h("button", { class: "chip" + (labels[i] != null ? " on" : ""), style: "padding:2px;border-radius:6px;position:relative", "data-pool": i, | |
| onclick: () => { active = i; picker(); } }, c, h("span", { style: "position:absolute;right:3px;bottom:0;font-weight:700" }, tag)); | |
| if (active === i) cell.style.outline = "2px solid #c0392b"; | |
| box.append(cell); | |
| }); | |
| } | |
| function picker() { | |
| const p = $("picker"); p.innerHTML = ""; | |
| if (active == null) { p.textContent = "Tap an image above, then choose its digit."; return; } | |
| const c = h("canvas"); grayImage(c, ctx.d.pool_images[active], 16, 16, 5, { lo: 0, hi: 255 }); | |
| const row = h("div", { class: "chips" }); | |
| for (let k = 0; k < 10; k++) row.append(h("button", { class: "chip", "data-label": k, onclick: () => setLabel(active, k) }, String(k))); | |
| row.append(h("button", { class: "chip", onclick: () => setLabel(active, null) }, "clear")); | |
| const hint = $("hint").checked ? h("span", { class: "small muted" }, ` dataset label: ${ctx.d.pool_y[active]}`) : null; | |
| p.append(h("div", { class: "row" }, c, hint), row); | |
| } | |
| function setLabel(i, k) { | |
| if (k == null) delete labels[i]; else labels[i] = k; | |
| active = null; poolGrid(); picker(); update(); | |
| } | |
| function update() { | |
| const ids = Object.keys(labels).map(Number).sort((a, b) => a - b); | |
| $("status").textContent = `Adapting with ${ids.length} labeled target images…`; | |
| const t0 = performance.now(); | |
| result = Core.adapt(ctx, ids, ids.map(i => labels[i])); | |
| $("k").textContent = ids.length; | |
| $("acc_ft").textContent = fmt(100 * result.accFt, 1) + "%"; | |
| $("acc_lin").textContent = fmt(100 * result.accLin, 1) + "%"; | |
| $("status").textContent = `Updated in ${fmt((performance.now() - t0) / 1000, 2)} s on ${ctx.d.test_y.length} held-out target images.`; | |
| const box = $("preds"); box.innerHTML = ""; | |
| for (let i = 0; i < 36; i++) { | |
| const c = h("canvas"); grayImage(c, ctx.d.test_images[i], 16, 16, 3, { lo: 0, hi: 255 }); | |
| const ok = result.predFt[i] === ctx.d.test_y[i]; | |
| box.append(h("div", { style: "text-align:center" }, c, h("div", { class: "small", style: `color:${ok ? "#0b6e4f" : "#c0392b"};font-weight:600` }, String(result.predFt[i])))); | |
| } | |
| chart(ids.length); | |
| } | |
| function chart(k) { | |
| const names = { next_shift: "Next Shift (AdaBN + fine-tune top)", frozen: "Frozen source features + linear head", random_init: "Random init, same data and steps" }; | |
| const series = Object.keys(names).map(m => ({ name: names[m] + " (benchmark)", color: COL[m], dash: m === "next_shift" ? [] : [5, 4], | |
| points: curves.k.map((kk, j) => [kk * 10, curves.mean[m][j]]) })); | |
| series.push({ name: "your labels (Next Shift, this page)", color: COL.you, points: [[k, result.accFt]], width: 6 }); | |
| const r = lineChart($("chart"), series, { xlabel: "labeled target images (benchmark uses k per class × 10)", ymin: 0, ymax: 1 }); | |
| const g = r.ctx; g.fillStyle = COL.you; g.beginPath(); g.arc(r.X(k), r.Y(result.accFt), 5, 0, 7); g.fill(); | |
| legend($("cl"), series); | |
| } | |
| function autoLabel() { | |
| // one dataset-labeled example per digit, first unlabeled ones in pool order | |
| for (let c = 0; c < 10; c++) { | |
| const i = ctx.d.pool_y.findIndex((y, j) => y === c && labels[j] == null); | |
| if (i >= 0) labels[i] = c; | |
| } | |
| poolGrid(); update(); | |
| } | |
| window.__liveTest = function () { | |
| labels = {}; update(); | |
| const zero = result.accFt; | |
| for (let c = 0; c < 10; c++) { | |
| const i = ctx.d.pool_y.findIndex(y => y === c); | |
| document.querySelector(`button[data-pool="${i}"]`).click(); | |
| document.querySelector(`#picker button[data-label="${c}"]`).click(); | |
| } | |
| return { ok: Object.keys(labels).length === 10 && $("k").textContent === "10" && result.predFt.length === ctx.d.test_y.length, | |
| acc_zero_shot: zero, acc_after_10_labels: result.accFt, frozen_after_10: result.accLin }; | |
| }; | |
| NLUI.boot(Core, async c => { | |
| ctx = c; | |
| curves = await (await fetch("curves.json")).json(); | |
| $("auto").addEventListener("click", autoLabel); | |
| $("reset").addEventListener("click", () => { labels = {}; active = null; poolGrid(); picker(); update(); }); | |
| $("hint").addEventListener("change", picker); | |
| window.addEventListener("resize", () => result && chart(Object.keys(labels).length)); | |
| poolGrid(); picker(); update(); | |
| }); | |