Spaces:
Sleeping
Sleeping
Create ingest/cluster.py
Browse files- ingest/cluster.py +16 -29
ingest/cluster.py
CHANGED
|
@@ -1,33 +1,20 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
try:
|
| 5 |
-
from sentence_transformers import SentenceTransformer
|
| 6 |
-
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 7 |
-
texts = [r.get("snippet", "") for r in results]
|
| 8 |
-
return model.encode(texts, show_progress_bar=False)
|
| 9 |
-
except Exception:
|
| 10 |
-
return None
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
import faiss
|
| 19 |
-
import numpy as np
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"index": i
|
| 30 |
-
})
|
| 31 |
-
return clusters
|
| 32 |
-
except Exception:
|
| 33 |
-
return {}
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 6 |
|
| 7 |
+
def semantic_cluster(results):
|
| 8 |
+
texts = [r["title"] + " " + r["snippet"] for r in results]
|
| 9 |
+
if not texts:
|
| 10 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
embeddings = model.encode(texts)
|
| 13 |
+
dim = embeddings.shape[1]
|
| 14 |
+
index = faiss.IndexFlatL2(dim)
|
| 15 |
+
index.add(np.array(embeddings))
|
| 16 |
|
| 17 |
+
return {
|
| 18 |
+
"points": embeddings.tolist(),
|
| 19 |
+
"labels": list(range(len(results)))
|
| 20 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|