Spaces:
Running
Running
Download source/src/worker.js from Segizu/buscador-visual: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/spaces/Segizu/buscador-visual/resolve/main/source/src/worker.js
- Command line
-
hf download hf://spaces/Segizu/buscador-visual/source/src/worker.js
-
curl -L -o worker.js https://huggingface.co/spaces/Segizu/buscador-visual/resolve/main/source/src/worker.js
3.14 kB
| // Web Worker: carga SigLIP 2 (torres de visi贸n y de texto por separado) y calcula embeddings. Nada sale del navegador. | |
| import { AutoTokenizer, AutoProcessor, SiglipTextModel, SiglipVisionModel, RawImage, env } from '@huggingface/transformers'; | |
| import { MODEL_ID, MODEL_REVISION, VISION_DTYPE, TEXT_DTYPE, TEXT_MAX_TOKENS } from './config.js'; | |
| env.allowLocalModels = false; | |
| env.allowRemoteModels = true; | |
| env.useBrowserCache = true; | |
| let tok, proc, textModel, visionModel, device; | |
| function configureWasm(wasmBase, wantWebGPU) { | |
| const suffix = wantWebGPU ? '.asyncify' : ''; | |
| env.backends.onnx.wasm.wasmPaths = { mjs: `${wasmBase}ort-wasm-simd-threaded${suffix}.mjs`, wasm: `${wasmBase}ort-wasm-simd-threaded${suffix}.wasm` }; | |
| const cores = self.navigator?.hardwareConcurrency || 2; | |
| env.backends.onnx.wasm.numThreads = self.crossOriginIsolated ? Math.min(4, cores) : 1; | |
| } | |
| async function load({ wantWebGPU, wasmBase }) { | |
| configureWasm(wasmBase, wantWebGPU); | |
| const progress_callback = (p) => self.postMessage({ type: 'progress', data: { status: p.status, file: p.file, loaded: p.loaded, total: p.total } }); | |
| const opts = { revision: MODEL_REVISION, progress_callback }; | |
| tok = await AutoTokenizer.from_pretrained(MODEL_ID, opts); | |
| proc = await AutoProcessor.from_pretrained(MODEL_ID, opts); | |
| const dev = wantWebGPU ? 'webgpu' : 'wasm'; | |
| try { | |
| visionModel = await SiglipVisionModel.from_pretrained(MODEL_ID, { ...opts, dtype: VISION_DTYPE, device: dev }); | |
| textModel = await SiglipTextModel.from_pretrained(MODEL_ID, { ...opts, dtype: TEXT_DTYPE, device: dev }); | |
| } catch (e) { | |
| if (wantWebGPU) { self.postMessage({ type: 'webgpu-failed', reason: String(e?.message || e).slice(0, 200) }); return; } | |
| throw e; | |
| } | |
| device = dev; | |
| self.postMessage({ type: 'ready', device, threads: env.backends.onnx.wasm.numThreads }); | |
| } | |
| function norm(arr) { let s = 0; for (const x of arr) s += x * x; s = Math.sqrt(s) || 1; return Float32Array.from(arr, (x) => x / s); } | |
| async function embedImage({ id, width, height, buffer }) { | |
| const image = new RawImage(new Uint8ClampedArray(buffer), width, height, 4).rgb(); | |
| const t0 = performance.now(); | |
| const inputs = await proc(image); | |
| const out = await visionModel(inputs); | |
| const vec = norm(out.pooler_output.data); | |
| self.postMessage({ type: 'vector', id, vec, ms: performance.now() - t0, device }, [vec.buffer]); | |
| } | |
| async function embedText({ id, text }) { | |
| const t0 = performance.now(); | |
| const inputs = tok([text], { padding: 'max_length', max_length: TEXT_MAX_TOKENS, truncation: true }); | |
| const out = await textModel(inputs); | |
| const vec = norm(out.pooler_output.data); | |
| self.postMessage({ type: 'vector', id, vec, ms: performance.now() - t0, device }, [vec.buffer]); | |
| } | |
| self.addEventListener('message', async (ev) => { | |
| const msg = ev.data || {}; | |
| try { | |
| if (msg.type === 'load') await load(msg); | |
| else if (msg.type === 'image') await embedImage(msg); | |
| else if (msg.type === 'text') await embedText(msg); | |
| } catch (e) { | |
| self.postMessage({ type: 'error', id: msg.id, stage: msg.type, message: String(e?.message || e).slice(0, 400) }); | |
| } | |
| }); | |