Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Carga
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
task="text-classification",
|
| 8 |
-
model="alramil/Practica7", # tu modelo alojado
|
| 9 |
-
device=0 # usa GPU si está disponible en el Space
|
| 10 |
-
)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def clasificar(texto):
|
| 15 |
result = classifier(texto)[0]
|
| 16 |
etiqueta = result['label']
|
| 17 |
puntuacion = result['score']
|
| 18 |
-
# Ajusta el formato según tus etiquetas (por ejemplo, 'LABEL_0' vs. 'Peligroso')
|
| 19 |
etiqueta_legible = 'Peligroso' if etiqueta in ['LABEL_0', '0'] else 'Seguro'
|
| 20 |
return f"{etiqueta_legible} ({puntuacion:.2%} de confianza)"
|
| 21 |
|
| 22 |
-
# Interfaz Gradio
|
| 23 |
demo = gr.Interface(
|
| 24 |
fn=clasificar,
|
| 25 |
inputs=gr.Textbox(lines=3, placeholder="Escribe aquí tu texto"),
|
| 26 |
outputs="text",
|
| 27 |
title="Clasificador de Texto Peligroso",
|
| 28 |
-
description="Detecta si un mensaje es seguro o peligroso usando tu modelo Practica7 alojado en
|
| 29 |
)
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
AutoModelForSequenceClassification,
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
TextClassificationPipeline
|
| 6 |
+
)
|
| 7 |
|
| 8 |
+
# 1) Carga manual del modelo y el tokenizador
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained("alramil/Practica7")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("alramil/Practica7")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# 2) Construye la pipeline
|
| 13 |
+
classifier = TextClassificationPipeline(
|
| 14 |
+
model=model,
|
| 15 |
+
tokenizer=tokenizer,
|
| 16 |
+
device=0 # GPU si está disponible
|
| 17 |
+
)
|
| 18 |
|
| 19 |
def clasificar(texto):
|
| 20 |
result = classifier(texto)[0]
|
| 21 |
etiqueta = result['label']
|
| 22 |
puntuacion = result['score']
|
|
|
|
| 23 |
etiqueta_legible = 'Peligroso' if etiqueta in ['LABEL_0', '0'] else 'Seguro'
|
| 24 |
return f"{etiqueta_legible} ({puntuacion:.2%} de confianza)"
|
| 25 |
|
| 26 |
+
# 3) Interfaz Gradio
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=clasificar,
|
| 29 |
inputs=gr.Textbox(lines=3, placeholder="Escribe aquí tu texto"),
|
| 30 |
outputs="text",
|
| 31 |
title="Clasificador de Texto Peligroso",
|
| 32 |
+
description="Detecta si un mensaje es seguro o peligroso usando tu modelo Practica7 alojado en HF Hub."
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|