File size: 12,588 Bytes
824bf31 9730fbc 824bf31 |
1 2 3 4 5 6 7 8 9 10 11 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 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
#!/usr/bin/env python3
"""
Integração completa entre Cidadão.AI e Hugging Face Hub
Este módulo facilita a integração entre o modelo especializado
e a biblioteca transformers do Hugging Face.
"""
import os
import sys
import torch
import logging
from pathlib import Path
from typing import Dict, List, Optional, Union, Tuple
from transformers import (
AutoModel, AutoTokenizer, AutoConfig,
pipeline, Pipeline
)
from src.core import json_utils
# Adicionar src ao path
sys.path.append(str(Path(__file__).parent.parent))
from src.ml.hf_cidadao_model import (
CidadaoAIConfig, CidadaoAIModel,
TransparencyAnalysisPipeline,
create_cidadao_pipeline,
analyze_transparency
)
logger = logging.getLogger(__name__)
class CidadaoAIHubManager:
"""Gerenciador de integração com Hugging Face Hub"""
def __init__(
self,
model_name: str = "neural-thinker/cidadao-gpt",
cache_dir: Optional[str] = None,
use_auth_token: Optional[str] = None
):
self.model_name = model_name
self.cache_dir = cache_dir
self.use_auth_token = use_auth_token or os.getenv("HUGGINGFACE_HUB_TOKEN")
self.model = None
self.tokenizer = None
self.pipeline = None
self.config = None
# Setup logging
logging.basicConfig(level=logging.INFO)
def load_from_hub(self) -> bool:
"""Carregar modelo do Hugging Face Hub"""
try:
logger.info(f"🔄 Carregando Cidadão.AI de {self.model_name}...")
# Carregar configuração
self.config = AutoConfig.from_pretrained(
self.model_name,
cache_dir=self.cache_dir,
use_auth_token=self.use_auth_token
)
# Carregar tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_name,
cache_dir=self.cache_dir,
use_auth_token=self.use_auth_token
)
# Carregar modelo
self.model = AutoModel.from_pretrained(
self.model_name,
config=self.config,
cache_dir=self.cache_dir,
use_auth_token=self.use_auth_token
)
# Criar pipeline especializado
self.pipeline = TransparencyAnalysisPipeline(
model=self.model,
tokenizer=self.tokenizer,
task="transparency-analysis"
)
logger.info("✅ Modelo carregado com sucesso do Hugging Face Hub")
return True
except Exception as e:
logger.error(f"❌ Erro ao carregar do Hub: {e}")
logger.info("🔄 Tentando carregar modelo local...")
return self._load_local_fallback()
def _load_local_fallback(self) -> bool:
"""Fallback para modelo local se Hub não disponível"""
try:
from src.ml.cidadao_model import create_cidadao_model
logger.info("📂 Carregando modelo local...")
# Criar modelo local
self.model = create_cidadao_model(
specialized_tasks=["all"],
model_size="medium"
)
# Usar tokenizer base
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
# Adicionar tokens especiais
special_tokens = [
"[CONTRACT]", "[ENTITY]", "[VALUE]", "[ANOMALY]",
"[LEGAL]", "[FINANCIAL]", "[CORRUPTION]", "[COMPLIANCE]"
]
self.tokenizer.add_special_tokens({
"additional_special_tokens": special_tokens
})
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
logger.info("✅ Modelo local carregado com sucesso")
return True
except Exception as e:
logger.error(f"❌ Erro ao carregar modelo local: {e}")
return False
def analyze_text(
self,
text: str,
analysis_type: str = "complete",
return_all_scores: bool = False
) -> Dict:
"""Analisar texto usando modelo Cidadão.AI"""
if not self.model:
raise RuntimeError("Modelo não carregado. Execute load_from_hub() primeiro.")
try:
if self.pipeline:
# Usar pipeline se disponível
return self.pipeline(
text,
return_all_scores=return_all_scores
)
else:
# Usar modelo diretamente
inputs = self.tokenizer(
text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=512
)
with torch.no_grad():
outputs = self.model(**inputs)
# Processar outputs
results = {}
# Anomalias
if hasattr(outputs, 'anomaly_logits') or 'anomaly_logits' in outputs:
anomaly_logits = outputs.get('anomaly_logits', outputs.anomaly_logits)
anomaly_probs = torch.softmax(anomaly_logits, dim=-1)
anomaly_pred = torch.argmax(anomaly_probs, dim=-1)
anomaly_labels = ["Normal", "Suspeito", "Anômalo"]
results["anomaly"] = {
"label": anomaly_labels[anomaly_pred.item()],
"score": anomaly_probs.max().item()
}
# Risco financeiro
if hasattr(outputs, 'financial_logits') or 'financial_logits' in outputs:
financial_logits = outputs.get('financial_logits', outputs.financial_logits)
financial_probs = torch.softmax(financial_logits, dim=-1)
financial_pred = torch.argmax(financial_probs, dim=-1)
financial_labels = ["Muito Baixo", "Baixo", "Médio", "Alto", "Muito Alto"]
results["financial"] = {
"label": financial_labels[financial_pred.item()],
"score": financial_probs.max().item()
}
# Conformidade legal
if hasattr(outputs, 'legal_logits') or 'legal_logits' in outputs:
legal_logits = outputs.get('legal_logits', outputs.legal_logits)
legal_probs = torch.softmax(legal_logits, dim=-1)
legal_pred = torch.argmax(legal_probs, dim=-1)
legal_labels = ["Não Conforme", "Conforme"]
results["legal"] = {
"label": legal_labels[legal_pred.item()],
"score": legal_probs.max().item()
}
return results
except Exception as e:
logger.error(f"❌ Erro na análise: {e}")
raise
def batch_analyze(
self,
texts: List[str],
analysis_type: str = "complete"
) -> List[Dict]:
"""Análise em lote de textos"""
results = []
for text in texts:
try:
result = self.analyze_text(text, analysis_type)
results.append(result)
except Exception as e:
logger.error(f"❌ Erro na análise do texto: {e}")
results.append({"error": str(e)})
return results
def get_model_info(self) -> Dict:
"""Obter informações do modelo"""
if not self.model:
return {"status": "not_loaded"}
try:
total_params = sum(p.numel() for p in self.model.parameters())
trainable_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
info = {
"model_name": self.model_name,
"total_parameters": total_params,
"trainable_parameters": trainable_params,
"model_size_gb": total_params * 4 / (1024**3), # Estimativa FP32
"status": "loaded",
"source": "huggingface_hub" if self.pipeline else "local"
}
if self.config:
info.update({
"hidden_size": getattr(self.config, 'hidden_size', None),
"num_layers": getattr(self.config, 'num_hidden_layers', None),
"vocab_size": getattr(self.config, 'vocab_size', None),
"specialized_tasks": {
"anomaly_detection": getattr(self.config, 'enable_anomaly_detection', False),
"financial_analysis": getattr(self.config, 'enable_financial_analysis', False),
"legal_reasoning": getattr(self.config, 'enable_legal_reasoning', False)
}
})
return info
except Exception as e:
logger.error(f"❌ Erro ao obter informações: {e}")
return {"status": "error", "error": str(e)}
def test_model(self) -> Dict:
"""Testar modelo com exemplo padrão"""
test_text = """
Contrato emergencial no valor de R$ 25.000.000,00 para aquisição
de equipamentos médicos dispensando licitação. Fornecedor: Empresa XYZ LTDA.
"""
try:
result = self.analyze_text(test_text.strip())
return {
"status": "success",
"test_input": test_text.strip(),
"analysis_result": result,
"model_info": self.get_model_info()
}
except Exception as e:
return {
"status": "error",
"error": str(e),
"model_info": self.get_model_info()
}
# Função de conveniência para uso global
_global_manager = None
def get_cidadao_manager(
model_name: str = "neural-thinker/cidadao-gpt",
force_reload: bool = False
) -> CidadaoAIHubManager:
"""Obter instância global do manager"""
global _global_manager
if _global_manager is None or force_reload:
_global_manager = CidadaoAIHubManager(model_name)
success = _global_manager.load_from_hub()
if not success:
logger.warning("⚠️ Falha ao carregar modelo. Verifique conectividade ou configuração.")
return _global_manager
def quick_analyze(text: str, model_name: str = "neural-thinker/cidadao-gpt") -> Dict:
"""Análise rápida usando modelo do HF Hub"""
manager = get_cidadao_manager(model_name)
return manager.analyze_text(text)
if __name__ == "__main__":
# Demonstração de uso
print("🤖 Testando integração Cidadão.AI + Hugging Face")
print("=" * 60)
# Criar manager
manager = CidadaoAIHubManager()
# Carregar modelo
success = manager.load_from_hub()
if success:
print("✅ Modelo carregado com sucesso!")
# Teste básico
test_result = manager.test_model()
print("\n📊 Resultado do teste:")
print(f"Status: {test_result['status']}")
if test_result['status'] == 'success':
result = test_result['analysis_result']
print(f"Anomalia: {result.get('anomaly', {}).get('label', 'N/A')}")
print(f"Risco Financeiro: {result.get('financial', {}).get('label', 'N/A')}")
print(f"Conformidade Legal: {result.get('legal', {}).get('label', 'N/A')}")
else:
print(f"Erro: {test_result.get('error', 'Desconhecido')}")
# Informações do modelo
info = manager.get_model_info()
print(f"\n🔧 Informações do modelo:")
print(f"Parâmetros: {info.get('total_parameters', 0):,}")
print(f"Fonte: {info.get('source', 'Desconhecida')}")
else:
print("❌ Falha ao carregar modelo") |