Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# ==============================================================================
|
| 2 |
# EL DETECTIVE DE ALIMENTOS - EDICIÓN HÍBRIDA DE ALTA DISPONIBILIDAD
|
| 3 |
# Desarrollado por: Jairo Alexander & Diana Milena Soler Martinez
|
| 4 |
-
# Motor: Gemini 2.5 Flash
|
| 5 |
# ==============================================================================
|
| 6 |
|
| 7 |
import streamlit as st
|
|
@@ -66,10 +66,10 @@ def get_gemini_client():
|
|
| 66 |
logger.warning(f"❌ Error instanciando cliente Gemini: {e}")
|
| 67 |
return None
|
| 68 |
|
| 69 |
-
# --- CASCADA
|
| 70 |
def generate_content_with_fallback(gemini_client, prompt):
|
| 71 |
-
"""
|
| 72 |
-
models_to_try = ['gemini-2.5-flash
|
| 73 |
for model_name in models_to_try:
|
| 74 |
try:
|
| 75 |
response = gemini_client.models.generate_content(
|
|
@@ -78,13 +78,13 @@ def generate_content_with_fallback(gemini_client, prompt):
|
|
| 78 |
)
|
| 79 |
if response and response.text:
|
| 80 |
logger.info(f"✅ Respuesta exitosa generada por el modelo: {model_name}")
|
| 81 |
-
return response.text
|
| 82 |
except Exception as e:
|
| 83 |
-
logger.warning(f"⚠️ Modelo {model_name} no disponible ({e}), probando siguiente
|
| 84 |
continue
|
| 85 |
-
raise RuntimeError("
|
| 86 |
|
| 87 |
-
# --- CARGA ULTRA-RÁPIDA DE DATOS Y FOODB
|
| 88 |
@st.cache_resource
|
| 89 |
def load_data():
|
| 90 |
try:
|
|
@@ -206,7 +206,7 @@ FOOD_TO_COMPOUND_MAP = {
|
|
| 206 |
"calabaza": ["salicilatos", "fodmaps"],
|
| 207 |
"zanahoria": ["salicilatos"],
|
| 208 |
|
| 209 |
-
# FERMENTADOS
|
| 210 |
"kimchi": ["histamina", "tiramina", "sulfitos", "fodmaps"],
|
| 211 |
"kombucha": ["histamina", "alcohol", "fructosa"],
|
| 212 |
|
|
@@ -1042,11 +1042,8 @@ def generate_detailed_analysis(query, match, entities=None, food_compound_map=No
|
|
| 1042 |
NO incluyas descargos de responsabilidad.
|
| 1043 |
"""
|
| 1044 |
try:
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
contents=prompt
|
| 1048 |
-
)
|
| 1049 |
-
ai_report = "🤖 **Análisis Redactado por IA Gemini 2.5 Flash Lite:**\n\n" + response.text + TEXTO_BANDERAS_ROJAS
|
| 1050 |
global_learned_cache[cache_lookup_key] = ai_report
|
| 1051 |
return ai_report
|
| 1052 |
except Exception as e:
|
|
@@ -1203,7 +1200,7 @@ if submitted:
|
|
| 1203 |
st.session_state.search_results = None
|
| 1204 |
st.session_state.entities = None
|
| 1205 |
|
| 1206 |
-
with st.spinner("🔍 Analizando pistas..."):
|
| 1207 |
raw_entities = extract_entities_with_gemini(current_query)
|
| 1208 |
reinforced = reinforce_entities_with_keywords(raw_entities, current_query, FOOD_TO_COMPOUND_MAP, MASTER_SYMPTOM_MAP)
|
| 1209 |
final_symptoms = translate_symptoms_local(reinforced.get("sintomas", []), MASTER_SYMPTOM_MAP)
|
|
|
|
| 1 |
# ==============================================================================
|
| 2 |
# EL DETECTIVE DE ALIMENTOS - EDICIÓN HÍBRIDA DE ALTA DISPONIBILIDAD
|
| 3 |
# Desarrollado por: Jairo Alexander & Diana Milena Soler Martinez
|
| 4 |
+
# Motor: Gemini 2.5 Flash / 2.0 Flash + FooDB.ca Graph + Fast-Path Local Engine
|
| 5 |
# ==============================================================================
|
| 6 |
|
| 7 |
import streamlit as st
|
|
|
|
| 66 |
logger.warning(f"❌ Error instanciando cliente Gemini: {e}")
|
| 67 |
return None
|
| 68 |
|
| 69 |
+
# --- CASCADA CON MODELOS OFICIALES DE GEMINI ---
|
| 70 |
def generate_content_with_fallback(gemini_client, prompt):
|
| 71 |
+
"""Llama a los modelos oficiales vigentes en Google AI Studio en cascada."""
|
| 72 |
+
models_to_try = ['gemini-2.5-flash', 'gemini-2.0-flash', 'gemini-1.5-flash']
|
| 73 |
for model_name in models_to_try:
|
| 74 |
try:
|
| 75 |
response = gemini_client.models.generate_content(
|
|
|
|
| 78 |
)
|
| 79 |
if response and response.text:
|
| 80 |
logger.info(f"✅ Respuesta exitosa generada por el modelo: {model_name}")
|
| 81 |
+
return response.text, model_name
|
| 82 |
except Exception as e:
|
| 83 |
+
logger.warning(f"⚠️ Modelo {model_name} no disponible ({e}), probando siguiente...")
|
| 84 |
continue
|
| 85 |
+
raise RuntimeError("Ningún modelo de Gemini respondió adecuadamente.")
|
| 86 |
|
| 87 |
+
# --- CARGA ULTRA-RÁPIDA DE DATOS Y FOODB ---
|
| 88 |
@st.cache_resource
|
| 89 |
def load_data():
|
| 90 |
try:
|
|
|
|
| 206 |
"calabaza": ["salicilatos", "fodmaps"],
|
| 207 |
"zanahoria": ["salicilatos"],
|
| 208 |
|
| 209 |
+
# FERMENTADOS Y PLANTAS
|
| 210 |
"kimchi": ["histamina", "tiramina", "sulfitos", "fodmaps"],
|
| 211 |
"kombucha": ["histamina", "alcohol", "fructosa"],
|
| 212 |
|
|
|
|
| 1042 |
NO incluyas descargos de responsabilidad.
|
| 1043 |
"""
|
| 1044 |
try:
|
| 1045 |
+
raw_text, used_model = generate_content_with_fallback(gemini_client, prompt)
|
| 1046 |
+
ai_report = f"🤖 **Análisis Redactado por IA Gemini ({used_model}):**\n\n" + raw_text + TEXTO_BANDERAS_ROJAS
|
|
|
|
|
|
|
|
|
|
| 1047 |
global_learned_cache[cache_lookup_key] = ai_report
|
| 1048 |
return ai_report
|
| 1049 |
except Exception as e:
|
|
|
|
| 1200 |
st.session_state.search_results = None
|
| 1201 |
st.session_state.entities = None
|
| 1202 |
|
| 1203 |
+
with st.spinner("🔍 Analizando pistas con Gemini..."):
|
| 1204 |
raw_entities = extract_entities_with_gemini(current_query)
|
| 1205 |
reinforced = reinforce_entities_with_keywords(raw_entities, current_query, FOOD_TO_COMPOUND_MAP, MASTER_SYMPTOM_MAP)
|
| 1206 |
final_symptoms = translate_symptoms_local(reinforced.get("sintomas", []), MASTER_SYMPTOM_MAP)
|