Spaces:
Sleeping
Sleeping
Delete cache.py
Browse files
cache.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
import time
|
| 2 |
-
from typing import Dict, Any, List
|
| 3 |
-
from core.faiss_vector import FaissIndex
|
| 4 |
-
|
| 5 |
-
_TTL = 300 # seconds
|
| 6 |
-
_cache: Dict[str, Any] = {}
|
| 7 |
-
_faiss = None
|
| 8 |
-
|
| 9 |
-
def _now():
|
| 10 |
-
return int(time.time())
|
| 11 |
-
|
| 12 |
-
def _get_index():
|
| 13 |
-
global _faiss
|
| 14 |
-
if _faiss is None:
|
| 15 |
-
_faiss = FaissIndex()
|
| 16 |
-
return _faiss
|
| 17 |
-
|
| 18 |
-
def cache_get(key):
|
| 19 |
-
v = _cache.get(key)
|
| 20 |
-
if not v:
|
| 21 |
-
return None
|
| 22 |
-
ts, data = v
|
| 23 |
-
if _now() - ts > _TTL:
|
| 24 |
-
_cache.pop(key, None)
|
| 25 |
-
return None
|
| 26 |
-
return data
|
| 27 |
-
|
| 28 |
-
def cache_set(key, data: List[dict]):
|
| 29 |
-
_cache[key] = (_now(), data)
|
| 30 |
-
# add snippets to FAISS for local semantic recall
|
| 31 |
-
texts = [d.get("snippet","") for d in data if d.get("snippet")]
|
| 32 |
-
if texts:
|
| 33 |
-
try:
|
| 34 |
-
_get_index().add(texts)
|
| 35 |
-
except Exception:
|
| 36 |
-
pass
|
| 37 |
-
|
| 38 |
-
def dedupe(results: List[dict]) -> List[dict]:
|
| 39 |
-
seen = set()
|
| 40 |
-
out = []
|
| 41 |
-
for r in results:
|
| 42 |
-
h = hash((r.get("source"), r.get("url"), r.get("snippet")))
|
| 43 |
-
if h not in seen:
|
| 44 |
-
seen.add(h)
|
| 45 |
-
out.append(r)
|
| 46 |
-
return out
|
| 47 |
-
|
| 48 |
-
def source_counts(results: List[dict]) -> Dict[str,int]:
|
| 49 |
-
counts = {}
|
| 50 |
-
for r in results:
|
| 51 |
-
s = r.get("source","Unknown")
|
| 52 |
-
counts[s] = counts.get(s, 0) + 1
|
| 53 |
-
return counts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|