Spaces:
Sleeping
Sleeping
trying fast api again
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from typing import Dict, Any
|
|
|
|
| 4 |
|
| 5 |
INTENT_MODEL = "rasouzadev/medgo-intent-classifier"
|
| 6 |
HATE_MODEL = "unitary/unbiased-toxic-roberta"
|
|
@@ -8,7 +10,19 @@ HATE_MODEL = "unitary/unbiased-toxic-roberta"
|
|
| 8 |
print("Loading pipelines (this may take a minute)...")
|
| 9 |
|
| 10 |
intent_pipe = pipeline("text-classification", model=INTENT_MODEL, truncation=True)
|
| 11 |
-
hate_pipe = pipeline("text-classification", model=HATE_MODEL, truncation=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def unify_scores(pipe_output):
|
| 14 |
if not pipe_output:
|
|
@@ -17,10 +31,25 @@ def unify_scores(pipe_output):
|
|
| 17 |
return pipe_output[0]
|
| 18 |
return pipe_output
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
hate_raw = hate_pipe(text)
|
| 25 |
hate_scores = unify_scores(hate_raw)
|
| 26 |
best_hate = max(hate_scores, key=lambda x: x.get("score", 0.0), default=None)
|
|
@@ -51,32 +80,5 @@ def classify(text: str) -> Dict[str, Any]:
|
|
| 51 |
"note": None
|
| 52 |
}
|
| 53 |
|
| 54 |
-
with gr.Blocks(title="MedGo - Intent & Hate Detector API") as demo:
|
| 55 |
-
gr.Markdown("# MedGo - Intent & Hate Detector API")
|
| 56 |
-
gr.Markdown("API para classificação de intenção e detecção de hate speech")
|
| 57 |
-
|
| 58 |
-
with gr.Row():
|
| 59 |
-
with gr.Column():
|
| 60 |
-
text_input = gr.Textbox(
|
| 61 |
-
label="Texto para classificar",
|
| 62 |
-
placeholder="Digite o texto aqui...",
|
| 63 |
-
lines=3
|
| 64 |
-
)
|
| 65 |
-
submit_btn = gr.Button("Classificar", variant="primary")
|
| 66 |
-
|
| 67 |
-
with gr.Column():
|
| 68 |
-
output_json = gr.JSON(label="Resultado")
|
| 69 |
-
|
| 70 |
-
gr.Examples(
|
| 71 |
-
examples=[
|
| 72 |
-
["Preciso marcar uma consulta"],
|
| 73 |
-
["Qual é o horário de atendimento?"],
|
| 74 |
-
["Você é um idiota!"],
|
| 75 |
-
],
|
| 76 |
-
inputs=text_input
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
submit_btn.click(fn=classify, inputs=text_input, outputs=output_json, api_name="predict")
|
| 80 |
-
|
| 81 |
if __name__ == "__main__":
|
| 82 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
from typing import Dict, Any
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
INTENT_MODEL = "rasouzadev/medgo-intent-classifier"
|
| 8 |
HATE_MODEL = "unitary/unbiased-toxic-roberta"
|
|
|
|
| 10 |
print("Loading pipelines (this may take a minute)...")
|
| 11 |
|
| 12 |
intent_pipe = pipeline("text-classification", model=INTENT_MODEL, truncation=True)
|
| 13 |
+
hate_pipe = pipeline("text-classification", model=HATE_MODEL, truncation=True, top_k=None)
|
| 14 |
+
|
| 15 |
+
app = FastAPI(title="MedGo - Intent & Hate Detector API")
|
| 16 |
+
|
| 17 |
+
class InputText(BaseModel):
|
| 18 |
+
text: str
|
| 19 |
+
|
| 20 |
+
class PredictionResponse(BaseModel):
|
| 21 |
+
intent: str
|
| 22 |
+
intent_score: float
|
| 23 |
+
hate_label: str | None
|
| 24 |
+
hate_score: float
|
| 25 |
+
note: str | None
|
| 26 |
|
| 27 |
def unify_scores(pipe_output):
|
| 28 |
if not pipe_output:
|
|
|
|
| 31 |
return pipe_output[0]
|
| 32 |
return pipe_output
|
| 33 |
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def root():
|
| 36 |
+
return {
|
| 37 |
+
"message": "MedGo API - Intent & Hate Detector",
|
| 38 |
+
"endpoints": {
|
| 39 |
+
"predict": "/predict",
|
| 40 |
+
"health": "/health",
|
| 41 |
+
"docs": "/docs"
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
@app.get("/health")
|
| 46 |
+
def health():
|
| 47 |
+
return {"status": "ok"}
|
| 48 |
+
|
| 49 |
+
@app.post("/predict", response_model=PredictionResponse)
|
| 50 |
+
def classify(input_data: InputText) -> Dict[str, Any]:
|
| 51 |
+
text = input_data.text
|
| 52 |
+
|
| 53 |
hate_raw = hate_pipe(text)
|
| 54 |
hate_scores = unify_scores(hate_raw)
|
| 55 |
best_hate = max(hate_scores, key=lambda x: x.get("score", 0.0), default=None)
|
|
|
|
| 80 |
"note": None
|
| 81 |
}
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|