Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,109 @@
|
|
| 1 |
-
import re
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
import random
|
|
|
|
| 5 |
from fuzzywuzzy import fuzz
|
| 6 |
|
| 7 |
-
#
|
| 8 |
def cargar_dataset(filename="datos_alojamientos.txt"):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
contenido = f.read().strip()
|
| 15 |
-
alojamientos = [bloque.strip() for bloque in contenido.split("\n\n") if bloque.strip()]
|
| 16 |
-
return alojamientos
|
| 17 |
-
except Exception as e:
|
| 18 |
-
print(f"Error al cargar el dataset: {e}")
|
| 19 |
-
return []
|
| 20 |
|
| 21 |
-
#
|
| 22 |
def limpiar_texto(texto):
|
| 23 |
return re.sub(r'\s+', ' ', texto.strip().lower())
|
| 24 |
|
| 25 |
-
# Funci贸n para extraer valores correctamente sin dejar "No especificado"
|
| 26 |
def obtener_valor(texto, patron):
|
| 27 |
match = re.search(patron, texto, re.IGNORECASE | re.MULTILINE)
|
| 28 |
-
|
| 29 |
-
return match.group(1).strip()
|
| 30 |
-
return "No disponible"
|
| 31 |
|
| 32 |
-
# Funci贸n para extraer la lista de servicios correctamente
|
| 33 |
def obtener_servicios(texto):
|
| 34 |
match = re.findall(r"servicios\s*:\s*(.+)", texto, re.IGNORECASE | re.MULTILINE)
|
| 35 |
-
if match
|
| 36 |
-
servicios_lista = [s.strip() for s in match[0].split(",")]
|
| 37 |
-
return "\n".join([f"馃洜 {servicio}" for servicio in servicios_lista])
|
| 38 |
-
return "No especificado"
|
| 39 |
-
|
| 40 |
-
# Funci贸n para buscar coincidencias difusas
|
| 41 |
-
def buscar_coincidencia(pregunta, texto):
|
| 42 |
-
return fuzz.partial_ratio(limpiar_texto(pregunta), limpiar_texto(texto)) > 70
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Funci贸n principal para responder preguntas
|
| 48 |
-
def responder_pregunta(pregunta, historial):
|
| 49 |
-
global resultados_previos
|
| 50 |
pregunta = limpiar_texto(pregunta)
|
| 51 |
resultados = []
|
| 52 |
cantidad_solicitada = next((int(p) for p in pregunta.split() if p.isdigit()), None)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"wi fi", "directv", "ropa blanca", "amueblado", "equipado", "habitaci贸n",
|
| 57 |
-
"cocina", "comedor", "ba帽o", "sommier", "cama", "vajilla completa", "heladera con freezer",
|
| 58 |
-
"tv led", "aire acondicionado", "parrilla", "pileta", "lavarropas", "garage", "quincho", "balc贸n"
|
| 59 |
-
]
|
| 60 |
-
|
| 61 |
-
busca_servicio = next((s for s in palabras_clave_servicios if s in pregunta), None)
|
| 62 |
-
|
| 63 |
for alojamiento in dataset:
|
| 64 |
-
if
|
| 65 |
-
(
|
| 66 |
-
(
|
|
|
|
| 67 |
resultados.append(alojamiento)
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# Funci贸n para mostrar m谩s resultados cuando el usuario escribe "ver m谩s"
|
| 83 |
def ver_mas(historial):
|
| 84 |
global resultados_previos
|
| 85 |
if not resultados_previos:
|
| 86 |
return historial + "\n\nNo hay m谩s alojamientos para mostrar."
|
| 87 |
|
| 88 |
-
|
| 89 |
-
resultados_previos = resultados_previos[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
historial += "\n\n" + "\n\n---\n\n".join(formatear_alojamiento(aloj) for aloj in mostrar_resultados)
|
| 92 |
-
|
| 93 |
-
if len(resultados_previos) > 0:
|
| 94 |
-
historial += "\n\n馃攷 驴Quieres ver m谩s resultados? Escribe 'ver m谩s'."
|
| 95 |
-
|
| 96 |
return historial
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
nombre = obtener_valor(texto, r"(?i)nombre\s*:\s*(.+)")
|
| 101 |
-
direccion = obtener_valor(texto, r"(?i)direcci贸n\s*:\s*(.+)")
|
| 102 |
-
telefono = obtener_valor(texto, r"(?i)tel[e茅]fono\s*:\s*(.+)")
|
| 103 |
-
plazas = obtener_valor(texto, r"(?i)plazas\s*:\s*(.+)")
|
| 104 |
-
mascotas = "S铆" if re.search(r"mascotas\s*:\s*s铆", texto, re.IGNORECASE) else "No"
|
| 105 |
-
wifi = "S铆" if re.search(r"wifi\s*:\s*s铆", texto, re.IGNORECASE) else "No"
|
| 106 |
-
servicios = obtener_servicios(texto)
|
| 107 |
-
|
| 108 |
-
return f"""馃彔 Nombre: {nombre}
|
| 109 |
-
馃搷 Direcci贸n: {direccion}
|
| 110 |
-
馃摓 Tel茅fono: {telefono}
|
| 111 |
-
馃洀 Plazas: {plazas}
|
| 112 |
-
馃惗 Pol铆tica de mascotas: {mascotas}
|
| 113 |
-
馃摱 Wi-Fi: {wifi}
|
| 114 |
-
{servicios}"""
|
| 115 |
-
|
| 116 |
-
# Cargar el dataset
|
| 117 |
-
dataset = cargar_dataset()
|
| 118 |
-
|
| 119 |
-
# Interfaz con Gradio - Mantiene historial del chat
|
| 120 |
-
with gr.Blocks(theme=gr.themes.Default()) as iface:
|
| 121 |
gr.Markdown("### Bienvenido a Valer-IA, tu informante tur铆stico virtual 馃彙")
|
| 122 |
-
gr.Markdown("Escribe una consulta sobre alojamientos, por ejemplo: '驴Qu茅 alojamientos aceptan mascotas?' o 'Alojamiento con pileta y aire acondicionado'")
|
| 123 |
-
|
| 124 |
chat_historial = gr.Textbox(label="Historial de chat", lines=15, interactive=False)
|
| 125 |
pregunta_input = gr.Textbox(label="Escribe tu consulta:")
|
| 126 |
-
|
| 127 |
def actualizar_chat(historial, nueva_pregunta):
|
| 128 |
nueva_pregunta = nueva_pregunta.strip().capitalize()
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
respuesta = "隆Hola! 馃槉 Soy Valer-IA, tu asistente tur铆stico virtual. 驴En qu茅 puedo ayudarte hoy?"
|
| 133 |
-
elif nueva_pregunta.lower() == "ver m谩s":
|
| 134 |
-
return ver_mas(historial), "" # Muestra m谩s resultados sin borrar el historial
|
| 135 |
-
else:
|
| 136 |
-
respuesta = responder_pregunta(nueva_pregunta, historial)
|
| 137 |
-
|
| 138 |
-
return respuesta, ""
|
| 139 |
-
|
| 140 |
-
preguntar_btn = gr.Button("Preguntar")
|
| 141 |
|
| 142 |
-
|
| 143 |
preguntar_btn.click(actualizar_chat, inputs=[chat_historial, pregunta_input], outputs=[chat_historial, pregunta_input])
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
iface.launch()
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import random
|
| 4 |
+
import gradio as gr
|
| 5 |
from fuzzywuzzy import fuzz
|
| 6 |
|
| 7 |
+
# Cargar dataset desde archivo
|
| 8 |
def cargar_dataset(filename="datos_alojamientos.txt"):
|
| 9 |
+
if not os.path.exists(filename):
|
| 10 |
+
raise FileNotFoundError(f"Archivo no encontrado: {filename}")
|
| 11 |
+
with open(filename, "r", encoding="utf-8") as f:
|
| 12 |
+
contenido = f.read().strip()
|
| 13 |
+
return [bloque.strip() for bloque in contenido.split("---") if bloque.strip()]
|
| 14 |
|
| 15 |
+
dataset = cargar_dataset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Normalizaci贸n de texto
|
| 18 |
def limpiar_texto(texto):
|
| 19 |
return re.sub(r'\s+', ' ', texto.strip().lower())
|
| 20 |
|
|
|
|
| 21 |
def obtener_valor(texto, patron):
|
| 22 |
match = re.search(patron, texto, re.IGNORECASE | re.MULTILINE)
|
| 23 |
+
return match.group(1).strip() if match else "No disponible"
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
def obtener_servicios(texto):
|
| 26 |
match = re.findall(r"servicios\s*:\s*(.+)", texto, re.IGNORECASE | re.MULTILINE)
|
| 27 |
+
return "\n".join([f"馃洜 {s.strip()}" for s in match[0].split(",")]) if match else "No especificado"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Filtrar alojamientos
|
| 30 |
+
def filtrar_alojamientos(pregunta):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
pregunta = limpiar_texto(pregunta)
|
| 32 |
resultados = []
|
| 33 |
cantidad_solicitada = next((int(p) for p in pregunta.split() if p.isdigit()), None)
|
| 34 |
+
busca_mascotas = "mascota" in pregunta or "pet friendly" in pregunta
|
| 35 |
+
busca_wifi = "wifi" in pregunta or "internet" in pregunta
|
| 36 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
for alojamiento in dataset:
|
| 38 |
+
if (cantidad_solicitada and f"plazas: {cantidad_solicitada}" in alojamiento.lower()) or \
|
| 39 |
+
(busca_mascotas and "mascotas: s铆" in alojamiento.lower()) or \
|
| 40 |
+
(busca_wifi and "wifi: s铆" in alojamiento.lower()) or \
|
| 41 |
+
fuzz.partial_ratio(pregunta, alojamiento) > 70:
|
| 42 |
resultados.append(alojamiento)
|
| 43 |
+
|
| 44 |
+
random.shuffle(resultados)
|
| 45 |
+
return resultados
|
| 46 |
|
| 47 |
+
# Formatear la respuesta
|
| 48 |
+
def formatear_alojamiento(texto):
|
| 49 |
+
nombre = obtener_valor(texto, r"alojamiento\s*:\s*(.+)")
|
| 50 |
+
direccion = obtener_valor(texto, r"direcci贸n\s*:\s*(.+)")
|
| 51 |
+
telefono = obtener_valor(texto, r"tel[e茅]fono[s]*\s*:\s*(.+)")
|
| 52 |
+
email = obtener_valor(texto, r"email\s*:\s*(.+)")
|
| 53 |
+
plazas = obtener_valor(texto, r"plazas\s*:\s*(\d+)")
|
| 54 |
+
mascotas = "S铆" if "mascotas: s铆" in texto.lower() else "No"
|
| 55 |
+
wifi = "S铆" if "wifi: s铆" in texto.lower() else "No"
|
| 56 |
+
servicios = obtener_servicios(texto)
|
| 57 |
+
return f"""馃彔 **{nombre}**\n馃搷 Direcci贸n: {direccion}\n馃摓 Tel茅fono: {telefono}\n馃摟 Email: {email}\n馃洀 Plazas: {plazas}\n馃惗 Mascotas: {mascotas}\n馃摱 Wi-Fi: {wifi}\n{servicios}"""
|
| 58 |
|
| 59 |
+
# Manejo de paginaci贸n
|
| 60 |
+
resultados_previos = []
|
| 61 |
|
| 62 |
+
def responder_pregunta(pregunta, historial):
|
| 63 |
+
global resultados_previos
|
| 64 |
+
resultados = filtrar_alojamientos(pregunta)
|
| 65 |
+
|
| 66 |
+
if not resultados:
|
| 67 |
+
return "No se encontraron alojamientos que coincidan con su b煤squeda."
|
| 68 |
+
|
| 69 |
+
mostrar = resultados[:3]
|
| 70 |
+
resultados_previos = resultados[3:]
|
| 71 |
+
historial += "\n\n".join(formatear_alojamiento(aloj) for aloj in mostrar)
|
| 72 |
+
|
| 73 |
+
if resultados_previos:
|
| 74 |
+
historial += "\n\n驴Desea ver m谩s opciones?"
|
| 75 |
+
|
| 76 |
+
return historial
|
| 77 |
|
|
|
|
| 78 |
def ver_mas(historial):
|
| 79 |
global resultados_previos
|
| 80 |
if not resultados_previos:
|
| 81 |
return historial + "\n\nNo hay m谩s alojamientos para mostrar."
|
| 82 |
|
| 83 |
+
mostrar = resultados_previos[:3]
|
| 84 |
+
resultados_previos = resultados_previos[3:]
|
| 85 |
+
historial += "\n\n".join(formatear_alojamiento(aloj) for aloj in mostrar)
|
| 86 |
+
|
| 87 |
+
if resultados_previos:
|
| 88 |
+
historial += "\n\n驴Desea ver m谩s opciones?"
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
return historial
|
| 91 |
|
| 92 |
+
# Interfaz con Gradio
|
| 93 |
+
with gr.Blocks() as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
gr.Markdown("### Bienvenido a Valer-IA, tu informante tur铆stico virtual 馃彙")
|
|
|
|
|
|
|
| 95 |
chat_historial = gr.Textbox(label="Historial de chat", lines=15, interactive=False)
|
| 96 |
pregunta_input = gr.Textbox(label="Escribe tu consulta:")
|
| 97 |
+
|
| 98 |
def actualizar_chat(historial, nueva_pregunta):
|
| 99 |
nueva_pregunta = nueva_pregunta.strip().capitalize()
|
| 100 |
+
if nueva_pregunta.lower() == "m谩s" or nueva_pregunta.lower() == "mas":
|
| 101 |
+
return ver_mas(historial), ""
|
| 102 |
+
return responder_pregunta(nueva_pregunta, historial), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
preguntar_btn = gr.Button("Preguntar")
|
| 105 |
preguntar_btn.click(actualizar_chat, inputs=[chat_historial, pregunta_input], outputs=[chat_historial, pregunta_input])
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
iface.launch()
|
| 109 |
+
|