Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,8 @@ import pandas as pd
|
|
| 12 |
import altair as alt
|
| 13 |
from datetime import datetime
|
| 14 |
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
|
|
|
|
|
|
| 15 |
|
| 16 |
st.set_page_config(page_title="El Detective de Alimentos", page_icon="馃崕", layout="wide")
|
| 17 |
|
|
@@ -461,6 +463,33 @@ INTEGRATED_NEURO_FOOD_MAP = {
|
|
| 461 |
}
|
| 462 |
}
|
| 463 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
|
| 465 |
def sanitize_text(text):
|
| 466 |
if not text: return ""
|
|
@@ -753,15 +782,21 @@ if st.session_state.search_results is not None:
|
|
| 753 |
col1, col2 = st.columns([3,1])
|
| 754 |
with col1:
|
| 755 |
st.success(f"Hemos encontrado {len(results)} posible(s) causa(s) relacionada(s) con tu caso.")
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
st.
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 765 |
|
| 766 |
st.subheader("An谩lisis de Relevancia de las Coincidencias")
|
| 767 |
st.altair_chart(create_relevance_chart(results), use_container_width=True)
|
|
|
|
| 12 |
import altair as alt
|
| 13 |
from datetime import datetime
|
| 14 |
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
| 15 |
+
from io import BytesIO
|
| 16 |
+
import docx
|
| 17 |
|
| 18 |
st.set_page_config(page_title="El Detective de Alimentos", page_icon="馃崕", layout="wide")
|
| 19 |
|
|
|
|
| 463 |
}
|
| 464 |
}
|
| 465 |
|
| 466 |
+
def generate_word_report(report_text):
|
| 467 |
+
"""
|
| 468 |
+
Carga una plantilla de Word, reemplaza un marcador de posici贸n y la devuelve como un objeto binario en memoria.
|
| 469 |
+
"""
|
| 470 |
+
try:
|
| 471 |
+
# Carga el documento de plantilla
|
| 472 |
+
template_path = os.path.join("PLANTILLAS", "PLANTILLA_INTERPRETACION.docx")
|
| 473 |
+
doc = docx.Document(template_path)
|
| 474 |
+
|
| 475 |
+
# Busca y reemplaza el marcador de posici贸n
|
| 476 |
+
for paragraph in doc.paragraphs:
|
| 477 |
+
if '<INTERPRETACION>' in paragraph.text:
|
| 478 |
+
# Limpiamos el p谩rrafo del marcador para empezar de cero
|
| 479 |
+
paragraph.clear()
|
| 480 |
+
# A帽adimos el nuevo texto. add_run permite a帽adir m谩s formato si se quiere en el futuro
|
| 481 |
+
paragraph.add_run(report_text)
|
| 482 |
+
|
| 483 |
+
# Guarda el documento modificado en un buffer de memoria
|
| 484 |
+
doc_io = BytesIO()
|
| 485 |
+
doc.save(doc_io)
|
| 486 |
+
doc_io.seek(0) # Rebobina el buffer al principio para que st.download_button pueda leerlo
|
| 487 |
+
|
| 488 |
+
return doc_io
|
| 489 |
+
|
| 490 |
+
except Exception as e:
|
| 491 |
+
logger.error(f"Error al generar el informe de Word: {e}")
|
| 492 |
+
return None
|
| 493 |
|
| 494 |
def sanitize_text(text):
|
| 495 |
if not text: return ""
|
|
|
|
| 782 |
col1, col2 = st.columns([3,1])
|
| 783 |
with col1:
|
| 784 |
st.success(f"Hemos encontrado {len(results)} posible(s) causa(s) relacionada(s) con tu caso.")
|
| 785 |
+
with col2:
|
| 786 |
+
# 1. Genera el contenido de texto como siempre
|
| 787 |
+
report_data_text = generate_report_text(st.session_state.user_query, results)
|
| 788 |
+
|
| 789 |
+
# 2. Procesa la plantilla de Word con ese texto
|
| 790 |
+
word_file_buffer = generate_word_report(report_data_text)
|
| 791 |
+
|
| 792 |
+
if word_file_buffer:
|
| 793 |
+
st.download_button(
|
| 794 |
+
label="馃搫 Descargar Informe (Word)",
|
| 795 |
+
data=word_file_buffer,
|
| 796 |
+
file_name=f"Informe_Detective_Alimentos_{datetime.now().strftime('%Y%m%d')}.docx",
|
| 797 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 798 |
+
key="download_word_report"
|
| 799 |
+
)
|
| 800 |
|
| 801 |
st.subheader("An谩lisis de Relevancia de las Coincidencias")
|
| 802 |
st.altair_chart(create_relevance_chart(results), use_container_width=True)
|