Spaces:
Sleeping
Sleeping
Create quantum_learner.py
Browse files- quantum_learner.py +21 -0
quantum_learner.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
from sklearn.cluster import KMeans
|
| 3 |
+
from memory_utils import get_history, decrypt_data
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
class QuantumLearner:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
| 9 |
+
|
| 10 |
+
def analyze_conversations(self):
|
| 11 |
+
history = get_history(limit=1000)
|
| 12 |
+
texts = [decrypt_data(row[2]) + " " + decrypt_data(row[3]) for row in history]
|
| 13 |
+
|
| 14 |
+
embeddings = self.model.encode(texts)
|
| 15 |
+
kmeans = KMeans(n_clusters=3).fit(embeddings)
|
| 16 |
+
|
| 17 |
+
topics = {}
|
| 18 |
+
for label, text in zip(kmeans.labels_, texts):
|
| 19 |
+
topics[label] = topics.get(label, []) + [text]
|
| 20 |
+
|
| 21 |
+
return {k: v[:3] for k, v in topics.items()}
|