Spaces:
Running
Running
Download app.mjs from WideChaos/tiny-hash-intent: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/spaces/WideChaos/tiny-hash-intent/resolve/main/app.mjs
- Command line
-
hf download hf://spaces/WideChaos/tiny-hash-intent/app.mjs
-
curl -L -o app.mjs https://huggingface.co/spaces/WideChaos/tiny-hash-intent/resolve/main/app.mjs
3.45 kB
| import { parseModel, predict } from "./inference.mjs"; | |
| const $ = (id) => document.getElementById(id); | |
| let model = null, | |
| modelSHA = null, | |
| url = null; | |
| function clear(message = "Input changed. Classify again for current results.") { | |
| if (url) URL.revokeObjectURL(url); | |
| url = null; | |
| $("downloads").replaceChildren(); | |
| $("report").textContent = ""; | |
| $("results").hidden = true; | |
| $("placeholder").hidden = false; | |
| $("error").hidden = true; | |
| $("state").textContent = message; | |
| } | |
| for (const id of ["query", "calibrated"]) | |
| $(id).addEventListener("input", () => clear()); | |
| function fail(message) { | |
| $("error").textContent = message; | |
| $("error").hidden = false; | |
| $("error").focus(); | |
| } | |
| $("run").addEventListener("click", () => { | |
| clear(); | |
| if (!model) { | |
| fail("Model weights are not ready. Reload the page to retry."); | |
| return; | |
| } | |
| try { | |
| const input = $("query").value, | |
| result = predict(model, input, $("calibrated").checked), | |
| report = { | |
| model_version: model.version, | |
| work_identity: "work:tiny-hash-intent:v1", | |
| model_file_sha256: modelSHA, | |
| input, | |
| representation: | |
| "per-row/per-column int8 stored weights, dequantized Float64 inference", | |
| ...result, | |
| scope: | |
| "closed-set English benchmark model; class scores do not establish correctness or OOD detection", | |
| }; | |
| $("prediction").textContent = result.predicted_label; | |
| $("condition").textContent = | |
| `${result.feature_bins_used} hashed feature bins · temperature ${result.temperature} · 77 known classes`; | |
| $("top3").replaceChildren(); | |
| for (const item of result.top3) { | |
| const li = document.createElement("li"), | |
| name = document.createElement("span"), | |
| score = document.createElement("strong"); | |
| name.textContent = item.label; | |
| score.textContent = (100 * item.score).toFixed(2) + "%"; | |
| li.append(name, score); | |
| $("top3").append(li); | |
| } | |
| $("report").textContent = JSON.stringify(report, null, 2); | |
| url = URL.createObjectURL( | |
| new Blob([JSON.stringify(report, null, 2) + "\n"], { | |
| type: "application/json;charset=utf-8", | |
| }), | |
| ); | |
| const a = document.createElement("a"); | |
| a.textContent = "Download current inference report"; | |
| a.href = url; | |
| a.download = "tiny-hash-intent-report.json"; | |
| $("downloads").append(a); | |
| $("placeholder").hidden = true; | |
| $("results").hidden = false; | |
| $("state").textContent = | |
| "Inference completed locally for the current text."; | |
| } catch (e) { | |
| fail(e.message || "Inference failed."); | |
| $("state").textContent = "No result for the current input."; | |
| } | |
| }); | |
| try { | |
| const response = await fetch("run-001/model-int8.json", { | |
| cache: "no-store", | |
| }); | |
| if (!response.ok) throw Error("Fixed model asset could not load."); | |
| const raw = await response.text(); | |
| model = parseModel(raw); | |
| if (!crypto.subtle) | |
| throw Error("Use HTTPS or localhost for model identity hashing."); | |
| modelSHA = Array.from( | |
| new Uint8Array( | |
| await crypto.subtle.digest("SHA-256", new TextEncoder().encode(raw)), | |
| ), | |
| (b) => b.toString(16).padStart(2, "0"), | |
| ).join(""); | |
| $("run").disabled = false; | |
| $("state").textContent = "Fixed model ready. Your input stays local."; | |
| } catch (e) { | |
| model = null; | |
| fail(e.message || "Model could not load."); | |
| $("state").textContent = | |
| "Model unavailable. Serve the complete release over HTTP(S) and reload."; | |
| } | |