Spaces:
Sleeping
Sleeping
Create summarizer.py
Browse files- summarizer.py +27 -0
summarizer.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer, util
|
| 2 |
+
import json, os
|
| 3 |
+
|
| 4 |
+
class MemorySummarizer:
|
| 5 |
+
def __init__(self, memory_path="semantic_memory.json"):
|
| 6 |
+
self.memory_path = memory_path
|
| 7 |
+
self.model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
+
|
| 9 |
+
def summarize(self):
|
| 10 |
+
if not os.path.exists(self.memory_path):
|
| 11 |
+
return "No stored memories yet."
|
| 12 |
+
|
| 13 |
+
with open(self.memory_path, "r") as f:
|
| 14 |
+
data = json.load(f)
|
| 15 |
+
|
| 16 |
+
memories = [d["text"] for d in data]
|
| 17 |
+
if not memories:
|
| 18 |
+
return "Memory empty."
|
| 19 |
+
|
| 20 |
+
joined = " ".join(memories)
|
| 21 |
+
sentences = [s.strip() for s in joined.split(".") if s.strip()]
|
| 22 |
+
embeddings = self.model.encode(sentences)
|
| 23 |
+
clusters = util.paraphrase_mining_embeddings(embeddings)
|
| 24 |
+
|
| 25 |
+
key_lines = [sentences[i] for (score, i, j) in clusters if score > 0.6][:5]
|
| 26 |
+
summary = "Here’s what I know about you:\n- " + "\n- ".join(key_lines)
|
| 27 |
+
return summary
|