Spaces:
Running
Running
Download source/src/lib.js from Segizu/buscador-visual: direct link, hf CLI and curl.
- Browser
- Download file 1.57 kB
-
https://huggingface.co/spaces/Segizu/buscador-visual/resolve/main/source/src/lib.js
- Command line
-
hf download hf://spaces/Segizu/buscador-visual/source/src/lib.js
-
curl -L -o lib.js https://huggingface.co/spaces/Segizu/buscador-visual/resolve/main/source/src/lib.js
1.57 kB
| // Utilidades puras (testeadas con vitest). | |
| export function l2normalize(v) { | |
| let s = 0; for (let i = 0; i < v.length; i++) s += v[i] * v[i]; | |
| s = Math.sqrt(s) || 1; | |
| const out = new Float32Array(v.length); for (let i = 0; i < v.length; i++) out[i] = v[i] / s; | |
| return out; | |
| } | |
| export function dot(a, b) { let s = 0; for (let i = 0; i < a.length; i++) s += a[i] * b[i]; return s; } | |
| // Devuelve [{index, score}] ordenado de mayor a menor; excludeIndex opcional (consulta = imagen de la galería). | |
| export function rank(query, vectors, k = 12, excludeIndex = -1) { | |
| const r = []; | |
| vectors.forEach((v, i) => { if (i !== excludeIndex && v) r.push({ index: i, score: dot(query, v) }); }); | |
| r.sort((a, b) => b.score - a.score); | |
| return r.slice(0, k); | |
| } | |
| // Escala relativa 0..1 para las barras (las similitudes texto-imagen de SigLIP son pequeñas y no calibradas). | |
| export function relativeBars(results) { | |
| if (!results.length) return []; | |
| const max = results[0].score, min = results[results.length - 1].score; | |
| const span = max - min || 1; | |
| return results.map((r) => ({ ...r, bar: (r.score - min) / span })); | |
| } | |
| // Rectángulo normalizado a partir de dos puntos (cualquier orden), recortado a [0,1]. | |
| export function normRect(x0, y0, x1, y1) { | |
| const c = (v) => Math.min(1, Math.max(0, v)); | |
| const a = [c(Math.min(x0, x1)), c(Math.min(y0, y1)), c(Math.max(x0, x1)), c(Math.max(y0, y1))]; | |
| return a; | |
| } | |
| export function fitSize(w, h, maxSide) { | |
| const s = Math.min(1, maxSide / Math.max(w, h)); | |
| return [Math.max(1, Math.round(w * s)), Math.max(1, Math.round(h * s))]; | |
| } | |