Spaces:
Build error
Build error
enable doc search
Browse files- app/main.py +96 -20
app/main.py
CHANGED
|
@@ -80,15 +80,13 @@ async def process_prompt(
|
|
| 80 |
group_res = agrupar_etiquetas_repetidas(prompt_res)
|
| 81 |
procesar_entidades(group_res)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# documentos_transformados.append(documento)
|
| 89 |
-
# json_response = {"data": documentos_transformados, "query": tag_res, "model": prompt_res}
|
| 90 |
|
| 91 |
-
response_data = {"
|
| 92 |
return response_data
|
| 93 |
|
| 94 |
except Exception as e:
|
|
@@ -118,18 +116,29 @@ def agrupar_etiquetas_repetidas(entidades):
|
|
| 118 |
resultado.append((textos[0], etiqueta))
|
| 119 |
return resultado
|
| 120 |
|
| 121 |
-
def procesar_entidades(entidades
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
datos_procesados = {}
|
| 123 |
datos_procesados["ubicacion"] = []
|
| 124 |
op1 = ["minima", "menor", "m铆nima"]
|
| 125 |
-
op2 = ["maxima", "mayor", "m谩xima"]
|
|
|
|
| 126 |
for texto, etiqueta in entidades:
|
| 127 |
if etiqueta == "PROPERTY_TYPE":
|
| 128 |
if isinstance(texto, tuple):
|
| 129 |
for t in texto:
|
| 130 |
if t in ["apartamento", "casa", "local", "finca", "oficina", "bodega", "lote"]:
|
| 131 |
filter["type"].append(t)
|
| 132 |
-
else:
|
| 133 |
if texto in ["apartamento", "casa", "local", "finca", "oficina", "bodega", "lote"]:
|
| 134 |
filter["type"].append(texto)
|
| 135 |
elif etiqueta == "OPERATION":
|
|
@@ -139,11 +148,14 @@ def procesar_entidades(entidades, filter):
|
|
| 139 |
filter["operation"] = "Venta"
|
| 140 |
else:
|
| 141 |
filter["operation"] = "Arriendo"
|
| 142 |
-
elif etiqueta == "
|
|
|
|
|
|
|
|
|
|
| 143 |
if isinstance(texto, tuple):
|
| 144 |
for t in texto:
|
| 145 |
datos_procesados["ubicacion"].append(t)
|
| 146 |
-
else:
|
| 147 |
datos_procesados["ubicacion"].append(texto)
|
| 148 |
elif etiqueta == "BEDROOMS":
|
| 149 |
if isinstance(texto, tuple):
|
|
@@ -151,11 +163,12 @@ def procesar_entidades(entidades, filter):
|
|
| 151 |
numero_habitaciones = re.search(r"\d+", t)
|
| 152 |
if numero_habitaciones:
|
| 153 |
filter["habitaciones"].append(int(numero_habitaciones.group(0)))
|
| 154 |
-
|
|
|
|
| 155 |
numero_habitaciones = re.search(r"\d+", texto)
|
| 156 |
if numero_habitaciones:
|
| 157 |
filter["habitaciones"].append(int(numero_habitaciones.group(0)))
|
| 158 |
-
else:
|
| 159 |
numero_habitaciones = re.search(r"^un|una", texto)
|
| 160 |
if numero_habitaciones:
|
| 161 |
filter["habitaciones"].append(1)
|
|
@@ -165,14 +178,16 @@ def procesar_entidades(entidades, filter):
|
|
| 165 |
numero_garajes = re.search(r"\d+", t)
|
| 166 |
if numero_garajes:
|
| 167 |
filter["garajes"].append(int(numero_garajes.group(0)))
|
|
|
|
| 168 |
else:
|
| 169 |
numero_garajes = re.search(r"\d+", texto)
|
| 170 |
if numero_garajes:
|
| 171 |
filter["garajes"].append(int(numero_garajes.group(0)))
|
| 172 |
-
else:
|
| 173 |
numero_garajes = re.search(r"^un|una", texto)
|
| 174 |
if numero_garajes:
|
| 175 |
filter["garajes"].append(1)
|
|
|
|
| 176 |
elif etiqueta == "AREA_SIZE":
|
| 177 |
medida_area = re.search(r"(\d+)\s*?(m2|hect谩reas|metros|m)", texto, re.IGNORECASE)
|
| 178 |
area = 50
|
|
@@ -188,6 +203,7 @@ def procesar_entidades(entidades, filter):
|
|
| 188 |
else:
|
| 189 |
filter["areaMin"] = area - area*0.2
|
| 190 |
filter["areaMax"] = area + area*0.2
|
|
|
|
| 191 |
elif etiqueta == "COST":
|
| 192 |
if isinstance(texto, tuple):
|
| 193 |
for t in texto:
|
|
@@ -195,11 +211,14 @@ def procesar_entidades(entidades, filter):
|
|
| 195 |
valor = 0
|
| 196 |
if valor_costo:
|
| 197 |
valor = int(valor_costo.group(0))
|
|
|
|
|
|
|
| 198 |
if filter["min"] < valor:
|
| 199 |
-
filter["min"] = filter["max"]
|
| 200 |
filter["max"] = valor
|
|
|
|
| 201 |
else:
|
| 202 |
-
filter["min"] = filter["max"]
|
| 203 |
filter["max"] = valor
|
| 204 |
else:
|
| 205 |
valor_costo = re.search(r"\d+", texto)
|
|
@@ -223,7 +242,64 @@ def procesar_entidades(entidades, filter):
|
|
| 223 |
numero_banos = re.search(r"\d+", texto)
|
| 224 |
if numero_banos:
|
| 225 |
filter["banos"].append(int(numero_banos.group(0)))
|
| 226 |
-
else:
|
| 227 |
numero_banos = re.search(r"^un|una", texto)
|
| 228 |
if numero_banos:
|
| 229 |
-
filter["banos"].append(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
group_res = agrupar_etiquetas_repetidas(prompt_res)
|
| 81 |
procesar_entidades(group_res)
|
| 82 |
|
| 83 |
+
docs = buscar_documentos(filter, collection)
|
| 84 |
+
documentos_transformados = []
|
| 85 |
+
for documento in docs:
|
| 86 |
+
documento["_id"] = str(documento["_id"]) # Convertir ObjectId a string
|
| 87 |
+
documentos_transformados.append(documento)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
response_data = {"data": documentos_transformados, "model": prompt_res}
|
| 90 |
return response_data
|
| 91 |
|
| 92 |
except Exception as e:
|
|
|
|
| 116 |
resultado.append((textos[0], etiqueta))
|
| 117 |
return resultado
|
| 118 |
|
| 119 |
+
def procesar_entidades(entidades):
|
| 120 |
+
"""
|
| 121 |
+
Procesa las entidades extra铆das, validando y extrayendo informaci贸n espec铆fica.
|
| 122 |
+
|
| 123 |
+
Args:
|
| 124 |
+
entidades (list): Una lista de tuplas (texto, etiqueta).
|
| 125 |
+
|
| 126 |
+
Returns:
|
| 127 |
+
dict: Un diccionario con los datos procesados y validados.
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
datos_procesados = {}
|
| 131 |
datos_procesados["ubicacion"] = []
|
| 132 |
op1 = ["minima", "menor", "m铆nima"]
|
| 133 |
+
op2 = [ "maxima", "mayor", "m谩xima"]
|
| 134 |
+
|
| 135 |
for texto, etiqueta in entidades:
|
| 136 |
if etiqueta == "PROPERTY_TYPE":
|
| 137 |
if isinstance(texto, tuple):
|
| 138 |
for t in texto:
|
| 139 |
if t in ["apartamento", "casa", "local", "finca", "oficina", "bodega", "lote"]:
|
| 140 |
filter["type"].append(t)
|
| 141 |
+
else:
|
| 142 |
if texto in ["apartamento", "casa", "local", "finca", "oficina", "bodega", "lote"]:
|
| 143 |
filter["type"].append(texto)
|
| 144 |
elif etiqueta == "OPERATION":
|
|
|
|
| 148 |
filter["operation"] = "Venta"
|
| 149 |
else:
|
| 150 |
filter["operation"] = "Arriendo"
|
| 151 |
+
# elif etiqueta == "LOCATION_TYPE":
|
| 152 |
+
# if texto in ["barrio", "vereda", "centro", "zona"]:
|
| 153 |
+
# datos_procesados["tipo_ubicacion"] = texto
|
| 154 |
+
elif etiqueta == "LOCATION":
|
| 155 |
if isinstance(texto, tuple):
|
| 156 |
for t in texto:
|
| 157 |
datos_procesados["ubicacion"].append(t)
|
| 158 |
+
else:
|
| 159 |
datos_procesados["ubicacion"].append(texto)
|
| 160 |
elif etiqueta == "BEDROOMS":
|
| 161 |
if isinstance(texto, tuple):
|
|
|
|
| 163 |
numero_habitaciones = re.search(r"\d+", t)
|
| 164 |
if numero_habitaciones:
|
| 165 |
filter["habitaciones"].append(int(numero_habitaciones.group(0)))
|
| 166 |
+
|
| 167 |
+
else:
|
| 168 |
numero_habitaciones = re.search(r"\d+", texto)
|
| 169 |
if numero_habitaciones:
|
| 170 |
filter["habitaciones"].append(int(numero_habitaciones.group(0)))
|
| 171 |
+
else :
|
| 172 |
numero_habitaciones = re.search(r"^un|una", texto)
|
| 173 |
if numero_habitaciones:
|
| 174 |
filter["habitaciones"].append(1)
|
|
|
|
| 178 |
numero_garajes = re.search(r"\d+", t)
|
| 179 |
if numero_garajes:
|
| 180 |
filter["garajes"].append(int(numero_garajes.group(0)))
|
| 181 |
+
|
| 182 |
else:
|
| 183 |
numero_garajes = re.search(r"\d+", texto)
|
| 184 |
if numero_garajes:
|
| 185 |
filter["garajes"].append(int(numero_garajes.group(0)))
|
| 186 |
+
else :
|
| 187 |
numero_garajes = re.search(r"^un|una", texto)
|
| 188 |
if numero_garajes:
|
| 189 |
filter["garajes"].append(1)
|
| 190 |
+
|
| 191 |
elif etiqueta == "AREA_SIZE":
|
| 192 |
medida_area = re.search(r"(\d+)\s*?(m2|hect谩reas|metros|m)", texto, re.IGNORECASE)
|
| 193 |
area = 50
|
|
|
|
| 203 |
else:
|
| 204 |
filter["areaMin"] = area - area*0.2
|
| 205 |
filter["areaMax"] = area + area*0.2
|
| 206 |
+
|
| 207 |
elif etiqueta == "COST":
|
| 208 |
if isinstance(texto, tuple):
|
| 209 |
for t in texto:
|
|
|
|
| 211 |
valor = 0
|
| 212 |
if valor_costo:
|
| 213 |
valor = int(valor_costo.group(0))
|
| 214 |
+
|
| 215 |
+
|
| 216 |
if filter["min"] < valor:
|
| 217 |
+
filter["min"] = filter["max"]
|
| 218 |
filter["max"] = valor
|
| 219 |
+
|
| 220 |
else:
|
| 221 |
+
filter["min"] = filter["max"]
|
| 222 |
filter["max"] = valor
|
| 223 |
else:
|
| 224 |
valor_costo = re.search(r"\d+", texto)
|
|
|
|
| 242 |
numero_banos = re.search(r"\d+", texto)
|
| 243 |
if numero_banos:
|
| 244 |
filter["banos"].append(int(numero_banos.group(0)))
|
| 245 |
+
else :
|
| 246 |
numero_banos = re.search(r"^un|una", texto)
|
| 247 |
if numero_banos:
|
| 248 |
+
filter["banos"].append(1)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def buscar_documentos(datos_procesados, collection):
|
| 254 |
+
"""
|
| 255 |
+
Busca documentos en MongoDB basados en los datos procesados.
|
| 256 |
+
|
| 257 |
+
Args:
|
| 258 |
+
datos_procesados (dict): Diccionario con los datos procesados.
|
| 259 |
+
db_nombre (str): Nombre de la base de datos.
|
| 260 |
+
collection_nombre (str): Nombre de la colecci贸n.
|
| 261 |
+
|
| 262 |
+
Returns:
|
| 263 |
+
list: Una lista de documentos que coinciden con la consulta.
|
| 264 |
+
"""
|
| 265 |
+
|
| 266 |
+
query = {}
|
| 267 |
+
|
| 268 |
+
if "tipo_propiedad" in datos_procesados:
|
| 269 |
+
query["technicalSheet.property_type_name"] = datos_procesados["tipo_propiedad"]
|
| 270 |
+
if "operacion" in datos_procesados:
|
| 271 |
+
query["operation"] = datos_procesados["operacion"]
|
| 272 |
+
if "ubicacion" in datos_procesados:
|
| 273 |
+
ubicacion = datos_procesados["ubicacion"]
|
| 274 |
+
if len(ubicacion) > 0:
|
| 275 |
+
if len(ubicacion) == 1:
|
| 276 |
+
query["url_frag"] = {"$regex": ubicacion[0], "$options": "i"}
|
| 277 |
+
else:
|
| 278 |
+
regex_pattern = "|".join(datos_procesados["ubicacion"])
|
| 279 |
+
query["url_frag"] = {"$regex": regex_pattern, "$options": "i"}
|
| 280 |
+
if "habitaciones" in datos_procesados:
|
| 281 |
+
query["technicalSheet.bedrooms"] = datos_procesados["habitaciones"]
|
| 282 |
+
if "garajes" in datos_procesados:
|
| 283 |
+
query["technicalSheet.garage"] = datos_procesados["garajes"]
|
| 284 |
+
if "area" in datos_procesados:
|
| 285 |
+
if datos_procesados["operacion_area"] is not None:
|
| 286 |
+
if datos_procesados["operacion_area"] == "minima":
|
| 287 |
+
query["area"] = {"$gte": datos_procesados["area"]["valor"]}
|
| 288 |
+
elif datos_procesados["operacion_area"] == "maxima":
|
| 289 |
+
query["area"] = {"$lte": datos_procesados["area"]["valor"]}
|
| 290 |
+
else:
|
| 291 |
+
area_value = datos_procesados["area"]["valor"]
|
| 292 |
+
query["area"] = {"$lte": area_value + area_value*0.2, "$gte": area_value - area_value*0.2}
|
| 293 |
+
|
| 294 |
+
if "costos" in datos_procesados:
|
| 295 |
+
if datos_procesados["operacion_costo"] == "precio m谩ximo":
|
| 296 |
+
query["costs.sale_amount"] = {"$lte": datos_procesados["costos"][0]}
|
| 297 |
+
elif datos_procesados["operacion_costo"] == "valor de arriendo":
|
| 298 |
+
query["costs.rental_amount"] = {"$gte": datos_procesados["costos"][0]}
|
| 299 |
+
else:
|
| 300 |
+
query["costs.rental_amount"] = {"$gte": datos_procesados["costos"][0], "$lte": datos_procesados["costos"][-1]}
|
| 301 |
+
if "banos" in datos_procesados:
|
| 302 |
+
query["technicalSheet.bathrooms"] = datos_procesados["banos"]
|
| 303 |
+
|
| 304 |
+
documentos = list(collection.find(query).limit(10))
|
| 305 |
+
return documentos
|