Spaces:
Sleeping
Sleeping
Update src/services/db_client.py
Browse files- src/services/db_client.py +34 -4
src/services/db_client.py
CHANGED
|
@@ -120,17 +120,47 @@ def search_faces(idx, vec: List[float], det_score: float) -> Dict[str, Any]:
|
|
| 120 |
}
|
| 121 |
return image_map
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
results = []
|
| 126 |
-
for match in
|
| 127 |
meta = match.get("metadata", {})
|
| 128 |
results.append({
|
| 129 |
"url": meta.get("url", ""),
|
| 130 |
-
"score": round(match.get("score", 0)
|
| 131 |
"raw_score": match.get("score", 0),
|
| 132 |
"folder": meta.get("folder", "uncategorized")
|
| 133 |
})
|
|
|
|
| 134 |
return results
|
| 135 |
|
| 136 |
def merge_face_results(groups: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
|
|
| 120 |
}
|
| 121 |
return image_map
|
| 122 |
|
| 123 |
+
import numpy as np
|
| 124 |
+
|
| 125 |
+
def search_objects(idx, vec: List[float], filter_dict: dict = None) -> List[Dict[str, Any]]:
|
| 126 |
+
query_kwargs = {"vector": vec, "top_k": 50, "include_metadata": True}
|
| 127 |
+
if filter_dict:
|
| 128 |
+
query_kwargs["filter"] = filter_dict
|
| 129 |
+
|
| 130 |
+
res = idx.query(**query_kwargs)
|
| 131 |
+
matches = res.get("matches", [])
|
| 132 |
+
|
| 133 |
+
if not matches:
|
| 134 |
+
return []
|
| 135 |
+
|
| 136 |
+
# ── ENTERPRISE FIX: Dynamic Gradient Analysis ──
|
| 137 |
+
# Extract the raw scores
|
| 138 |
+
scores = [m.get("score", 0) for m in matches]
|
| 139 |
+
|
| 140 |
+
# Calculate the drop-off from the absolute best match to the 5th match
|
| 141 |
+
if len(scores) >= 5:
|
| 142 |
+
top_score = scores[0]
|
| 143 |
+
fifth_score = scores[4]
|
| 144 |
+
gradient = top_score - fifth_score
|
| 145 |
+
|
| 146 |
+
# If the highest score is mediocre AND there is no statistical "cliff",
|
| 147 |
+
# it means the AI just grabbed a random cluster of distant neighbors.
|
| 148 |
+
# This dynamically catches out-of-distribution items without hardcoding
|
| 149 |
+
# strict global cutoffs.
|
| 150 |
+
if top_score < 0.65 and gradient < 0.05:
|
| 151 |
+
return [] # System realizes it's hallucinating and returns nothing
|
| 152 |
+
|
| 153 |
+
# Proceed to map results normally...
|
| 154 |
results = []
|
| 155 |
+
for match in matches:
|
| 156 |
meta = match.get("metadata", {})
|
| 157 |
results.append({
|
| 158 |
"url": meta.get("url", ""),
|
| 159 |
+
"score": round(match.get("score", 0), 4),
|
| 160 |
"raw_score": match.get("score", 0),
|
| 161 |
"folder": meta.get("folder", "uncategorized")
|
| 162 |
})
|
| 163 |
+
|
| 164 |
return results
|
| 165 |
|
| 166 |
def merge_face_results(groups: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|