-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
290 lines (239 loc) · 10.9 KB
/
api.py
File metadata and controls
290 lines (239 loc) · 10.9 KB
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import Response, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from io import BytesIO
import re
import json
from typing import Dict, List, Optional
from PIL import Image
import asyncio
from models.isso import test_enhanced_model # Sua função existente
from pydantic import BaseModel
app = FastAPI(title="Fake News Analysis API")
# Configuração CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://127.0.0.1:3000"], # URLs do frontend
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
API_KEY = "your-api-key-here"
# Cache para armazenar análises (em produção use Redis ou database)
analysis_cache = {}
class ThemeAnalysisRequest(BaseModel):
theme: str
max_posts: Optional[int] = 10
language: Optional[str] = 'pt'
def parse_report(report_content: str) -> Dict:
"""Parseia o relatório e extrai os dados estruturados"""
patterns = {
'total': r"Total de textos analisados: (\d+)",
'fake': r"Classificados como FAKE: (\d+) \((\d+\.\d+)%\)",
'true': r"Classificados como TRUE: (\d+) \((\d+\.\d+)%\)",
'confidence': r"Confiança média: (\d+\.\d+)%",
'reliability': r"Score de confiabilidade médio: (\d+\.\d+)%",
'text': r"--- TEXTO (\d+) ---(.*?)(?=--- TEXTO|\Z)",
'ml': r"Predição ML: (FAKE|TRUE) \(Confiança: (\d+\.\d+)%\)",
'gpt': r"Recomendação GPT: (FAKE|TRUE|INCERTO)",
'reliability_score': r"Score de Confiabilidade: (\d+\.\d+)%"
}
data = {'summary': {}, 'texts': []}
try:
# Dados gerais
data['summary']['total_texts'] = int(re.search(patterns['total'], report_content).group(1))
data['summary']['fake_count'] = int(re.search(patterns['fake'], report_content).group(1))
data['summary']['fake_percentage'] = float(re.search(patterns['fake'], report_content).group(2))
data['summary']['true_count'] = int(re.search(patterns['true'], report_content).group(1))
data['summary']['true_percentage'] = float(re.search(patterns['true'], report_content).group(2))
data['summary']['avg_confidence'] = float(re.search(patterns['confidence'], report_content).group(1))
data['summary']['avg_reliability'] = float(re.search(patterns['reliability'], report_content).group(1))
# Dados por texto
text_matches = re.findall(patterns['text'], report_content, re.DOTALL)
for text_num, text_content in text_matches:
try:
ml_match = re.search(patterns['ml'], text_content)
gpt_match = re.search(patterns['gpt'], text_content)
reliability_match = re.search(patterns['reliability_score'], text_content)
text_data = {
'number': int(text_num),
'ml_prediction': ml_match.group(1) if ml_match else 'UNKNOWN',
'ml_confidence': float(ml_match.group(2)) if ml_match else 0,
'gpt_recommendation': gpt_match.group(1) if gpt_match else 'UNKNOWN',
'reliability_score': float(reliability_match.group(1)) if reliability_match else 0
}
data['texts'].append(text_data)
except Exception as e:
print(f"Erro ao processar texto {text_num}: {e}")
continue
except Exception as e:
print(f"Erro no parsing do relatório: {e}")
raise HTTPException(status_code=500, detail="Erro ao processar relatório")
return data
def generate_dashboard_image(data: Dict, theme: str) -> BytesIO:
"""Gera imagem PNG do dashboard"""
plt.style.use('default')
sns.set_palette("Set2")
fig = plt.figure(figsize=(16, 12))
fig.suptitle(f'Dashboard de Análise - Tema: {theme}', fontsize=16, fontweight='bold')
# Layout
gs = fig.add_gridspec(3, 2, hspace=0.4, wspace=0.3)
# 1. Gráfico de pizza
ax1 = fig.add_subplot(gs[0, 0])
labels = ['FAKE', 'TRUE']
sizes = [data['summary']['fake_percentage'], data['summary']['true_percentage']]
colors = ['#ff6b6b', '#51cf66']
ax1.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
ax1.set_title('Distribuição FAKE vs TRUE')
ax1.axis('equal')
# 2. Métricas gerais
ax2 = fig.add_subplot(gs[0, 1])
categories = ['Confiança Média', 'Score Confiabilidade']
values = [data['summary']['avg_confidence'], data['summary']['avg_reliability']]
bars = ax2.bar(categories, values, color=['#339af0', '#ff922b'])
ax2.set_ylabel('Percentual (%)')
ax2.set_ylim(0, 100)
ax2.set_title('Métricas Gerais de Confiança')
for bar, value in zip(bars, values):
ax2.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 1,
f'{value:.1f}%', ha='center', va='bottom')
# 3. Comparação por texto
ax3 = fig.add_subplot(gs[1, :])
texts = data['texts']
text_numbers = [f'Texto {t["number"]}' for t in texts]
ml_confidences = [t["ml_confidence"] for t in texts]
reliability_scores = [t["reliability_score"] for t in texts]
x = np.arange(len(text_numbers))
width = 0.35
bars1 = ax3.bar(x - width/2, ml_confidences, width, label='Confiança ML', color='#339af0')
bars2 = ax3.bar(x + width/2, reliability_scores, width, label='Score Confiabilidade', color='#ff922b')
ax3.set_xlabel('Textos Analisados')
ax3.set_ylabel('Scores (%)')
ax3.set_title('Comparação por Texto: ML vs Confiabilidade')
ax3.set_xticks(x)
ax3.set_xticklabels(text_numbers)
ax3.legend()
ax3.set_ylim(0, 100)
# 4. Tabela de resumo
ax4 = fig.add_subplot(gs[2, :])
ax4.axis('off')
summary_data = [
['Total de Textos', data['summary']['total_texts']],
['Textos FAKE', f"{data['summary']['fake_count']} ({data['summary']['fake_percentage']}%)"],
['Textos TRUE', f"{data['summary']['true_count']} ({data['summary']['true_percentage']}%)"],
['Confiança Média', f"{data['summary']['avg_confidence']}%"],
['Score Confiabilidade Médio', f"{data['summary']['avg_reliability']}%"]
]
table = ax4.table(cellText=summary_data,
cellLoc='left',
loc='center',
bbox=[0, 0, 1, 1])
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1, 2)
plt.tight_layout()
buf = BytesIO()
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
plt.close()
buf.seek(0)
return buf
async def analyze_theme_with_gpt(theme: str, max_posts: int = 10, language: str = 'pt') -> Dict:
"""Executa análise do tema usando o modelo GPT"""
# Simulação de coleta de posts (substitua pela sua implementação real)
print(f"Analisando tema: {theme}, posts: {max_posts}, idioma: {language}")
# Chama sua função existente (ajuste conforme necessário)
#report_filename = test_enhanced_model(openai_api_key=API_KEY)
report_filename = "respostas.txt"
# Lê e parseia o relatório
with open(report_filename, 'r', encoding='utf-8') as f:
report_content = f.read()
return parse_report(report_content)
@app.get("/")
async def root():
"""Endpoint raiz com informações da API unificada"""
return {
"message": "Unified Fake News Detection API",
"version": "2.0.0",
"services": {
"bluesky_analysis": "Análise de posts do Bluesky por tema",
"enhanced_model": "Modelo aprimorado de detecção com GPT-4"
},
"endpoints": {
"health": "/health",
"bluesky_analyze": "/analyze-theme",
"bluesky_dashboard": "/dashboard/{theme}",
"bluesky_posts": "/posts/{theme}",
"enhanced_predict": "/predict",
"enhanced_report": "/predict/report/{filename}",
"docs": "/docs"
}
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "message": "API is running"}
@app.post("/analyze-theme")
async def analyze_theme(request: ThemeAnalysisRequest):
"""Analisa um tema e retorna dados estruturados"""
try:
# Verifica se já existe no cache
cache_key = f"{request.theme}_{request.max_posts}_{request.language}"
if cache_key in analysis_cache:
return analysis_cache[cache_key]
# Executa análise
analysis_data = await analyze_theme_with_gpt(request.theme, request.max_posts, request.language)
# Armazena no cache
analysis_cache[cache_key] = analysis_data
return analysis_data
except Exception as e:
raise HTTPException(status_code=500, detail=f"Erro na análise: {str(e)}")
@app.get("/dashboard/{theme}")
async def get_dashboard(theme: str, max_posts: int = 10):
"""Retorna dashboard como imagem PNG"""
try:
cache_key = f"{theme}_{max_posts}_pt"
if cache_key not in analysis_cache:
# Se não existe, executa análise primeiro
analysis_data = await analyze_theme_with_gpt(theme, max_posts)
analysis_cache[cache_key] = analysis_data
data = analysis_cache[cache_key]
image_buf = generate_dashboard_image(data, theme)
return Response(
content=image_buf.getvalue(),
media_type="image/png",
headers={"Content-Disposition": f"inline; filename=dashboard_{theme}.png"}
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Erro ao gerar dashboard: {str(e)}")
@app.get("/posts/{theme}")
async def get_posts(theme: str, max_posts: int = 10):
"""Retorna os posts analisados (dados brutos)"""
try:
cache_key = f"{theme}_{max_posts}_pt"
if cache_key not in analysis_cache:
analysis_data = await analyze_theme_with_gpt(theme, max_posts)
analysis_cache[cache_key] = analysis_data
# Retorna os textos analisados
posts_data = []
for text in analysis_cache[cache_key]['texts']:
posts_data.append({
'id': text['number'],
'ml_prediction': text['ml_prediction'],
'ml_confidence': text['ml_confidence'],
'gpt_recommendation': text['gpt_recommendation'],
'reliability_score': text['reliability_score']
})
return {
'theme': theme,
'total_posts': analysis_cache[cache_key]['summary']['total_texts'],
'posts': posts_data
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Erro ao obter posts: {str(e)}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)