File size: 3,449 Bytes
a2bd61d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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.";
}