Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,11 +11,6 @@ Overview
|
|
| 11 |
- "/chat" : Simple chat UI powered by Groq API
|
| 12 |
- "/api/*" : Chat and speech-to-text helpers
|
| 13 |
|
| 14 |
-
Important
|
| 15 |
-
- Accuracy depends on the quality and consistency of notebook embeddings. The query
|
| 16 |
-
embedding here is a lightweight placeholder to keep the system running without
|
| 17 |
-
heavy models. Replace `embed_query_vector()` later with the same embedding model
|
| 18 |
-
used in the notebook for best results.
|
| 19 |
"""
|
| 20 |
|
| 21 |
from __future__ import annotations
|
|
@@ -222,9 +217,9 @@ def bootstrap_artifacts() -> None:
|
|
| 222 |
with open(LABELS_JSON, "r", encoding="utf-8") as f:
|
| 223 |
labels = json.load(f)
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
|
| 229 |
app.logger.info(
|
| 230 |
"[bootstrap] gallery=%s labels=%d threshold=%.4f",
|
|
@@ -377,15 +372,18 @@ def verify_face_identity(user_name: str, image_bytes: bytes) -> dict:
|
|
| 377 |
g = G[idx].astype("float32")
|
| 378 |
|
| 379 |
# Cosine distance decision
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
|
| 391 |
@app.post("/verify")
|
|
|
|
| 11 |
- "/chat" : Simple chat UI powered by Groq API
|
| 12 |
- "/api/*" : Chat and speech-to-text helpers
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
from __future__ import annotations
|
|
|
|
| 217 |
with open(LABELS_JSON, "r", encoding="utf-8") as f:
|
| 218 |
labels = json.load(f)
|
| 219 |
|
| 220 |
+
with open(THRESH_JSON, "r", encoding="utf-8") as f:
|
| 221 |
+
config = json.load(f)
|
| 222 |
+
COSINE_SIM_THRESHOLD = float(config.get("cosine_threshold", COSINE_SIM_THRESHOLD))
|
| 223 |
|
| 224 |
app.logger.info(
|
| 225 |
"[bootstrap] gallery=%s labels=%d threshold=%.4f",
|
|
|
|
| 372 |
g = G[idx].astype("float32")
|
| 373 |
|
| 374 |
# Cosine distance decision
|
| 375 |
+
dist = cosine_distance(q, g) # distance (0 = identical, 1 = different)
|
| 376 |
+
sim = 1.0 - dist # convert back to similarity
|
| 377 |
+
|
| 378 |
+
accepted = sim >= COSINE_SIM_THRESHOLD
|
| 379 |
+
|
| 380 |
+
return {
|
| 381 |
+
"ok": bool(accepted),
|
| 382 |
+
"score": float(sim), # now showing similarity
|
| 383 |
+
"threshold": float(COSINE_SIM_THRESHOLD),
|
| 384 |
+
"reason": None if accepted else "Not within threshold",
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
|
| 388 |
|
| 389 |
@app.post("/verify")
|