Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def check_image_exists(path):
|
| 6 |
+
"""
|
| 7 |
+
Verifica si la imagen existe en la ruta especificada.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
path (str): Ruta de la imagen.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
bool: True si la imagen existe, False en caso contrario.
|
| 14 |
+
"""
|
| 15 |
+
return os.path.exists(path)
|
| 16 |
+
|
| 17 |
+
def process_tags_chat(search_results):
|
| 18 |
+
"""
|
| 19 |
+
Procesa los resultados de la b煤squeda vectorial para detectar tags espec铆ficos y determinar la URL de la imagen correspondiente.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
search_results (list): Lista de resultados de la b煤squeda vectorial.
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
tuple: (full_response (str), image_url (str o None))
|
| 26 |
+
"""
|
| 27 |
+
# Manejar la l贸gica para procesar tags e im谩genes internamente
|
| 28 |
+
relevant_links = [
|
| 29 |
+
result['metadata'].get(field)
|
| 30 |
+
for result in search_results
|
| 31 |
+
for field in LINK_FIELDS
|
| 32 |
+
if field in result['metadata']
|
| 33 |
+
]
|
| 34 |
+
full_response = ""
|
| 35 |
+
image_url = None
|
| 36 |
+
tags_detected = []
|
| 37 |
+
|
| 38 |
+
filtered_links = []
|
| 39 |
+
if relevant_links:
|
| 40 |
+
# Limitar a solo dos tags
|
| 41 |
+
tag_list = ["rId101", "rId105"]
|
| 42 |
+
for link in relevant_links:
|
| 43 |
+
if any(tag in link for tag in tag_list):
|
| 44 |
+
tags_detected.append(link) # Guardar el tag pero no mostrarlo
|
| 45 |
+
else:
|
| 46 |
+
filtered_links.append(link)
|
| 47 |
+
|
| 48 |
+
# A帽adir el primer enlace relevante bajo una secci贸n "Respuestas relevantes"
|
| 49 |
+
if filtered_links:
|
| 50 |
+
full_response += f"\n\nRespuestas relevantes:\n{filtered_links[0]}"
|
| 51 |
+
|
| 52 |
+
# Manejar las im谩genes basadas en los tags detectados
|
| 53 |
+
tags_to_images = {
|
| 54 |
+
"rId101": "images/rId101.png",
|
| 55 |
+
"rId105": "images/rId105.png",
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
for tag in tags_detected:
|
| 59 |
+
for key, path in tags_to_images.items():
|
| 60 |
+
if key in tag and check_image_exists(path):
|
| 61 |
+
image_url = path
|
| 62 |
+
break # Asumimos solo una imagen relevante
|
| 63 |
+
|
| 64 |
+
return full_response, image_url
|