Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| from dotenv import load_dotenv | |
| # --- CONFIGURACIÓN --- | |
| load_dotenv() | |
| MI_TOKEN = os.getenv("HF_TOKEN") | |
| MODELO = "facebook/bart-large-mnli" | |
| API_URL = f"https://router.huggingface.co/hf-inference/models/{MODELO}" | |
| HEADERS = {"Authorization": f"Bearer {MI_TOKEN}"} | |
| def clasificar_incidencia(mensaje): | |
| if not mensaje: | |
| return None | |
| print(f"📡 Enviando incidencia: '{mensaje}'...") | |
| # Departamentos (Etiquetas) | |
| departamentos = [ | |
| "Soporte IT", | |
| "Mantenimiento", | |
| "Secretaría", | |
| "Seguridad", | |
| "Cafetería" | |
| ] | |
| # Configuración para clasificación | |
| payload = { | |
| "inputs": mensaje, | |
| "parameters": { | |
| "candidate_labels": departamentos, | |
| "multi_label": False | |
| } | |
| } | |
| try: | |
| response = requests.post(API_URL, headers=HEADERS, json=payload) | |
| datos = response.json() | |
| print(f"📦 DATOS RECIBIDOS: {datos}") | |
| if response.status_code == 200: | |
| resultados_gradio = {} | |
| if isinstance(datos, list): | |
| for item in datos: | |
| etiqueta = item.get('label') | |
| puntuacion = item.get('score') | |
| if etiqueta and puntuacion is not None: | |
| resultados_gradio[etiqueta] = puntuacion | |
| return resultados_gradio | |
| elif isinstance(datos, dict) and 'labels' in datos: | |
| return {l: s for l, s in zip(datos['labels'], datos['scores'])} | |
| else: | |
| return {"Error: Formato desconocido": 0.0} | |
| elif response.status_code == 503: | |
| return {"⏳ Cargando modelo... (Prueba en 10s)": 0.0} | |
| else: | |
| return {f"Error {response.status_code}": 0.0} | |
| except Exception as e: | |
| return {f"Error técnico: {e}": 0.0} | |
| # Interfaz | |
| ui = gr.Interface( | |
| fn=clasificar_incidencia, | |
| inputs=gr.Textbox( | |
| label="📝 Incidencia", | |
| placeholder="Ej: Se ha roto la silla del aula 4...", | |
| lines=2 | |
| ), | |
| outputs=gr.Label(num_top_classes=5, label="Clasificación IA"), | |
| title="🏢 Smart Campus: Router IA", | |
| description="Sistema de triaje inteligente para incidencias universitarias.", | |
| examples=[ | |
| ["El proyector no enciende y tengo clase ahora."], | |
| ["He perdido mi cartera en el pasillo."], | |
| ["Necesito un certificado de notas para la beca."] | |
| ], | |
| flagging_mode="never" | |
| ) | |
| if __name__ == "__main__": | |
| ui.launch() |