Spaces:
Sleeping
Sleeping
| from sentence_transformers import SentenceTransformer | |
| from sklearn.cluster import KMeans | |
| from memory_utils import get_history, decrypt_data | |
| import numpy as np | |
| class QuantumLearner: | |
| def __init__(self): | |
| self.model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2') | |
| self.model.max_seq_length = 256 # Optimizacija za performanse | |
| def analyze_conversations(self, history_limit=1000): | |
| """Analizira povijest razgovora i identificira ključne teme""" | |
| try: | |
| history = get_history(limit=history_limit) | |
| if not history: | |
| return {"info": "Nema dostupne povijesti razgovora"} | |
| # Priprema i dekriptiranje poruka | |
| texts = [ | |
| f"{decrypt_data(row[2])} → {decrypt_data(row[3])}" | |
| for row in history | |
| if len(row) >= 4 # Zaštita od nepotpunih podataka | |
| ] | |
| # Vektorizacija teksta | |
| embeddings = self.model.encode(texts, show_progress_bar=False) | |
| # Dinamičko grupiranje (2-5 tema) | |
| optimal_clusters = min(5, max(2, len(texts)//3)) | |
| kmeans = KMeans(n_clusters=optimal_clusters).fit(embeddings) | |
| # Organizacija tema | |
| topics = {} | |
| for label, text in zip(kmeans.labels_, texts): | |
| if label not in topics: | |
| topics[label] = [] | |
| topics[label].append(text[:200]) # Skraćivanje za prikaz | |
| return { | |
| "topics": topics, | |
| "cluster_centers": kmeans.cluster_centers_.tolist() | |
| } | |
| except Exception as e: | |
| return {"error": str(e)} |