Spaces:
Running on Zero
Running on Zero
Clamp cosine scores to [-1, 1]
Browse files
app.py
CHANGED
|
@@ -286,7 +286,7 @@ def cross_modal_search(
|
|
| 286 |
dict(it, value=f"title: none | text: {it['value']}") if it["kind"] == "text" else it for it in corpus
|
| 287 |
]
|
| 288 |
c_vecs = _encode(docs, None, dim)
|
| 289 |
-
scores = c_vecs @ q_vec
|
| 290 |
order = np.argsort(-scores)
|
| 291 |
ranked = [(corpus[i], float(scores[i])) for i in order]
|
| 292 |
counts = {k: sum(1 for c in corpus if c["kind"] == k) for k in ("text", "image", "audio")}
|
|
@@ -350,7 +350,7 @@ def text_search(query: str, documents: str, task: str = "Web search") -> list[li
|
|
| 350 |
for dim in DIMS:
|
| 351 |
qd = q[:dim] / np.linalg.norm(q[:dim])
|
| 352 |
dd = d[:, :dim] / np.linalg.norm(d[:, :dim], axis=1, keepdims=True)
|
| 353 |
-
s = dd @ qd
|
| 354 |
if dim == 768:
|
| 355 |
full_scores = s
|
| 356 |
order = np.argsort(-s)
|
|
@@ -405,7 +405,7 @@ def similarity_matrix(sentences: str, task: str = "Sentence similarity", dim: in
|
|
| 405 |
if len(lines) > 16:
|
| 406 |
raise gr.Error("Use at most 16 lines.")
|
| 407 |
vecs = _encode([{"kind": "text", "value": s} for s in lines], SYMMETRIC_TASKS[task], int(dim))
|
| 408 |
-
return _heatmap_html(lines, vecs @ vecs.T)
|
| 409 |
|
| 410 |
|
| 411 |
# ----------------------------------------------------------------------------- tab 4: raw embedding (API / MCP)
|
|
|
|
| 286 |
dict(it, value=f"title: none | text: {it['value']}") if it["kind"] == "text" else it for it in corpus
|
| 287 |
]
|
| 288 |
c_vecs = _encode(docs, None, dim)
|
| 289 |
+
scores = np.clip(c_vecs @ q_vec, -1.0, 1.0) # bf16 can nudge self-matches past 1
|
| 290 |
order = np.argsort(-scores)
|
| 291 |
ranked = [(corpus[i], float(scores[i])) for i in order]
|
| 292 |
counts = {k: sum(1 for c in corpus if c["kind"] == k) for k in ("text", "image", "audio")}
|
|
|
|
| 350 |
for dim in DIMS:
|
| 351 |
qd = q[:dim] / np.linalg.norm(q[:dim])
|
| 352 |
dd = d[:, :dim] / np.linalg.norm(d[:, :dim], axis=1, keepdims=True)
|
| 353 |
+
s = np.clip(dd @ qd, -1.0, 1.0)
|
| 354 |
if dim == 768:
|
| 355 |
full_scores = s
|
| 356 |
order = np.argsort(-s)
|
|
|
|
| 405 |
if len(lines) > 16:
|
| 406 |
raise gr.Error("Use at most 16 lines.")
|
| 407 |
vecs = _encode([{"kind": "text", "value": s} for s in lines], SYMMETRIC_TASKS[task], int(dim))
|
| 408 |
+
return _heatmap_html(lines, np.clip(vecs @ vecs.T, -1.0, 1.0))
|
| 409 |
|
| 410 |
|
| 411 |
# ----------------------------------------------------------------------------- tab 4: raw embedding (API / MCP)
|