Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
40 lines (33 loc) · 1.34 KB
/
Copy pathapp.py
File metadata and controls
40 lines (33 loc) · 1.34 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
app = FastAPI(title="INCLUA – IA Demo", version="1.0.0")
# Treino rápido (mock) – substitua por dados reais quando desejar
TRAIN_TEXTS = [
"atendimento igualitário e sem discriminação",
"barreiras de acesso e tratamento desigual",
"serviço público acolhedor e inclusivo",
"discriminação institucional e estigmatização",
]
LABELS = [1, 0, 1, 0] # 1 = positivo/inclusivo, 0 = risco/desigualdade
pipeline: Pipeline = Pipeline([
("vect", CountVectorizer()),
("clf", LogisticRegression(max_iter=1000)),
])
pipeline.fit(TRAIN_TEXTS, LABELS)
class PredictOut(BaseModel):
label: str
score: float
@app.get("/predict", response_model=PredictOut)
async def predict(text: Optional[str] = None):
if not text:
raise HTTPException(status_code=400, detail="Parametro 'text' é obrigatório")
proba = pipeline.predict_proba([text])[0]
label_idx = int(proba.argmax())
label = "inclusivo" if label_idx == 1 else "risco"
score = float(proba[label_idx])
return PredictOut(label=label, score=score)
# Comando: uvicorn app:app --host 0.0.0.0 --port 8000