Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import re
|
| 3 |
import random
|
| 4 |
import gradio as gr
|
| 5 |
-
from
|
| 6 |
|
| 7 |
# Cargar dataset desde archivo
|
| 8 |
def cargar_dataset(filename="datos_alojamientos.txt"):
|
|
@@ -14,6 +14,9 @@ def cargar_dataset(filename="datos_alojamientos.txt"):
|
|
| 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())
|
|
@@ -26,23 +29,31 @@ 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
for alojamiento in dataset:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Formatear la respuesta
|
| 48 |
def formatear_alojamiento(texto):
|
|
@@ -105,5 +116,4 @@ with gr.Blocks() as iface:
|
|
| 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 |
-
|
|
|
|
| 2 |
import re
|
| 3 |
import random
|
| 4 |
import gradio as gr
|
| 5 |
+
from transformers import pipeline
|
| 6 |
|
| 7 |
# Cargar dataset desde archivo
|
| 8 |
def cargar_dataset(filename="datos_alojamientos.txt"):
|
|
|
|
| 14 |
|
| 15 |
dataset = cargar_dataset()
|
| 16 |
|
| 17 |
+
# Cargar TinyBERT para b煤squeda sem谩ntica
|
| 18 |
+
tinybert_pipeline = pipeline("feature-extraction", model="huawei-noah/TinyBERT_General_4L_312D")
|
| 19 |
+
|
| 20 |
# Normalizaci贸n de texto
|
| 21 |
def limpiar_texto(texto):
|
| 22 |
return re.sub(r'\s+', ' ', texto.strip().lower())
|
|
|
|
| 29 |
match = re.findall(r"servicios\s*:\s*(.+)", texto, re.IGNORECASE | re.MULTILINE)
|
| 30 |
return "\n".join([f"馃洜 {s.strip()}" for s in match[0].split(",")]) if match else "No especificado"
|
| 31 |
|
| 32 |
+
# Filtrar alojamientos usando TinyBERT
|
| 33 |
def filtrar_alojamientos(pregunta):
|
| 34 |
pregunta = limpiar_texto(pregunta)
|
| 35 |
resultados = []
|
| 36 |
+
|
| 37 |
+
# Convertir la pregunta en un embedding usando TinyBERT
|
| 38 |
+
pregunta_embedding = tinybert_pipeline(pregunta)[0][0] # Extraer el embedding de la pregunta
|
| 39 |
|
| 40 |
for alojamiento in dataset:
|
| 41 |
+
# Convertir el alojamiento en un embedding
|
| 42 |
+
alojamiento_embedding = tinybert_pipeline(alojamiento)[0][0]
|
| 43 |
+
|
| 44 |
+
# Calcular la similitud entre la pregunta y el alojamiento (similitud de coseno)
|
| 45 |
+
similitud = sum(p * a for p, a in zip(pregunta_embedding, alojamiento_embedding)) / (
|
| 46 |
+
(sum(p**2 for p in pregunta_embedding) ** 0.5) * (sum(a**2 for a in alojamiento_embedding) ** 0.5)
|
| 47 |
+
|
| 48 |
+
# Si la similitud es mayor a un umbral, agregar el alojamiento a los resultados
|
| 49 |
+
if similitud > 0.7: # Umbral de similitud ajustable
|
| 50 |
+
resultados.append((alojamiento, similitud))
|
| 51 |
|
| 52 |
+
# Ordenar los resultados por similitud (de mayor a menor)
|
| 53 |
+
resultados.sort(key=lambda x: x[1], reverse=True)
|
| 54 |
+
|
| 55 |
+
# Devolver solo los alojamientos (sin las puntuaciones de similitud)
|
| 56 |
+
return [alojamiento for alojamiento, _ in resultados]
|
| 57 |
|
| 58 |
# Formatear la respuesta
|
| 59 |
def formatear_alojamiento(texto):
|
|
|
|
| 116 |
preguntar_btn.click(actualizar_chat, inputs=[chat_historial, pregunta_input], outputs=[chat_historial, pregunta_input])
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
+
iface.launch()
|
|
|