Spaces:
Running
Running
Download test_inference.mjs from WideChaos/tiny-hash-intent: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/WideChaos/tiny-hash-intent/resolve/main/test_inference.mjs
- Command line
-
hf download hf://spaces/WideChaos/tiny-hash-intent/test_inference.mjs
-
curl -L -o test_inference.mjs https://huggingface.co/spaces/WideChaos/tiny-hash-intent/resolve/main/test_inference.mjs
1.45 kB
| import assert from "node:assert/strict"; | |
| import fs from "node:fs"; | |
| import { | |
| parseModel, | |
| predict, | |
| features, | |
| fnv, | |
| normalized, | |
| } from "./inference.mjs"; | |
| const raw = fs.readFileSync( | |
| new URL("./run-001/model-int8.json", import.meta.url), | |
| "utf8", | |
| ), | |
| model = parseModel(raw); | |
| assert.equal(fnv("hello"), 0x4f9f2cab); | |
| assert.equal(normalized(" Cafe\u0301\u0085 NOTE "), "café note"); | |
| assert.ok(features("😀").ids.length > 0); | |
| for (const text of [ | |
| "Where is my card?", | |
| "中文😀", | |
| "<script>alert(1)</script>", | |
| "x".repeat(2048), | |
| ]) { | |
| const a = predict(model, text, true), | |
| b = predict(model, text, false); | |
| assert.equal(a.probabilities.length, 77); | |
| assert.ok(Math.abs(a.probabilities.reduce((a, b) => a + b, 0) - 1) < 1e-12); | |
| assert.equal(a.predicted_label_id, b.predicted_label_id); | |
| } | |
| for (const input of ["", " ", "x".repeat(2049), "\ud800"]) | |
| assert.throws(() => predict(model, input)); | |
| for (const edit of [ | |
| (m) => (m.dimensions.classes = 76), | |
| (m) => (m.labels[1] = m.labels[0]), | |
| (m) => (m.embedding_scales[0] = -1), | |
| (m) => (m.output_bias[0] = "bad"), | |
| (m) => (m.embedding_q_base64 = "a"), | |
| (m) => (m.extra = true), | |
| ]) { | |
| const m = JSON.parse(raw); | |
| edit(m); | |
| assert.throws(() => parseModel(JSON.stringify(m))); | |
| } | |
| console.log( | |
| JSON.stringify({ | |
| status: "PASS", | |
| known_classes: 77, | |
| input_limit_utf16: 2048, | |
| calibration_preserves_argmax: true, | |
| malformed_models_rejected: 6, | |
| }), | |
| ); | |