Spaces:
Sleeping
Sleeping
| from sentence_transformers import SentenceTransformer | |
| import json, os, numpy as np | |
| class MemorySummarizer: | |
| def __init__(self, memory_path="semantic_memory.json"): | |
| self.memory_path = memory_path | |
| self.model = SentenceTransformer("all-MiniLM-L6-v2") | |
| def summarize(self): | |
| if not os.path.exists(self.memory_path): | |
| return "No memory yet." | |
| with open(self.memory_path, "r") as f: | |
| data = json.load(f) | |
| texts = [d["text"] for d in data] | |
| if not texts: | |
| return "Memory empty." | |
| embeddings = self.model.encode(texts) | |
| centroid = np.mean(embeddings, axis=0) | |
| sims = np.dot(embeddings, centroid) / ( | |
| np.linalg.norm(embeddings, axis=1) * np.linalg.norm(centroid) | |
| ) | |
| top = np.argsort(sims)[-5:][::-1] | |
| key_lines = [texts[i] for i in top] | |
| return "Here’s what I know about you:\n- " + "\n- ".join(key_lines) |