Yunpeng-26 commited on
Commit
6b232ac
·
verified ·
1 Parent(s): 535c08d

Delete inference-worker.js

Browse files
Files changed (1) hide show
  1. inference-worker.js +0 -58
inference-worker.js DELETED
@@ -1,58 +0,0 @@
1
- let runtime;
2
- let current;
3
- let currentName;
4
- const modelInfo = {
5
- resnet18: {id: "Xenova/resnet-18", task: "image-classification"},
6
- vit: {id: "Xenova/vit-base-patch16-224", task: "image-classification"},
7
- clip: {id: "Xenova/clip-vit-base-patch32", task: "zero-shot-image-classification"},
8
- };
9
- let busy = false;
10
- self.onmessage = async ({data}) => {
11
- if (busy) return;
12
- busy = true;
13
- try {
14
- const spec = modelInfo[data.model];
15
- if (!spec) throw new Error("Choose a supported model.");
16
- if (!runtime) {
17
- self.postMessage({type: "status", text: "Loading the browser AI runtime…"});
18
- runtime = await import("https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1/dist/transformers.min.js");
19
- runtime.env.allowLocalModels = false;
20
- runtime.env.backends.onnx.wasm.numThreads = 1;
21
- runtime.env.backends.onnx.wasm.proxy = false;
22
- }
23
- const cold = currentName !== data.model;
24
- if (cold) {
25
- if (current) { await current.dispose(); current = null; currentName = null; }
26
- self.postMessage({type: "status", text: "Downloading or loading " + spec.id + "…"});
27
- current = await runtime.pipeline(spec.task, spec.id, {
28
- device: "wasm", dtype: "q8",
29
- progress_callback: (p) => {
30
- if (p.status === "progress") self.postMessage({type: "progress", file: p.file, progress: p.progress, loaded: p.loaded, total: p.total});
31
- if (p.status === "done") self.postMessage({type: "status", text: "Model file ready. Initializing inference…"});
32
- },
33
- });
34
- currentName = data.model;
35
- }
36
- self.postMessage({type: "status", text: "Classifying the image on your device…"});
37
- const image = new runtime.RawImage(new Uint8ClampedArray(data.pixels), data.width, data.height, 4);
38
- const start = performance.now();
39
- let predictions = data.model === "clip"
40
- ? await current(image, data.labels, {hypothesis_template: data.template})
41
- : await current(image, {top_k: data.topk});
42
- const ms = performance.now() - start;
43
- predictions = predictions.sort((a,b) => b.score - a.score).slice(0, data.topk);
44
- self.postMessage({type: "result", result: {
45
- model: data.model, model_id: spec.id, runtime: "Transformers.js 3.8.1 / ONNX Runtime Web",
46
- device: "browser CPU (WebAssembly)", dtype: "q8", first_run_for_selected_model: cold,
47
- input_size: [data.width, data.height], label_space: data.model === "clip" ? "candidate descriptions" : "ImageNet-1K",
48
- candidate_count: data.model === "clip" ? data.labels.length : 1000,
49
- candidates: data.model === "clip" ? data.labels : null,
50
- prompt_template: data.model === "clip" ? data.template : null,
51
- inference_ms: Math.round(ms), timing_scope: "preprocessing + model inference + postprocessing; excludes model loading and interface time",
52
- predictions: predictions.map((p,i) => ({rank:i+1,label:p.label,score:p.score})),
53
- score_note: data.model === "clip" ? "Scores are relative to the candidate descriptions. Changing candidates changes scores." : "Softmax scores over 1,000 fixed classes; not calibrated correctness probabilities.",
54
- }});
55
- } catch (error) {
56
- self.postMessage({type: "error", message: String(error.message || error)});
57
- } finally { busy = false; }
58
- };