Spaces:
Running
Running
Update modules/treinamento.py
Browse files- modules/treinamento.py +315 -417
modules/treinamento.py
CHANGED
|
@@ -1,417 +1,315 @@
|
|
| 1 |
-
# treinamento.py
|
| 2 |
-
import threading
|
| 3 |
-
import time
|
| 4 |
-
import logging
|
| 5 |
-
import sqlite3
|
| 6 |
-
import re
|
| 7 |
-
import json
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
self.db.
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
return girias_novas
|
| 317 |
-
|
| 318 |
-
def _eh_giria_potencial(self, palavra: str, mensagens: List[tuple], threshold: int = 3) -> bool:
|
| 319 |
-
"""Verifica se uma palavra é uma gíria potencial baseada na frequência"""
|
| 320 |
-
count = 0
|
| 321 |
-
for mensagem, resposta in mensagens:
|
| 322 |
-
texto = (mensagem or '') + ' ' + (resposta or '')
|
| 323 |
-
count += texto.lower().count(palavra.lower())
|
| 324 |
-
|
| 325 |
-
return count >= threshold
|
| 326 |
-
|
| 327 |
-
def _inferir_significado_giria(self, giria: str, mensagens: List[tuple]) -> Optional[str]:
|
| 328 |
-
"""Tenta inferir o significado de uma gíria baseada no contexto"""
|
| 329 |
-
contextos = []
|
| 330 |
-
|
| 331 |
-
for mensagem, resposta in mensagens:
|
| 332 |
-
texto = (mensagem or '') + ' ' + (resposta or '')
|
| 333 |
-
if giria.lower() in texto.lower():
|
| 334 |
-
# Extrair contexto ao redor da gíria
|
| 335 |
-
palavras = texto.split()
|
| 336 |
-
try:
|
| 337 |
-
idx = next(i for i, p in enumerate(palavras) if p.lower() == giria.lower())
|
| 338 |
-
contexto = ' '.join(palavras[max(0, idx-3):idx+4])
|
| 339 |
-
contextos.append(contexto)
|
| 340 |
-
except (StopIteration, ValueError):
|
| 341 |
-
continue
|
| 342 |
-
|
| 343 |
-
if not contextos:
|
| 344 |
-
return None
|
| 345 |
-
|
| 346 |
-
# Análise simples: se aparece com exclamação, pode ser admiração
|
| 347 |
-
if any('!' in ctx for ctx in contextos):
|
| 348 |
-
return "expressão de admiração ou surpresa"
|
| 349 |
-
|
| 350 |
-
# Se aparece com interrogação, pode ser dúvida
|
| 351 |
-
if any('?' in ctx for ctx in contextos):
|
| 352 |
-
return "expressão de dúvida ou confusão"
|
| 353 |
-
|
| 354 |
-
# Default: expressão emocional
|
| 355 |
-
return "expressão emocional ou gíria local"
|
| 356 |
-
|
| 357 |
-
def _extrair_contexto_giria(self, giria: str, mensagens: List[tuple]) -> str:
|
| 358 |
-
"""Extrai contexto de uso da gíria"""
|
| 359 |
-
contextos = []
|
| 360 |
-
for mensagem, resposta in mensagens:
|
| 361 |
-
texto = (mensagem or '') + ' ' + (resposta or '')
|
| 362 |
-
if giria.lower() in texto.lower():
|
| 363 |
-
contextos.append(texto[:100] + '...' if len(texto) > 100 else texto)
|
| 364 |
-
|
| 365 |
-
return '; '.join(contextos[:3]) # Limitar a 3 exemplos
|
| 366 |
-
|
| 367 |
-
def _salvar_aprendizados_usuario(self, numero_usuario: str, analise_emocional: Dict[str, Any],
|
| 368 |
-
tom_predominante: str, girias_aprendidas: List[Dict[str, Any]]):
|
| 369 |
-
"""Salva os aprendizados específicos do usuário"""
|
| 370 |
-
try:
|
| 371 |
-
# Salvar emoção predominante
|
| 372 |
-
self.db.salvar_aprendizado_detalhado(numero_usuario, 'emocao_predominante',
|
| 373 |
-
analise_emocional['emocao_predominante'])
|
| 374 |
-
|
| 375 |
-
# Salvar intensidade emocional
|
| 376 |
-
self.db.salvar_aprendizado_detalhado(numero_usuario, 'intensidade_emocional',
|
| 377 |
-
str(analise_emocional['intensidade_media']))
|
| 378 |
-
|
| 379 |
-
# Salvar tom predominante
|
| 380 |
-
self.db.registrar_tom_usuario(numero_usuario, tom_predominante,
|
| 381 |
-
analise_emocional['intensidade_media'])
|
| 382 |
-
|
| 383 |
-
# Salvar distribuição de emoções
|
| 384 |
-
self.db.salvar_aprendizado_detalhado(numero_usuario, 'distribuicao_emocoes',
|
| 385 |
-
json.dumps(analise_emocional['distribuicao_emocoes']))
|
| 386 |
-
|
| 387 |
-
logger.info(f'Aprendizados salvos para usuário {numero_usuario}: emoção={analise_emocional["emocao_predominante"]}, tom={tom_predominante}')
|
| 388 |
-
|
| 389 |
-
except Exception as e:
|
| 390 |
-
logger.warning(f'Erro ao salvar aprendizados do usuário {numero_usuario}: {e}')
|
| 391 |
-
|
| 392 |
-
def _run_loop(self):
|
| 393 |
-
interval = max(1, self.interval_hours) * 3600
|
| 394 |
-
logger.info(f'Treinamento periódico iniciado (interval_hours={self.interval_hours})')
|
| 395 |
-
while self._running:
|
| 396 |
-
try:
|
| 397 |
-
self.train_once()
|
| 398 |
-
except Exception as e:
|
| 399 |
-
logger.exception(f'Erro no loop de treinamento: {e}')
|
| 400 |
-
# dormir pelo intervalo configurado
|
| 401 |
-
for _ in range(int(interval)):
|
| 402 |
-
if not self._running:
|
| 403 |
-
break
|
| 404 |
-
time.sleep(1)
|
| 405 |
-
logger.info('Treinamento periódico finalizado.')
|
| 406 |
-
|
| 407 |
-
def start_periodic_training(self):
|
| 408 |
-
if self._running:
|
| 409 |
-
return
|
| 410 |
-
self._running = True
|
| 411 |
-
self._thread = threading.Thread(target=self._run_loop, daemon=True)
|
| 412 |
-
self._thread.start()
|
| 413 |
-
|
| 414 |
-
def stop(self):
|
| 415 |
-
self._running = False
|
| 416 |
-
if self._thread:
|
| 417 |
-
self._thread.join(timeout=5)
|
|
|
|
| 1 |
+
# treinamento.py
|
| 2 |
+
import threading
|
| 3 |
+
import time
|
| 4 |
+
import logging
|
| 5 |
+
import sqlite3
|
| 6 |
+
import re
|
| 7 |
+
import json
|
| 8 |
+
import collections
|
| 9 |
+
from typing import Optional, Any, List, Dict, Tuple
|
| 10 |
+
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
# MODELO MAIS PESADO E ROBUSTO: paraphrase-multilingual-mpnet-base-v2
|
| 14 |
+
# - 110M parâmetros
|
| 15 |
+
# - Suporta 50+ idiomas (inclui português, gírias, sotaques)
|
| 16 |
+
# - Excelente em: semântica, intenção, emoção, ironia, contexto
|
| 17 |
+
# - Ideal para bots com "alma humana"
|
| 18 |
+
try:
|
| 19 |
+
from sentence_transformers import SentenceTransformer
|
| 20 |
+
# Força o modelo mais poderoso
|
| 21 |
+
MODEL_NAME = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
| 22 |
+
logger.info(f"Carregando modelo pesado: {MODEL_NAME}")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
logger.warning(f"sentence_transformers não disponível: {e}")
|
| 25 |
+
SentenceTransformer = None
|
| 26 |
+
MODEL_NAME = None
|
| 27 |
+
|
| 28 |
+
# Listas expandidas para análise emocional + gírias angolanas
|
| 29 |
+
PALAVRAS_POSITIVAS = [
|
| 30 |
+
'bom', 'ótimo', 'incrível', 'maravilhoso', 'feliz', 'alegre', 'amor', 'gostar', 'adorei',
|
| 31 |
+
'top', 'show', 'legal', 'bacana', 'fixe', 'bué', 'oroh', 'máximo', 'perfeito', 'genial',
|
| 32 |
+
'divertido', 'hilário', 'gargalhada', 'rsrs', 'kkk', 'lol', 'haha', 'amo', 'adoro'
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
PALAVRAS_NEGATIVAS = [
|
| 36 |
+
'ruim', 'péssimo', 'horrível', 'triste', 'ódio', 'raiva', 'chateado', 'detesto', 'odeio',
|
| 37 |
+
'merda', 'porra', 'caralho', 'puto', 'foda-se', 'tristeza', 'depressão', 'sofrimento',
|
| 38 |
+
'choro', 'lágrima', 'dor', 'sofrer', 'fracasso', 'perdi', 'derrota'
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
GIRIAS_ANGOLANAS = [
|
| 42 |
+
'mano', 'puto', 'kkk', 'rsrs', 'lol', 'tô', 'cê', 'num', 'tipo', 'né', 'bah', 'uai',
|
| 43 |
+
'oxe', 'eita', 'caramba', 'pqp', 'fdp', 'vsf', 'mlk', 'mwangolé', 'kota', 'mané',
|
| 44 |
+
'oroh', 'bué', 'fixe', 'kota', 'baza', 'bazar', 'bazar fora', 'está fixe', 'está bué'
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
PALAVRAS_RUDES = [
|
| 48 |
+
'puto', 'merda', 'porra', 'caralho', 'cacete', 'fdp', 'vsf', 'mlk', 'arrombado',
|
| 49 |
+
'viado', 'bicha', 'cu', 'buceta', 'rola', 'pau', 'bunda', 'peito', 'teta', 'bct',
|
| 50 |
+
'pnc', 'pnctl', 'fuder', 'foder', 'transar', 'comer', 'chupar', 'mamada', 'boquete',
|
| 51 |
+
'punheta', 'gozar', 'pqp', 'vai tomar no cu', 'vai se foder', 'seu filho da puta'
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class Treinamento:
|
| 56 |
+
"""
|
| 57 |
+
Treinamento com o modelo MAIS PESADO E HUMANO:
|
| 58 |
+
- paraphrase-multilingual-mpnet-base-v2
|
| 59 |
+
- Aprendizado em tempo real + periódico
|
| 60 |
+
- Detecta: intenção, emoção, ironia, gírias, tom, contexto
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
def __init__(self, db, contexto: Optional[Any] = None, interval_hours: int = 1):
|
| 64 |
+
self.db = db
|
| 65 |
+
self.contexto = contexto
|
| 66 |
+
self.interval_hours = interval_hours
|
| 67 |
+
self._thread = None
|
| 68 |
+
self._running = False
|
| 69 |
+
self._model = None
|
| 70 |
+
self.privileged_users = ['244937035662', 'isaac', 'isaac quarenta', 'ceo', 'fundador']
|
| 71 |
+
|
| 72 |
+
# ================================================================
|
| 73 |
+
# CARREGAMENTO DO MODELO PESADO (com fallback)
|
| 74 |
+
# ================================================================
|
| 75 |
+
|
| 76 |
+
def _ensure_model(self):
|
| 77 |
+
if self._model is not None:
|
| 78 |
+
return
|
| 79 |
+
if self.contexto and hasattr(self.contexto, 'model') and self.contexto.model:
|
| 80 |
+
self._model = self.contexto.model
|
| 81 |
+
return
|
| 82 |
+
if SentenceTransformer is None or MODEL_NAME is None:
|
| 83 |
+
logger.warning("Modelo pesado não disponível. Usando análise heurística.")
|
| 84 |
+
return
|
| 85 |
+
try:
|
| 86 |
+
logger.info(f"Carregando modelo pesado: {MODEL_NAME} (pode demorar 10-20s)...")
|
| 87 |
+
self._model = SentenceTransformer(MODEL_NAME)
|
| 88 |
+
logger.info("Modelo pesado carregado com sucesso! Akira agora é mais humana.")
|
| 89 |
+
except Exception as e:
|
| 90 |
+
logger.error(f"Falha ao carregar modelo pesado: {e}")
|
| 91 |
+
self._model = None
|
| 92 |
+
|
| 93 |
+
# ================================================================
|
| 94 |
+
# APRENDIZADO EM TEMPO REAL
|
| 95 |
+
# ================================================================
|
| 96 |
+
|
| 97 |
+
def registrar_interacao(self, usuario: str, mensagem: str, resposta: str, numero: str = '', is_reply: bool = False, mensagem_original: str = ''):
|
| 98 |
+
"""Registra + aprende na hora com modelo pesado"""
|
| 99 |
+
try:
|
| 100 |
+
self.db.salvar_mensagem(usuario, mensagem, resposta, numero, is_reply, mensagem_original)
|
| 101 |
+
self._aprender_em_tempo_real(numero, mensagem, resposta)
|
| 102 |
+
logger.info(f"Interação aprendida em tempo real: {numero}")
|
| 103 |
+
except Exception as e:
|
| 104 |
+
logger.warning(f'Erro ao registrar: {e}')
|
| 105 |
+
|
| 106 |
+
def _aprender_em_tempo_real(self, numero: str, msg: str, resp: str):
|
| 107 |
+
if not numero or numero == 'unknown':
|
| 108 |
+
return
|
| 109 |
+
|
| 110 |
+
texto = f"{msg} {resp}".lower()
|
| 111 |
+
|
| 112 |
+
# === ANÁLISE COM MODELO PESADO (se disponível) ===
|
| 113 |
+
self._ensure_model()
|
| 114 |
+
if self._model:
|
| 115 |
+
try:
|
| 116 |
+
# Embedding da mensagem completa
|
| 117 |
+
emb = self._model.encode(texto).tobytes()
|
| 118 |
+
self.db.salvar_embedding(texto, emb)
|
| 119 |
+
|
| 120 |
+
# Similaridade com frases emocionais (exemplo)
|
| 121 |
+
frases_emocao = {
|
| 122 |
+
"feliz": "estou muito feliz hoje",
|
| 123 |
+
"triste": "estou muito triste e sozinho",
|
| 124 |
+
"raiva": "estou puto com tudo",
|
| 125 |
+
"amor": "eu te amo muito"
|
| 126 |
+
}
|
| 127 |
+
embs_ref = self._model.encode(list(frases_emocao.values()))
|
| 128 |
+
sims = self._model.encode(texto) @ embs_ref.T
|
| 129 |
+
emocao_pred = list(frases_emocao.keys())[sims.argmax()]
|
| 130 |
+
intensidade = float(sims.max())
|
| 131 |
+
self.db.salvar_aprendizado_detalhado(numero, "emocao_ia", json.dumps({
|
| 132 |
+
"emocao": emocao_pred,
|
| 133 |
+
"intensidade": intensidade,
|
| 134 |
+
"fonte": "mpnet"
|
| 135 |
+
}))
|
| 136 |
+
except Exception as e:
|
| 137 |
+
logger.warning(f"Erro no modelo pesado: {e}")
|
| 138 |
+
|
| 139 |
+
# === ANÁLISE HEURÍSTICA (sempre) ===
|
| 140 |
+
rude = any(p in texto for p in PALAVRAS_RUDES)
|
| 141 |
+
tom = 'rude' if rude else 'casual'
|
| 142 |
+
|
| 143 |
+
palavras = [p for p in re.findall(r'\b\w{4,}\b', texto)
|
| 144 |
+
if p not in {'não', 'que', 'com', 'pra', 'pro', 'uma', 'ele', 'ela', 'isso'}]
|
| 145 |
+
contador = collections.Counter(palavras)
|
| 146 |
+
top_girias = [w for w, c in contador.most_common(5) if c > 1]
|
| 147 |
+
|
| 148 |
+
# Salvar tom
|
| 149 |
+
intensidade_tom = 0.8 if rude else 0.5
|
| 150 |
+
self.db.registrar_tom_usuario(numero, tom, intensidade_tom, texto[:100])
|
| 151 |
+
|
| 152 |
+
# Salvar gírias
|
| 153 |
+
for giria in top_girias:
|
| 154 |
+
significado = "gíria agressiva" if rude else "gíria local"
|
| 155 |
+
self.db.salvar_giria_aprendida(numero, giria, significado, texto[:100])
|
| 156 |
+
|
| 157 |
+
# ================================================================
|
| 158 |
+
# TREINAMENTO PERIÓDICO (a cada hora)
|
| 159 |
+
# ================================================================
|
| 160 |
+
|
| 161 |
+
def train_once(self):
|
| 162 |
+
logger.info("Iniciando treinamento periódico com modelo pesado...")
|
| 163 |
+
data = self._fetch_recent_data(limit=1000)
|
| 164 |
+
if not data:
|
| 165 |
+
logger.info("Nenhum dado para treinar.")
|
| 166 |
+
self._salvar_ultimo_treino()
|
| 167 |
+
return
|
| 168 |
+
|
| 169 |
+
usuarios = set(row[1] for row in data if row[1] and row[1].startswith('244'))
|
| 170 |
+
for numero in usuarios:
|
| 171 |
+
msgs = self._fetch_user_messages(numero, limit=50)
|
| 172 |
+
if len(msgs) < 3:
|
| 173 |
+
continue
|
| 174 |
+
|
| 175 |
+
analise = self._analisar_com_mpnet(msgs) if self._model else self._analisar_heuristica(msgs)
|
| 176 |
+
tom = self._detectar_tom_usuario(msgs, numero)
|
| 177 |
+
|
| 178 |
+
self.db.salvar_aprendizado_detalhado(numero, 'emocao_predominante', analise['emocao_predominante'])
|
| 179 |
+
self.db.salvar_aprendizado_detalhado(numero, 'intensidade_emocional', str(analise['intensidade_media']))
|
| 180 |
+
self.db.registrar_tom_usuario(numero, tom, analise['intensidade_media'])
|
| 181 |
+
|
| 182 |
+
self._gerar_embeddings_globais(data)
|
| 183 |
+
self._salvar_ultimo_treino()
|
| 184 |
+
logger.info("Treinamento concluído com sucesso.")
|
| 185 |
+
|
| 186 |
+
def _analisar_com_mpnet(self, mensagens: List[Tuple]) -> Dict:
|
| 187 |
+
"""Análise emocional com modelo pesado"""
|
| 188 |
+
textos = [f"{m} {r}" for m, r in mensagens]
|
| 189 |
+
embs = self._model.encode(textos)
|
| 190 |
+
|
| 191 |
+
# Frases de referência
|
| 192 |
+
refs = {
|
| 193 |
+
"feliz": "estou muito feliz e animado",
|
| 194 |
+
"triste": "estou triste e deprimido",
|
| 195 |
+
"raiva": "estou com raiva e irritado",
|
| 196 |
+
"amor": "eu amo e adoro essa pessoa"
|
| 197 |
+
}
|
| 198 |
+
ref_embs = self._model.encode(list(refs.values()))
|
| 199 |
+
sims = embs @ ref_embs.T
|
| 200 |
+
emocoes = [list(refs.keys())[i] for i in sims.argmax(axis=1)]
|
| 201 |
+
intensidades = sims.max(axis=1)
|
| 202 |
+
|
| 203 |
+
counter = collections.Counter(emocoes)
|
| 204 |
+
return {
|
| 205 |
+
'emocao_predominante': counter.most_common(1)[0][0],
|
| 206 |
+
'intensidade_media': float(intensidades.mean())
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
def _analisar_heuristica(self, mensagens: List[Tuple]) -> Dict:
|
| 210 |
+
counter = collections.Counter()
|
| 211 |
+
intensidade = 0
|
| 212 |
+
total = len(mensagens)
|
| 213 |
+
for msg, resp in mensagens:
|
| 214 |
+
texto = (msg or '') + ' ' + (resp or '')
|
| 215 |
+
analise = self.db.analisar_emocoes_mensagem(texto)
|
| 216 |
+
counter[analise['emocao']] += 1
|
| 217 |
+
intensidade += analise['intensidade']
|
| 218 |
+
return {
|
| 219 |
+
'emocao_predominante': counter.most_common(1)[0][0] if counter else 'neutro',
|
| 220 |
+
'intensidade_media': intensidade / total if total > 0 else 0
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
def _detectar_tom_usuario(self, mensagens: List[Tuple], numero: str) -> str:
|
| 224 |
+
if numero in self.privileged_users:
|
| 225 |
+
return 'formal'
|
| 226 |
+
counter = collections.Counter()
|
| 227 |
+
for msg, _ in mensagens:
|
| 228 |
+
msg_lower = (msg or '').lower()
|
| 229 |
+
if any(p in msg_lower for p in PALAVRAS_RUDES):
|
| 230 |
+
counter['rude'] += 1
|
| 231 |
+
elif any(p in msg_lower for p in ['por favor', 'obrigado', 'senhor']):
|
| 232 |
+
counter['formal'] += 1
|
| 233 |
+
elif any(p in msg_lower for p in GIRIAS_ANGOLANAS):
|
| 234 |
+
counter['casual'] += 1
|
| 235 |
+
else:
|
| 236 |
+
counter['neutro'] += 1
|
| 237 |
+
return counter.most_common(1)[0][0] if counter else 'neutro'
|
| 238 |
+
|
| 239 |
+
def _gerar_embeddings_globais(self, data: List[Tuple]):
|
| 240 |
+
if not self._model:
|
| 241 |
+
return
|
| 242 |
+
sentences = [row[2] for row in data if row[2] and len(row[2]) > 10][:256]
|
| 243 |
+
try:
|
| 244 |
+
embeddings = self._model.encode(sentences)
|
| 245 |
+
for s, emb in zip(sentences, embeddings):
|
| 246 |
+
self.db.salvar_embedding(s, emb.tobytes())
|
| 247 |
+
except Exception as e:
|
| 248 |
+
logger.warning(f'Erro ao gerar embeddings globais: {e}')
|
| 249 |
+
|
| 250 |
+
def _fetch_recent_data(self, limit=1000) -> List[Tuple]:
|
| 251 |
+
rows = []
|
| 252 |
+
try:
|
| 253 |
+
conn = sqlite3.connect(self.db.db_path)
|
| 254 |
+
c = conn.cursor()
|
| 255 |
+
c.execute('''
|
| 256 |
+
SELECT usuario, numero, mensagem, resposta
|
| 257 |
+
FROM mensagens
|
| 258 |
+
WHERE resposta IS NOT NULL AND resposta != ''
|
| 259 |
+
AND numero IS NOT NULL AND numero != '' AND numero != 'unknown'
|
| 260 |
+
AND LENGTH(numero) >= 10 AND numero LIKE '244%'
|
| 261 |
+
ORDER BY id DESC LIMIT ?
|
| 262 |
+
''', (limit,))
|
| 263 |
+
rows = c.fetchall()
|
| 264 |
+
conn.close()
|
| 265 |
+
except Exception as e:
|
| 266 |
+
logger.error(f'Erro ao buscar dados: {e}')
|
| 267 |
+
return rows
|
| 268 |
+
|
| 269 |
+
def _fetch_user_messages(self, numero: str, limit: int = 50) -> List[Tuple]:
|
| 270 |
+
rows = []
|
| 271 |
+
try:
|
| 272 |
+
conn = sqlite3.connect(self.db.db_path)
|
| 273 |
+
c = conn.cursor()
|
| 274 |
+
c.execute('SELECT mensagem, resposta FROM mensagens WHERE numero=? ORDER BY id DESC LIMIT ?', (numero, limit))
|
| 275 |
+
rows = c.fetchall()
|
| 276 |
+
conn.close()
|
| 277 |
+
except Exception as e:
|
| 278 |
+
logger.error(f'Erro ao buscar mensagens do usuário {numero}: {e}')
|
| 279 |
+
return rows
|
| 280 |
+
|
| 281 |
+
def _salvar_ultimo_treino(self):
|
| 282 |
+
try:
|
| 283 |
+
self.db.salvar_info_geral('ultimo_treino', str(time.time()))
|
| 284 |
+
except:
|
| 285 |
+
pass
|
| 286 |
+
|
| 287 |
+
# ================================================================
|
| 288 |
+
# LOOP DE TREINAMENTO
|
| 289 |
+
# ================================================================
|
| 290 |
+
|
| 291 |
+
def _run_loop(self):
|
| 292 |
+
interval = max(1, self.interval_hours) * 3600
|
| 293 |
+
logger.info(f"Treinamento periódico iniciado (a cada {self.interval_hours}h)")
|
| 294 |
+
while self._running:
|
| 295 |
+
try:
|
| 296 |
+
self.train_once()
|
| 297 |
+
except Exception as e:
|
| 298 |
+
logger.exception(f"Erro no treinamento: {e}")
|
| 299 |
+
for _ in range(int(interval)):
|
| 300 |
+
if not self._running:
|
| 301 |
+
break
|
| 302 |
+
time.sleep(1)
|
| 303 |
+
logger.info("Treinamento periódico parado.")
|
| 304 |
+
|
| 305 |
+
def start_periodic_training(self):
|
| 306 |
+
if self._running:
|
| 307 |
+
return
|
| 308 |
+
self._running = True
|
| 309 |
+
self._thread = threading.Thread(target=self._run_loop, daemon=True)
|
| 310 |
+
self._thread.start()
|
| 311 |
+
|
| 312 |
+
def stop(self):
|
| 313 |
+
self._running = False
|
| 314 |
+
if self._thread:
|
| 315 |
+
self._thread.join(timeout=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|