Caveat / dev /browser-tests /web.mjs
umer-wasim's picture
Add dev/: training and export scripts, GPAI rule tests, browser tests, workspace map
28e3615 verified
Raw History Blame Contribute Delete
1.06 kB
import * as ort from "onnxruntime-web";
import fs from "fs";
ort.env.wasm.numThreads = 1;
const L = ["minimal_risk", "limited_risk", "high_risk", "prohibited"];
const probe = JSON.parse(fs.readFileSync("probe_out.json")); // token ids from transformers.js' tokenizer (== Python's)
const res = {};
for (const f of ["model_quantized.onnx", "model.onnx"]) {
const s = await ort.InferenceSession.create(fs.readFileSync(`models/eu-ai-act-classifier-v5/onnx/${f}`), { executionProviders: ["wasm"] });
const names = s.inputNames; const out = [];
for (const { ids } of probe) {
const n = ids.length, t = (a) => new ort.Tensor("int64", BigInt64Array.from(a.map(BigInt)), [1, n]);
const feed = { input_ids: t(ids), attention_mask: t(ids.map(() => 1)), token_type_ids: t(ids.map(() => 0)) };
const r = await s.run(Object.fromEntries(names.map(k => [k, feed[k]])));
const lg = Array.from(r.logits.data); out.push(L[lg.indexOf(Math.max(...lg))]);
}
res[f] = out;
}
fs.writeFileSync("web_preds.json", JSON.stringify(res));
console.log("done");