Spaces:
Sleeping
Sleeping
File size: 2,641 Bytes
75b5d8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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() |