Spaces:
Running
Running
File size: 1,172 Bytes
e176226 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // Node smoke: runs REAL inference with onnxruntime-node (not WASM/browser). Does not replace browser verification.
import { pipeline, env, RawImage } from '@huggingface/transformers';
import { readFileSync } from 'node:fs';
import { MODEL_ID, MODEL_REVISION, MODEL_DTYPE, EXAMPLES } from '../src/config.js';
env.allowLocalModels = false;
const t0 = performance.now();
const det = await pipeline('zero-shot-object-detection', MODEL_ID, { revision: MODEL_REVISION, dtype: MODEL_DTYPE, device: 'cpu' });
console.log('load_ms', Math.round(performance.now() - t0));
for (const ex of EXAMPLES) {
const img = await RawImage.read(new URL('../public/examples/' + ex.file, import.meta.url).pathname);
const t = performance.now();
const out = await det(img, ex.labels, { threshold: ex.threshold, percentage: false });
const ms = Math.round(performance.now() - t);
const counts = {};
for (const d of out) counts[d.label] = (counts[d.label] || 0) + 1;
console.log(JSON.stringify({ id: ex.id, size: [img.width, img.height], ms, n: out.length, counts, top: out.slice(0, 4).map(d => ({ l: d.label, s: +d.score.toFixed(3), b: Object.values(d.box).map(Math.round) })) }));
}
|