Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -843,7 +843,10 @@ def generate_molecular_report_text(best_match, entities, _foodb_index, _foodb_lo
|
|
| 843 |
return "\n".join(report_lines)
|
| 844 |
|
| 845 |
@st.cache_data(show_spinner=False)
|
| 846 |
-
|
|
|
|
|
|
|
|
|
|
| 847 |
try:
|
| 848 |
template_path = os.path.join("PLANTILLAS", "PLANTILLA_INTERPRETACION.docx")
|
| 849 |
if not os.path.exists(template_path):
|
|
@@ -851,11 +854,17 @@ def generate_word_report_cached(report_text):
|
|
| 851 |
doc.add_paragraph(report_text)
|
| 852 |
else:
|
| 853 |
doc = docx.Document(template_path)
|
|
|
|
| 854 |
for paragraph in doc.paragraphs:
|
| 855 |
if '<INTERPRETACION>' in paragraph.text:
|
| 856 |
for run in list(paragraph.runs):
|
| 857 |
run._element.getparent().remove(run._element)
|
| 858 |
paragraph.add_run(report_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
doc_io = BytesIO()
|
| 860 |
doc.save(doc_io)
|
| 861 |
doc_io.seek(0)
|
|
@@ -864,6 +873,7 @@ def generate_word_report_cached(report_text):
|
|
| 864 |
logger.error(f"Error al generar informe Word: {e}")
|
| 865 |
return None
|
| 866 |
|
|
|
|
| 867 |
def create_relevance_chart(results):
|
| 868 |
top_results = results[:10]
|
| 869 |
data = pd.DataFrame({
|
|
@@ -883,6 +893,7 @@ if 'user_query' not in st.session_state: st.session_state.user_query = ""
|
|
| 883 |
if 'entities' not in st.session_state: st.session_state.entities = None
|
| 884 |
if 'analysis_cache' not in st.session_state: st.session_state.analysis_cache = {}
|
| 885 |
if 'has_searched' not in st.session_state: st.session_state.has_searched = False
|
|
|
|
| 886 |
|
| 887 |
col_img1, col_text, col_img2 = st.columns([1, 4, 1])
|
| 888 |
with col_img1:
|
|
@@ -930,6 +941,8 @@ if submitted:
|
|
| 930 |
st.warning("鈿狅笍 Por favor describe tus s铆ntomas o alimentos consumidos antes de analizar.")
|
| 931 |
|
| 932 |
# --- DISPLAY DE RESULTADOS ---
|
|
|
|
|
|
|
| 933 |
if st.session_state.has_searched:
|
| 934 |
results = st.session_state.search_results
|
| 935 |
|
|
@@ -938,6 +951,8 @@ if st.session_state.has_searched:
|
|
| 938 |
st.success(f"馃攷 Coincidencia Principal: **{best_match.get('condicion_asociada')}**")
|
| 939 |
|
| 940 |
cache_key = f"analysis_{hash(st.session_state.user_query)}_{best_match.get('condicion_asociada')}"
|
|
|
|
|
|
|
| 941 |
if cache_key not in st.session_state.analysis_cache:
|
| 942 |
with st.spinner("鉁嶏笍 Redactando informe..."):
|
| 943 |
analysis = generate_detailed_analysis(
|
|
@@ -945,6 +960,10 @@ if st.session_state.has_searched:
|
|
| 945 |
st.session_state.entities, FOOD_TO_COMPOUND_MAP, INTEGRATED_NEURO_FOOD_MAP
|
| 946 |
)
|
| 947 |
st.session_state.analysis_cache[cache_key] = analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
| 948 |
|
| 949 |
tab_main, tab_neuro, tab_mol = st.tabs(["馃挕 Interpretaci贸n Cl铆nica", "馃 Efectos Neuropsicol贸gicos", "馃敩 An谩lisis Molecular"])
|
| 950 |
|
|
@@ -962,8 +981,8 @@ if st.session_state.has_searched:
|
|
| 962 |
FOOD_NAME_TO_FOODB_KEY, COMPOUND_SYNONYM_MAP, FOOD_TO_COMPOUND_MAP, INTEGRATED_NEURO_FOOD_MAP
|
| 963 |
))
|
| 964 |
|
| 965 |
-
|
| 966 |
-
word_bytes =
|
| 967 |
if word_bytes:
|
| 968 |
st.download_button(
|
| 969 |
label="馃搫 Descargar Informe",
|
|
|
|
| 843 |
return "\n".join(report_lines)
|
| 844 |
|
| 845 |
@st.cache_data(show_spinner=False)
|
| 846 |
+
# --- GENERADOR EFICIENTE DE REPORTE WORD ---
|
| 847 |
+
def generate_word_report(report_text):
|
| 848 |
+
if not report_text:
|
| 849 |
+
return None
|
| 850 |
try:
|
| 851 |
template_path = os.path.join("PLANTILLAS", "PLANTILLA_INTERPRETACION.docx")
|
| 852 |
if not os.path.exists(template_path):
|
|
|
|
| 854 |
doc.add_paragraph(report_text)
|
| 855 |
else:
|
| 856 |
doc = docx.Document(template_path)
|
| 857 |
+
replaced = False
|
| 858 |
for paragraph in doc.paragraphs:
|
| 859 |
if '<INTERPRETACION>' in paragraph.text:
|
| 860 |
for run in list(paragraph.runs):
|
| 861 |
run._element.getparent().remove(run._element)
|
| 862 |
paragraph.add_run(report_text)
|
| 863 |
+
replaced = True
|
| 864 |
+
break # Salir de inmediato tras reemplazar el tag
|
| 865 |
+
if not replaced:
|
| 866 |
+
doc.add_paragraph(report_text)
|
| 867 |
+
|
| 868 |
doc_io = BytesIO()
|
| 869 |
doc.save(doc_io)
|
| 870 |
doc_io.seek(0)
|
|
|
|
| 873 |
logger.error(f"Error al generar informe Word: {e}")
|
| 874 |
return None
|
| 875 |
|
| 876 |
+
|
| 877 |
def create_relevance_chart(results):
|
| 878 |
top_results = results[:10]
|
| 879 |
data = pd.DataFrame({
|
|
|
|
| 893 |
if 'entities' not in st.session_state: st.session_state.entities = None
|
| 894 |
if 'analysis_cache' not in st.session_state: st.session_state.analysis_cache = {}
|
| 895 |
if 'has_searched' not in st.session_state: st.session_state.has_searched = False
|
| 896 |
+
if 'word_bytes_cache' not in st.session_state: st.session_state.word_bytes_cache = {}
|
| 897 |
|
| 898 |
col_img1, col_text, col_img2 = st.columns([1, 4, 1])
|
| 899 |
with col_img1:
|
|
|
|
| 941 |
st.warning("鈿狅笍 Por favor describe tus s铆ntomas o alimentos consumidos antes de analizar.")
|
| 942 |
|
| 943 |
# --- DISPLAY DE RESULTADOS ---
|
| 944 |
+
|
| 945 |
+
# --- DISPLAY DE RESULTADOS OPTIMIZADO ---
|
| 946 |
if st.session_state.has_searched:
|
| 947 |
results = st.session_state.search_results
|
| 948 |
|
|
|
|
| 951 |
st.success(f"馃攷 Coincidencia Principal: **{best_match.get('condicion_asociada')}**")
|
| 952 |
|
| 953 |
cache_key = f"analysis_{hash(st.session_state.user_query)}_{best_match.get('condicion_asociada')}"
|
| 954 |
+
|
| 955 |
+
# GENERACI脫N 脷NICA DEL AN脕LISIS Y DEL DOCUMENTO WORD
|
| 956 |
if cache_key not in st.session_state.analysis_cache:
|
| 957 |
with st.spinner("鉁嶏笍 Redactando informe..."):
|
| 958 |
analysis = generate_detailed_analysis(
|
|
|
|
| 960 |
st.session_state.entities, FOOD_TO_COMPOUND_MAP, INTEGRATED_NEURO_FOOD_MAP
|
| 961 |
)
|
| 962 |
st.session_state.analysis_cache[cache_key] = analysis
|
| 963 |
+
|
| 964 |
+
# Generar el archivo Word UNA SOLA VEZ en memoria
|
| 965 |
+
full_report = f"REPORTE\n\n{analysis}"
|
| 966 |
+
st.session_state.word_bytes_cache[cache_key] = generate_word_report(full_report)
|
| 967 |
|
| 968 |
tab_main, tab_neuro, tab_mol = st.tabs(["馃挕 Interpretaci贸n Cl铆nica", "馃 Efectos Neuropsicol贸gicos", "馃敩 An谩lisis Molecular"])
|
| 969 |
|
|
|
|
| 981 |
FOOD_NAME_TO_FOODB_KEY, COMPOUND_SYNONYM_MAP, FOOD_TO_COMPOUND_MAP, INTEGRATED_NEURO_FOOD_MAP
|
| 982 |
))
|
| 983 |
|
| 984 |
+
# BOT脫N DE DESCARGA R脕PIDO (Cero costo de procesamiento)
|
| 985 |
+
word_bytes = st.session_state.word_bytes_cache.get(cache_key)
|
| 986 |
if word_bytes:
|
| 987 |
st.download_button(
|
| 988 |
label="馃搫 Descargar Informe",
|