Download app.py from alramil/Practica7: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/alramil/Practica7/resolve/main/app.py
- Command line
-
hf download hf://spaces/alramil/Practica7/app.py
-
curl -L -o app.py https://huggingface.co/spaces/alramil/Practica7/resolve/main/app.py
1.11 kB
| import gradio as gr | |
| from transformers import ( | |
| AutoModelForSequenceClassification, | |
| AutoTokenizer, | |
| TextClassificationPipeline | |
| ) | |
| # 1) Carga manual del modelo y el tokenizador | |
| model = AutoModelForSequenceClassification.from_pretrained("alramil/Practica7MA") | |
| tokenizer = AutoTokenizer.from_pretrained("alramil/Practica7MA") | |
| # 2) Construye la pipeline | |
| classifier = TextClassificationPipeline( | |
| model=model, | |
| tokenizer=tokenizer, | |
| device=0 # GPU si está disponible | |
| ) | |
| def clasificar(texto): | |
| result = classifier(texto)[0] | |
| etiqueta = result['label'] | |
| puntuacion = result['score'] | |
| etiqueta_legible = 'Peligroso' if etiqueta in ['LABEL_0', '0'] else 'Seguro' | |
| return f"{etiqueta_legible} ({puntuacion:.2%} de confianza)" | |
| # 3) Interfaz Gradio | |
| demo = gr.Interface( | |
| fn=clasificar, | |
| inputs=gr.Textbox(lines=3, placeholder="Escribe aquí tu texto"), | |
| outputs="text", | |
| title="Clasificador de Texto Peligroso", | |
| description="Detecta si un mensaje es seguro o peligroso usando tu modelo Practica7MA alojado en HF Hub." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |