Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,7 @@ model = get_gemini_model()
|
|
| 41 |
def load_data():
|
| 42 |
try:
|
| 43 |
path_alimentos = os.path.join('DATOS', 'alimentos_enriquecido.json')
|
| 44 |
-
path_foodb_index = os.path.join('DATOS', '
|
| 45 |
with open(path_alimentos, 'r', encoding='utf-8') as f:
|
| 46 |
data_alimentos = json.load(f)
|
| 47 |
foodb_index = {}
|
|
@@ -593,13 +593,12 @@ if st.session_state.search_results is not None:
|
|
| 593 |
st.success(f"Hemos encontrado {len(results)} posible(s) causa(s) relacionada(s) con tu caso.")
|
| 594 |
with col2:
|
| 595 |
report_data = generate_report_text(st.session_state.user_query, results)
|
| 596 |
-
# --- CORRECCIÓN APLICADA AQUÍ ---
|
| 597 |
st.download_button(
|
| 598 |
label="📄 Descargar Informe",
|
| 599 |
data=report_data,
|
| 600 |
file_name=f"informe_detective_{datetime.now().strftime('%Y%m%d')}.txt",
|
| 601 |
mime="text/plain",
|
| 602 |
-
key="download_main_report"
|
| 603 |
)
|
| 604 |
|
| 605 |
st.subheader("Análisis de Relevancia de las Coincidencias")
|
|
@@ -617,45 +616,53 @@ if st.session_state.search_results is not None:
|
|
| 617 |
score_col2.metric("Puntos por Síntomas", f"{best_match_data['score']['symptoms']}")
|
| 618 |
score_col3.metric("PUNTUACIÓN TOTAL", f"{best_match_data['score']['total']}", delta="Máxima coincidencia")
|
| 619 |
|
|
|
|
| 620 |
with col2:
|
| 621 |
st.write("")
|
| 622 |
if foodb_index:
|
| 623 |
-
with st.popover("🔬 Componentes
|
| 624 |
-
st.info("
|
|
|
|
| 625 |
user_foods_mentioned = st.session_state.entities.get("alimentos", [])
|
|
|
|
| 626 |
|
| 627 |
if not user_foods_mentioned:
|
| 628 |
st.warning("No se identificó un alimento específico para buscar.")
|
| 629 |
else:
|
| 630 |
-
|
| 631 |
-
displayed_foodb_keys = set()
|
| 632 |
|
|
|
|
| 633 |
for alimento_es in user_foods_mentioned:
|
| 634 |
-
search_terms_en = []
|
| 635 |
for key_es, value_en_list in FOOD_NAME_TO_FOODB_KEY.items():
|
| 636 |
if key_es in alimento_es.lower():
|
| 637 |
search_terms_en.extend(value_en_list)
|
| 638 |
-
|
| 639 |
-
for term in set(search_terms_en):
|
| 640 |
-
for foodb_key, foodb_data in foodb_index.items():
|
| 641 |
-
if term in foodb_key and foodb_key not in displayed_foodb_keys:
|
| 642 |
-
found_data = True
|
| 643 |
-
displayed_foodb_keys.add(foodb_key)
|
| 644 |
-
with st.container(border=True):
|
| 645 |
-
st.subheader(f"Análisis de: {foodb_key.capitalize()}")
|
| 646 |
-
for item in foodb_data[:3]:
|
| 647 |
-
st.write(f"**Compuesto:** {item['compound']}")
|
| 648 |
-
st.write(f"**Efectos reportados:** {', '.join(item['effects'])}")
|
| 649 |
-
st.markdown("---")
|
| 650 |
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
st.markdown("---")
|
| 655 |
|
| 656 |
with st.container(border=True):
|
| 657 |
st.markdown("##### 🧠 Posibles Efectos Neuropsicológicos de los Componentes")
|
| 658 |
-
|
| 659 |
user_foods = st.session_state.entities.get("alimentos", [])
|
| 660 |
relevant_compounds = set()
|
| 661 |
if user_foods:
|
|
@@ -665,7 +672,7 @@ if st.session_state.search_results is not None:
|
|
| 665 |
|
| 666 |
found_neuro_effect = False
|
| 667 |
if relevant_compounds:
|
| 668 |
-
for compound in sorted(list(relevant_compounds)):
|
| 669 |
if compound in INTEGRATED_NEURO_FOOD_MAP:
|
| 670 |
found_neuro_effect = True
|
| 671 |
effect_info = INTEGRATED_NEURO_FOOD_MAP[compound]
|
|
|
|
| 41 |
def load_data():
|
| 42 |
try:
|
| 43 |
path_alimentos = os.path.join('DATOS', 'alimentos_enriquecido.json')
|
| 44 |
+
path_foodb_index = os.path.join('DATOS', 'foodb_index_smart.json')
|
| 45 |
with open(path_alimentos, 'r', encoding='utf-8') as f:
|
| 46 |
data_alimentos = json.load(f)
|
| 47 |
foodb_index = {}
|
|
|
|
| 593 |
st.success(f"Hemos encontrado {len(results)} posible(s) causa(s) relacionada(s) con tu caso.")
|
| 594 |
with col2:
|
| 595 |
report_data = generate_report_text(st.session_state.user_query, results)
|
|
|
|
| 596 |
st.download_button(
|
| 597 |
label="📄 Descargar Informe",
|
| 598 |
data=report_data,
|
| 599 |
file_name=f"informe_detective_{datetime.now().strftime('%Y%m%d')}.txt",
|
| 600 |
mime="text/plain",
|
| 601 |
+
key="download_main_report"
|
| 602 |
)
|
| 603 |
|
| 604 |
st.subheader("Análisis de Relevancia de las Coincidencias")
|
|
|
|
| 616 |
score_col2.metric("Puntos por Síntomas", f"{best_match_data['score']['symptoms']}")
|
| 617 |
score_col3.metric("PUNTUACIÓN TOTAL", f"{best_match_data['score']['total']}", delta="Máxima coincidencia")
|
| 618 |
|
| 619 |
+
# --- LÓGICA DEL POPOVER INTELIGENTE ---
|
| 620 |
with col2:
|
| 621 |
st.write("")
|
| 622 |
if foodb_index:
|
| 623 |
+
with st.popover("🔬 Componentes Moleculares Relevantes"):
|
| 624 |
+
st.info("Compuestos en los alimentos mencionados que podrían estar relacionados con tus síntomas (datos de FoodB en inglés).")
|
| 625 |
+
|
| 626 |
user_foods_mentioned = st.session_state.entities.get("alimentos", [])
|
| 627 |
+
user_symptoms = st.session_state.entities.get("sintomas", [])
|
| 628 |
|
| 629 |
if not user_foods_mentioned:
|
| 630 |
st.warning("No se identificó un alimento específico para buscar.")
|
| 631 |
else:
|
| 632 |
+
found_relevant_data = False
|
|
|
|
| 633 |
|
| 634 |
+
search_terms_en = []
|
| 635 |
for alimento_es in user_foods_mentioned:
|
|
|
|
| 636 |
for key_es, value_en_list in FOOD_NAME_TO_FOODB_KEY.items():
|
| 637 |
if key_es in alimento_es.lower():
|
| 638 |
search_terms_en.extend(value_en_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
+
for foodb_key, compounds_data in foodb_index.items():
|
| 641 |
+
if any(term in foodb_key.lower() for term in set(search_terms_en)):
|
| 642 |
+
|
| 643 |
+
relevant_compounds = []
|
| 644 |
+
for item in compounds_data:
|
| 645 |
+
effects = item.get("effects", [])
|
| 646 |
+
if any(symptom.lower() in effect.lower() for symptom in user_symptoms for effect in effects):
|
| 647 |
+
relevant_compounds.append(item)
|
| 648 |
+
|
| 649 |
+
if relevant_compounds:
|
| 650 |
+
found_relevant_data = True
|
| 651 |
+
with st.container(border=True):
|
| 652 |
+
st.subheader(f"Análisis de: {foodb_key.capitalize()}")
|
| 653 |
+
for item in relevant_compounds[:5]:
|
| 654 |
+
st.write(f"**Compuesto:** {item['compound']}")
|
| 655 |
+
relevant_effects = [eff for eff in item['effects'] if any(symptom.lower() in eff.lower() for symptom in user_symptoms)]
|
| 656 |
+
st.write(f"**Efectos Relevantes:** {', '.join(relevant_effects)}")
|
| 657 |
+
st.markdown("---")
|
| 658 |
+
|
| 659 |
+
if not found_relevant_data:
|
| 660 |
+
st.warning("No se encontraron compuestos moleculares directamente relacionados con tus síntomas para los alimentos mencionados.")
|
| 661 |
+
|
| 662 |
st.markdown("---")
|
| 663 |
|
| 664 |
with st.container(border=True):
|
| 665 |
st.markdown("##### 🧠 Posibles Efectos Neuropsicológicos de los Componentes")
|
|
|
|
| 666 |
user_foods = st.session_state.entities.get("alimentos", [])
|
| 667 |
relevant_compounds = set()
|
| 668 |
if user_foods:
|
|
|
|
| 672 |
|
| 673 |
found_neuro_effect = False
|
| 674 |
if relevant_compounds:
|
| 675 |
+
for compound in sorted(list(relevant_compounds)):
|
| 676 |
if compound in INTEGRATED_NEURO_FOOD_MAP:
|
| 677 |
found_neuro_effect = True
|
| 678 |
effect_info = INTEGRATED_NEURO_FOOD_MAP[compound]
|