Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,11 @@ import urllib.parse
|
|
| 6 |
base_conocimiento = [
|
| 7 |
{
|
| 8 |
"claves": ["horario", "hora", "abren", "atienden"],
|
| 9 |
-
"respuesta": "Nuestro horario es:\
|
| 10 |
},
|
| 11 |
{
|
| 12 |
"claves": ["servicio", "hacen", "ofrecen"],
|
| 13 |
-
"respuesta": "
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"claves": ["norma", "regla", "prohibido", "comer"],
|
|
@@ -22,17 +22,13 @@ base_conocimiento = [
|
|
| 22 |
}
|
| 23 |
]
|
| 24 |
|
|
|
|
| 25 |
saludos = ["hola", "buenas", "buenos días", "buenas tardes", "buenas noches"]
|
| 26 |
|
| 27 |
-
libros = [
|
| 28 |
-
{"titulo": "Cien años de soledad", "autor": "Gabriel García Márquez"},
|
| 29 |
-
{"titulo": "1984", "autor": "George Orwell"},
|
| 30 |
-
{"titulo": "El principito", "autor": "Antoine de Saint-Exupéry"}
|
| 31 |
-
]
|
| 32 |
-
|
| 33 |
# 🧠 Memoria simple
|
| 34 |
historial_global = []
|
| 35 |
|
|
|
|
| 36 |
def limpiar_consulta(texto):
|
| 37 |
palabras_basura = [
|
| 38 |
"necesito", "quiero", "buscar", "buscando", "busca",
|
|
@@ -42,11 +38,11 @@ def limpiar_consulta(texto):
|
|
| 42 |
]
|
| 43 |
|
| 44 |
palabras = texto.lower().split()
|
| 45 |
-
|
| 46 |
palabras_limpias = [p for p in palabras if p not in palabras_basura]
|
| 47 |
|
| 48 |
return " ".join(palabras_limpias)
|
| 49 |
|
|
|
|
| 50 |
def separar_consultas(texto):
|
| 51 |
separadores = [" y ", ",", " e "]
|
| 52 |
|
|
@@ -54,66 +50,50 @@ def separar_consultas(texto):
|
|
| 54 |
if sep in texto:
|
| 55 |
return [t.strip() for t in texto.split(sep)]
|
| 56 |
|
| 57 |
-
return [texto]
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
if any(p in mensaje for p in ["horario", "hora", "abren", "atienden"]):
|
| 68 |
-
return "📚 Nuestro horario es:\nCampus Tecnológico: 7am a 7pm\nCasa Lemaitre: 7am a 5pm"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
# 🔎 BÚSQUEDA DE LIBROS
|
| 75 |
if any(p in mensaje for p in ["libro", "buscar", "autor", "necesito", "quiero"]):
|
| 76 |
-
|
| 77 |
consulta = limpiar_consulta(mensaje).strip()
|
| 78 |
-
|
| 79 |
if consulta == "":
|
| 80 |
return "¿Qué libro deseas buscar? 😊"
|
| 81 |
-
|
| 82 |
consultas = separar_consultas(consulta)
|
| 83 |
-
|
| 84 |
respuesta = "🔎 Claro 😊 encontré lo siguiente:\n\n"
|
| 85 |
-
|
| 86 |
for c in consultas:
|
| 87 |
link = generar_link_primo(c)
|
| 88 |
respuesta += f"📚 **{c.capitalize()}**\n👉 [Ver en PRIMO]({link})\n\n"
|
| 89 |
-
|
| 90 |
-
return respuesta
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# 🤖 RESPUESTA POR DEFECTO
|
| 94 |
-
return "Puedo ayudarte con horarios, servicios o búsqueda de libros en PRIMONDE 😊"
|
| 95 |
-
|
| 96 |
-
# Guardar historial
|
| 97 |
-
historial_global.append(mensaje)
|
| 98 |
-
|
| 99 |
-
# 👋 Saludo solo si es el primer mensaje
|
| 100 |
-
if len(historial_global) == 1 and any(s in mensaje for s in saludos):
|
| 101 |
-
return random.choice([
|
| 102 |
-
"¡Hola! 😊 Soy el asistente de la biblioteca. ¿En qué puedo ayudarte?",
|
| 103 |
-
"¡Bienvenido/a! 📚 Estoy aquí para ayudarte con la biblioteca.",
|
| 104 |
-
])
|
| 105 |
-
# 🔎 Buscar libros
|
| 106 |
-
def generar_link_primo(consulta):
|
| 107 |
-
base_url = "https://primo.utb.edu.co/discovery/search"
|
| 108 |
-
|
| 109 |
-
params = {
|
| 110 |
-
"vid": "57UTB_INST:57UTB_INST",
|
| 111 |
-
"query": f"any,contains,{consulta}"
|
| 112 |
-
}
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
-
#
|
| 117 |
for item in base_conocimiento:
|
| 118 |
if any(p in mensaje for p in item["claves"]):
|
| 119 |
return random.choice([
|
|
@@ -121,16 +101,17 @@ def generar_link_primo(consulta):
|
|
| 121 |
f"Te cuento 👇\n\n{item['respuesta']}"
|
| 122 |
])
|
| 123 |
|
| 124 |
-
# 🤖
|
| 125 |
return random.choice([
|
| 126 |
-
"
|
| 127 |
-
"
|
| 128 |
])
|
| 129 |
|
|
|
|
| 130 |
chat = gr.ChatInterface(
|
| 131 |
responder,
|
| 132 |
title="📚 Biblioteca PRIMONDE",
|
| 133 |
-
description="
|
| 134 |
)
|
| 135 |
|
| 136 |
chat.launch()
|
|
|
|
| 6 |
base_conocimiento = [
|
| 7 |
{
|
| 8 |
"claves": ["horario", "hora", "abren", "atienden"],
|
| 9 |
+
"respuesta": "📚 Nuestro horario es:\nCampus Tecnológico: 7am a 7pm\nCasa Lemaitre: 7am a 5pm"
|
| 10 |
},
|
| 11 |
{
|
| 12 |
"claves": ["servicio", "hacen", "ofrecen"],
|
| 13 |
+
"respuesta": "Ofrecemos préstamo de libros, asesorías, PRIMONDE, renovaciones y biblioteca digital."
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"claves": ["norma", "regla", "prohibido", "comer"],
|
|
|
|
| 22 |
}
|
| 23 |
]
|
| 24 |
|
| 25 |
+
# 👋 Saludos
|
| 26 |
saludos = ["hola", "buenas", "buenos días", "buenas tardes", "buenas noches"]
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# 🧠 Memoria simple
|
| 29 |
historial_global = []
|
| 30 |
|
| 31 |
+
# 🧹 Limpiar texto
|
| 32 |
def limpiar_consulta(texto):
|
| 33 |
palabras_basura = [
|
| 34 |
"necesito", "quiero", "buscar", "buscando", "busca",
|
|
|
|
| 38 |
]
|
| 39 |
|
| 40 |
palabras = texto.lower().split()
|
|
|
|
| 41 |
palabras_limpias = [p for p in palabras if p not in palabras_basura]
|
| 42 |
|
| 43 |
return " ".join(palabras_limpias)
|
| 44 |
|
| 45 |
+
# 🔀 Separar múltiples libros
|
| 46 |
def separar_consultas(texto):
|
| 47 |
separadores = [" y ", ",", " e "]
|
| 48 |
|
|
|
|
| 50 |
if sep in texto:
|
| 51 |
return [t.strip() for t in texto.split(sep)]
|
| 52 |
|
| 53 |
+
return [texto]
|
| 54 |
|
| 55 |
+
# 🔗 Generar link PRIMO
|
| 56 |
+
def generar_link_primo(consulta):
|
| 57 |
+
base_url = "https://primo.utb.edu.co/discovery/search"
|
| 58 |
|
| 59 |
+
params = {
|
| 60 |
+
"vid": "57UTB_INST:57UTB_INST",
|
| 61 |
+
"query": f"any,contains,{consulta}"
|
| 62 |
+
}
|
| 63 |
|
| 64 |
+
return f"{base_url}?{urllib.parse.urlencode(params)}"
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# 🤖 Función principal
|
| 67 |
+
def responder(mensaje, historial):
|
| 68 |
+
mensaje = mensaje.lower()
|
| 69 |
+
historial_global.append(mensaje)
|
| 70 |
+
|
| 71 |
+
# 👋 Saludo SOLO si es primer mensaje
|
| 72 |
+
if len(historial_global) == 1 and any(s in mensaje for s in saludos):
|
| 73 |
+
return random.choice([
|
| 74 |
+
"¡Hola! 😊 Soy el asistente de la biblioteca 📚 ¿En qué puedo ayudarte?",
|
| 75 |
+
"¡Bienvenido/a! 😄 Estoy aquí para ayudarte con la biblioteca."
|
| 76 |
+
])
|
| 77 |
|
| 78 |
+
# 🔎 BÚSQUEDA DE LIBROS
|
| 79 |
if any(p in mensaje for p in ["libro", "buscar", "autor", "necesito", "quiero"]):
|
| 80 |
+
|
| 81 |
consulta = limpiar_consulta(mensaje).strip()
|
| 82 |
+
|
| 83 |
if consulta == "":
|
| 84 |
return "¿Qué libro deseas buscar? 😊"
|
| 85 |
+
|
| 86 |
consultas = separar_consultas(consulta)
|
| 87 |
+
|
| 88 |
respuesta = "🔎 Claro 😊 encontré lo siguiente:\n\n"
|
| 89 |
+
|
| 90 |
for c in consultas:
|
| 91 |
link = generar_link_primo(c)
|
| 92 |
respuesta += f"📚 **{c.capitalize()}**\n👉 [Ver en PRIMO]({link})\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
return respuesta
|
| 95 |
|
| 96 |
+
# 📚 RESPUESTAS DE BASE DE CONOCIMIENTO
|
| 97 |
for item in base_conocimiento:
|
| 98 |
if any(p in mensaje for p in item["claves"]):
|
| 99 |
return random.choice([
|
|
|
|
| 101 |
f"Te cuento 👇\n\n{item['respuesta']}"
|
| 102 |
])
|
| 103 |
|
| 104 |
+
# 🤖 RESPUESTA POR DEFECTO
|
| 105 |
return random.choice([
|
| 106 |
+
"Puedo ayudarte con horarios, servicios o búsqueda de libros 📚😊",
|
| 107 |
+
"No tengo esa información exacta 😅, pero puedo ayudarte con la biblioteca."
|
| 108 |
])
|
| 109 |
|
| 110 |
+
# 🎨 Interfaz
|
| 111 |
chat = gr.ChatInterface(
|
| 112 |
responder,
|
| 113 |
title="📚 Biblioteca PRIMONDE",
|
| 114 |
+
description="Asistente virtual de la biblioteca"
|
| 115 |
)
|
| 116 |
|
| 117 |
chat.launch()
|