File size: 1,058 Bytes
28e3615 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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");
|