Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Inicializar FastAPI
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Cargar el modelo y el tokenizador desde Hugging Face
|
9 |
+
model_name = "ancerlop/ToxicBERTMultilabelTextClassification"
|
10 |
+
model = AutoModel.from_pretrained(model_name)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# Definir funci贸n de predicci贸n
|
14 |
+
def predict(text):
|
15 |
+
inputs = tokenizer(text, return_tensors="pt")
|
16 |
+
outputs = model(**inputs)
|
17 |
+
return outputs.logits
|
18 |
+
|
19 |
+
# Crear una interfaz Gradio
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=predict,
|
22 |
+
inputs=gr.inputs.Textboxbox(),
|
23 |
+
outputs=gr.outputs.Label(num_top_classes=5),
|
24 |
+
live=True,
|
25 |
+
title="Modelo de Clasificaci贸n de Texto",
|
26 |
+
description="Este modelo clasifica texto en diferentes categor铆as."
|
27 |
+
)
|
28 |
+
|
29 |
+
# Definir una ruta en FastAPI para la API
|
30 |
+
@app.post("/api/predict/")
|
31 |
+
def predict_api(text: str):
|
32 |
+
try:
|
33 |
+
result = predict(text)
|
34 |
+
return {"predictions": result.tolist()}
|
35 |
+
except Exception as e:
|
36 |
+
raise HTTPException(status_code=500, detail=str(e))
|
37 |
+
|
38 |
+
# Montar Gradio en FastAPI
|
39 |
+
@app.get("/gradio")
|
40 |
+
def gradio_interface():
|
41 |
+
return iface.launch()
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
import uvicorn
|
45 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|