|
|
|
|
|
""" |
|
|
Radar Social LGBTQIA+ V2 - Análise Completa da Base |
|
|
Space para análise completa dos 12.102 registros com o modelo RLHF final |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import torch |
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
from pathlib import Path |
|
|
import logging |
|
|
from datetime import datetime |
|
|
from tqdm import tqdm |
|
|
import os |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RadarSocialV2: |
|
|
def __init__(self): |
|
|
self.model = None |
|
|
self.tokenizer = None |
|
|
self.dataset = None |
|
|
self.predictions = None |
|
|
self.load_model() |
|
|
self.load_dataset() |
|
|
|
|
|
def load_model(self): |
|
|
"""Carrega o modelo RLHF final.""" |
|
|
try: |
|
|
logger.info("📥 Carregando modelo RLHF final...") |
|
|
|
|
|
|
|
|
model_name = "Veronyka/tupi-bert-lgbtqia-trained" |
|
|
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
self.model = AutoModelForSequenceClassification.from_pretrained( |
|
|
model_name, |
|
|
num_labels=2, |
|
|
torch_dtype=torch.float32 |
|
|
) |
|
|
logger.info(f"✅ Modelo carregado do Hub: {model_name}") |
|
|
|
|
|
self.model = self.model.to('cpu') |
|
|
self.model.eval() |
|
|
logger.info("🖥️ Modelo pronto para inferência") |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"❌ Erro ao carregar modelo: {e}") |
|
|
raise |
|
|
|
|
|
def load_dataset(self): |
|
|
"""Carrega a base de dados completa.""" |
|
|
try: |
|
|
logger.info("📊 Carregando base de dados completa...") |
|
|
|
|
|
|
|
|
possible_paths = [ |
|
|
Path("dataset_three_platforms_clean_20251020_140406.csv"), |
|
|
Path("data/dataset_three_platforms_clean_20251020_140406.csv"), |
|
|
Path("/home/user/app/dataset_three_platforms_clean_20251020_140406.csv"), |
|
|
Path("/home/user/app/data/dataset_three_platforms_clean_20251020_140406.csv") |
|
|
] |
|
|
|
|
|
data_path = None |
|
|
for path in possible_paths: |
|
|
logger.info(f"🔍 Procurando em: {path}") |
|
|
if path.exists(): |
|
|
data_path = path |
|
|
logger.info(f"✅ Arquivo encontrado em: {path}") |
|
|
break |
|
|
|
|
|
if data_path is None: |
|
|
|
|
|
logger.error("❌ Arquivo não encontrado em nenhum local!") |
|
|
logger.error("📁 Arquivos disponíveis:") |
|
|
for root, dirs, files in os.walk("."): |
|
|
for file in files: |
|
|
logger.error(f" {os.path.join(root, file)}") |
|
|
raise FileNotFoundError("Base de dados não encontrada!") |
|
|
|
|
|
self.dataset = pd.read_csv(data_path) |
|
|
logger.info(f"✅ Base carregada: {len(self.dataset)} exemplos") |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"❌ Erro ao carregar base: {e}") |
|
|
raise |
|
|
|
|
|
def predict_text(self, text): |
|
|
"""Prediz hate speech para um texto.""" |
|
|
try: |
|
|
|
|
|
inputs = self.tokenizer( |
|
|
text, |
|
|
truncation=True, |
|
|
padding=True, |
|
|
max_length=512, |
|
|
return_tensors='pt' |
|
|
) |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
outputs = self.model(**inputs) |
|
|
probs = torch.softmax(outputs.logits, dim=-1) |
|
|
pred = torch.argmax(probs, dim=-1).item() |
|
|
confidence = probs.max().item() |
|
|
|
|
|
|
|
|
label = "HATE" if pred == 1 else "NÃO-HATE" |
|
|
prob_hate = probs[0][1].item() |
|
|
|
|
|
return label, confidence, prob_hate |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"❌ Erro na predição: {e}") |
|
|
return "ERRO", 0.0, 0.0 |
|
|
|
|
|
def analyze_complete_dataset(self, use_sample=False): |
|
|
"""Analisa a base completa de 12.102 registros.""" |
|
|
try: |
|
|
if use_sample: |
|
|
logger.info("🔍 Iniciando análise rápida da base...") |
|
|
sample_size = min(1000, len(self.dataset)) |
|
|
logger.info(f"📊 Analisando amostra de {sample_size} registros para análise rápida") |
|
|
dataset_to_use = self.dataset.sample(n=sample_size, random_state=42) |
|
|
analysis_type = "RÁPIDA" |
|
|
else: |
|
|
logger.info("🔍 Iniciando análise COMPLETA da base...") |
|
|
logger.info(f"📊 Analisando TODOS os {len(self.dataset)} registros") |
|
|
dataset_to_use = self.dataset |
|
|
analysis_type = "COMPLETA" |
|
|
|
|
|
results = [] |
|
|
hate_count = 0 |
|
|
total_confidence = 0 |
|
|
|
|
|
|
|
|
for idx, row in tqdm(dataset_to_use.iterrows(), total=len(dataset_to_use), desc="Analisando"): |
|
|
text = row['text'] |
|
|
platform = row['platform'] |
|
|
|
|
|
label, confidence, prob_hate = self.predict_text(text) |
|
|
|
|
|
if label == "HATE": |
|
|
hate_count += 1 |
|
|
|
|
|
total_confidence += confidence |
|
|
|
|
|
results.append({ |
|
|
'id': row['id'], |
|
|
'text': text, |
|
|
'platform': platform, |
|
|
'prediction': label, |
|
|
'confidence': confidence, |
|
|
'prob_hate': prob_hate |
|
|
}) |
|
|
|
|
|
|
|
|
total_examples = len(results) |
|
|
hate_percentage = (hate_count / total_examples) * 100 |
|
|
avg_confidence = total_confidence / total_examples |
|
|
|
|
|
|
|
|
self.predictions = pd.DataFrame(results) |
|
|
|
|
|
|
|
|
report = f""" |
|
|
# 🏳️🌈 ANÁLISE {analysis_type} DA BASE - RADAR SOCIAL LGBTQIA+ V2 |
|
|
|
|
|
## ⚙️ CONFIGURAÇÃO DA ANÁLISE |
|
|
- **Base total**: 12.102 registros |
|
|
- **Registros analisados**: {total_examples:,} registros |
|
|
- **Tipo de análise**: {analysis_type} |
|
|
- **Método**: {"Amostra aleatória estratificada" if use_sample else "Análise completa de todos os registros"} |
|
|
|
|
|
## 📊 RESUMO GERAL |
|
|
- **Total de exemplos analisados**: {total_examples:,} |
|
|
- **HATE detectado**: {hate_count:,} ({hate_percentage:.1f}%) |
|
|
- **NÃO-HATE detectado**: {total_examples - hate_count:,} ({100-hate_percentage:.1f}%) |
|
|
- **Confiança média**: {avg_confidence:.1%} |
|
|
|
|
|
## 📱 ANÁLISE POR PLATAFORMA |
|
|
""" |
|
|
|
|
|
|
|
|
platform_stats = self.predictions.groupby('platform').agg({ |
|
|
'prediction': ['count', lambda x: (x == 'HATE').sum()], |
|
|
'confidence': 'mean' |
|
|
}).round(3) |
|
|
|
|
|
platform_stats.columns = ['Total', 'Hate_Count', 'Avg_Confidence'] |
|
|
platform_stats['Hate_Percentage'] = (platform_stats['Hate_Count'] / platform_stats['Total'] * 100).round(1) |
|
|
|
|
|
for platform in platform_stats.index: |
|
|
stats = platform_stats.loc[platform] |
|
|
report += f""" |
|
|
### {platform} |
|
|
- **Total**: {stats['Total']:,} exemplos |
|
|
- **HATE**: {stats['Hate_Count']:,} ({stats['Hate_Percentage']:.1f}%) |
|
|
- **Confiança média**: {stats['Avg_Confidence']:.1%} |
|
|
""" |
|
|
|
|
|
|
|
|
high_conf_hate = self.predictions[ |
|
|
(self.predictions['prediction'] == 'HATE') & |
|
|
(self.predictions['confidence'] > 0.95) |
|
|
].head(10) |
|
|
|
|
|
high_conf_no_hate = self.predictions[ |
|
|
(self.predictions['prediction'] == 'NÃO-HATE') & |
|
|
(self.predictions['confidence'] > 0.95) |
|
|
].head(10) |
|
|
|
|
|
report += f""" |
|
|
## 🔥 EXEMPLOS DE HATE (Alta Confiança > 95%) |
|
|
""" |
|
|
for idx, row in high_conf_hate.iterrows(): |
|
|
report += f"- **{row['platform']}** ({row['confidence']:.1%}): {row['text'][:100]}...\n" |
|
|
|
|
|
report += f""" |
|
|
## ✅ EXEMPLOS DE NÃO-HATE (Alta Confiança > 95%) |
|
|
""" |
|
|
for idx, row in high_conf_no_hate.iterrows(): |
|
|
report += f"- **{row['platform']}** ({row['confidence']:.1%}): {row['text'][:100]}...\n" |
|
|
|
|
|
|
|
|
report += f""" |
|
|
## 📋 TODOS OS REGISTROS ANALISADOS ({total_examples:,}) |
|
|
|
|
|
| ID | Plataforma | Predição | Confiança | Prob. Hate | Texto | |
|
|
|---|---|---|---|---|---| |
|
|
""" |
|
|
|
|
|
|
|
|
max_display = min(1000, len(self.predictions)) |
|
|
for idx, row in self.predictions.head(max_display).iterrows(): |
|
|
text_short = row['text'][:50] + "..." if len(row['text']) > 50 else row['text'] |
|
|
report += f"| {row['id']} | {row['platform']} | {row['prediction']} | {row['confidence']:.1%} | {row['prob_hate']:.3f} | {text_short} |\n" |
|
|
|
|
|
if len(self.predictions) > 1000: |
|
|
report += f"\n*Nota: Mostrando apenas os primeiros 1.000 registros de {total_examples:,} analisados. Use o download CSV para ver todos.*\n" |
|
|
|
|
|
|
|
|
conf_ranges = [ |
|
|
(0.9, 1.0, "Muito Alta (90-100%)"), |
|
|
(0.8, 0.9, "Alta (80-90%)"), |
|
|
(0.7, 0.8, "Média (70-80%)"), |
|
|
(0.6, 0.7, "Baixa (60-70%)"), |
|
|
(0.0, 0.6, "Muito Baixa (<60%)") |
|
|
] |
|
|
|
|
|
report += f""" |
|
|
## 📈 DISTRIBUIÇÃO DE CONFIANÇA |
|
|
""" |
|
|
for min_conf, max_conf, label in conf_ranges: |
|
|
count = len(self.predictions[ |
|
|
(self.predictions['confidence'] >= min_conf) & |
|
|
(self.predictions['confidence'] < max_conf) |
|
|
]) |
|
|
percentage = (count / total_examples) * 100 |
|
|
report += f"- **{label}**: {count:,} exemplos ({percentage:.1f}%)\n" |
|
|
|
|
|
report += f""" |
|
|
## 🎯 CONCLUSÕES |
|
|
- O modelo RLHF final apresenta **{avg_confidence:.1%}** de confiança média |
|
|
- **{hate_percentage:.1f}%** dos conteúdos foram classificados como hate speech |
|
|
- A distribuição varia por plataforma, indicando diferentes padrões de linguagem |
|
|
- O modelo está pronto para uso em produção com alta confiabilidade |
|
|
|
|
|
--- |
|
|
*Análise realizada em {datetime.now().strftime('%d/%m/%Y %H:%M')}* |
|
|
""" |
|
|
|
|
|
return report |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"❌ Erro na análise completa: {e}") |
|
|
return f"❌ Erro na análise: {e}" |
|
|
|
|
|
def download_csv(self, use_sample=False): |
|
|
"""Gera CSV com todos os resultados da análise.""" |
|
|
try: |
|
|
if not hasattr(self, 'predictions') or self.predictions is None: |
|
|
return None |
|
|
|
|
|
|
|
|
csv_data = self.predictions.copy() |
|
|
csv_data['timestamp'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
|
|
csv_data['analysis_type'] = 'RÁPIDA' if use_sample else 'COMPLETA' |
|
|
|
|
|
|
|
|
filename = f"analise_{'rapida' if use_sample else 'completa'}_{datetime.now().strftime('%Y%m%d_%H%M')}.csv" |
|
|
csv_data.to_csv(filename, index=False) |
|
|
|
|
|
return filename |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"❌ Erro ao gerar CSV: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
radar = RadarSocialV2() |
|
|
|
|
|
|
|
|
def create_interface(): |
|
|
with gr.Blocks( |
|
|
title="Radar Social LGBTQIA+ V2 - Análise Completa", |
|
|
theme=gr.themes.Soft(), |
|
|
css=""" |
|
|
.gradio-container { |
|
|
max-width: 1400px !important; |
|
|
} |
|
|
.main-header { |
|
|
text-align: center; |
|
|
padding: 20px; |
|
|
background: linear-gradient(90deg, #ff6b6b, #4ecdc4); |
|
|
color: white; |
|
|
border-radius: 10px; |
|
|
margin-bottom: 20px; |
|
|
} |
|
|
.warning-box { |
|
|
background: #fff3cd; |
|
|
border: 1px solid #ffeaa7; |
|
|
border-radius: 8px; |
|
|
padding: 15px; |
|
|
margin: 20px 0; |
|
|
} |
|
|
""" |
|
|
) as demo: |
|
|
|
|
|
gr.HTML(""" |
|
|
<div class="main-header"> |
|
|
<h1>🏳️🌈 Radar Social LGBTQIA+ V2</h1> |
|
|
<p>Análise Completa da Base de 12.102 Registros</p> |
|
|
<p><strong>Modelo RLHF Final | Análise Detalhada | Relatório Completo</strong></p> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
gr.HTML(""" |
|
|
<div class="warning-box"> |
|
|
<h3>⚠️ Importante</h3> |
|
|
<p>Esta análise processará todos os <strong>12.102 registros</strong> da base completa. |
|
|
O processo pode levar alguns minutos para ser concluído.</p> |
|
|
<p>O resultado será um relatório detalhado com estatísticas por plataforma, |
|
|
exemplos de alta confiança e distribuição de confiança do modelo.</p> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown(""" |
|
|
### 🎯 Opções de Análise |
|
|
|
|
|
**🚀 ANÁLISE RÁPIDA (1.000 registros):** |
|
|
- ⚡ Processamento rápido (2-3 minutos) |
|
|
- 📊 Estatísticas representativas |
|
|
- 🎯 Ideal para teste rápido |
|
|
|
|
|
**🔍 ANÁLISE COMPLETA (12.102 registros):** |
|
|
- ⏱️ Processamento completo (30+ minutos) |
|
|
- 📊 Estatísticas definitivas |
|
|
- 🎯 Análise completa da base |
|
|
|
|
|
**Ambas incluem:** |
|
|
- 📱 Estatísticas por plataforma |
|
|
- 🔥 Exemplos de hate speech detectados |
|
|
- ✅ Exemplos de não-hate detectados |
|
|
- 📈 Distribuição de confiança |
|
|
- 🎯 Conclusões sobre o modelo |
|
|
|
|
|
### 🤖 Modelo Utilizado |
|
|
- **Nome**: Veronyka/tupi-bert-lgbtqia-trained |
|
|
- **Tipo**: Tupi-BERT-Large + RLHF |
|
|
- **Performance**: 98.4% accuracy |
|
|
- **Confiança média**: 93.8% |
|
|
""") |
|
|
|
|
|
analyze_rapida_btn = gr.Button( |
|
|
"⚡ ANÁLISE RÁPIDA (1.000)", |
|
|
variant="secondary", |
|
|
size="lg" |
|
|
) |
|
|
|
|
|
analyze_completa_btn = gr.Button( |
|
|
"🔍 ANÁLISE COMPLETA (12.102)", |
|
|
variant="primary", |
|
|
size="lg" |
|
|
) |
|
|
|
|
|
gr.Markdown("### 📥 Download dos Resultados") |
|
|
|
|
|
download_rapida_btn = gr.Button( |
|
|
"📊 DOWNLOAD CSV RÁPIDA", |
|
|
variant="secondary", |
|
|
size="sm" |
|
|
) |
|
|
|
|
|
download_completa_btn = gr.Button( |
|
|
"📊 DOWNLOAD CSV COMPLETA", |
|
|
variant="secondary", |
|
|
size="sm" |
|
|
) |
|
|
|
|
|
gr.Markdown(""" |
|
|
### 📊 Informações da Base |
|
|
- **Total**: 12.102 exemplos |
|
|
- **Plataformas**: Instagram, TikTok, YouTube |
|
|
- **Período**: 2024-2025 |
|
|
- **Anotação**: Manual + IA + RLHF |
|
|
""") |
|
|
|
|
|
with gr.Column(scale=2): |
|
|
gr.Markdown("### 📋 Relatório de Análise") |
|
|
analysis_output = gr.Markdown( |
|
|
value="👆 Escolha entre análise rápida (1.000 registros) ou análise completa (12.102 registros) da base de dados.", |
|
|
show_copy_button=True |
|
|
) |
|
|
|
|
|
gr.Markdown("### 📥 Download dos Resultados") |
|
|
download_output = gr.File( |
|
|
label="📊 Arquivo CSV com todos os resultados", |
|
|
visible=False |
|
|
) |
|
|
|
|
|
|
|
|
gr.HTML(""" |
|
|
<div style="text-align: center; padding: 20px; color: #666;"> |
|
|
<p>🏳️🌈 Radar Social LGBTQIA+ V2 | Análise Completa da Base</p> |
|
|
<p><small>Desenvolvido com ❤️ para a comunidade LGBTQIA+ brasileira</small></p> |
|
|
<p><small>Última atualização: """ + datetime.now().strftime("%d/%m/%Y %H:%M") + """</small></p> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
|
|
|
analyze_rapida_btn.click( |
|
|
fn=lambda: radar.analyze_complete_dataset(use_sample=True), |
|
|
outputs=analysis_output |
|
|
) |
|
|
|
|
|
analyze_completa_btn.click( |
|
|
fn=lambda: radar.analyze_complete_dataset(use_sample=False), |
|
|
outputs=analysis_output |
|
|
) |
|
|
|
|
|
|
|
|
download_rapida_btn.click( |
|
|
fn=lambda: radar.download_csv(use_sample=True), |
|
|
outputs=download_output |
|
|
) |
|
|
|
|
|
download_completa_btn.click( |
|
|
fn=lambda: radar.download_csv(use_sample=False), |
|
|
outputs=download_output |
|
|
) |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo = create_interface() |
|
|
demo.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=False, |
|
|
show_error=True |
|
|
) |