Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -151,18 +151,49 @@ FOOD_NAME_TO_FOODB_KEY = {
|
|
| 151 |
"aceituna": ["olive"], "caf茅": ["coffee"], "caldo": ["broth", "stock"], "cerveza": ["beer"], "chocolate": ["chocolate"], "jengibre": ["ginger"], "c煤rcuma": ["turmeric"], "miel": ["honey"], "mostaza": ["mustard"], "t茅": ["tea"], "vinagre": ["vinegar"], "vino": ["wine", "red wine", "white wine"]
|
| 152 |
}
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
if not entities:
|
| 156 |
entities = {"alimentos": [], "sintomas": []}
|
|
|
|
| 157 |
query_sanitized = sanitize_text(query)
|
|
|
|
|
|
|
| 158 |
current_foods = entities.get("alimentos", [])
|
| 159 |
current_foods_sanitized = {sanitize_text(f) for f in current_foods}
|
| 160 |
for food_keyword in food_map.keys():
|
| 161 |
-
if food_keyword in query_sanitized:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
current_foods.append(food_keyword)
|
| 165 |
entities["alimentos"] = list(set(current_foods))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
return entities
|
| 167 |
|
| 168 |
def sanitize_text(text):
|
|
@@ -405,7 +436,7 @@ if st.session_state.start_analysis:
|
|
| 405 |
else:
|
| 406 |
with st.spinner("馃 Interpretando tu caso y buscando pistas con IA..."):
|
| 407 |
entities = extract_and_infer_with_gemini(query_to_analyze, lista_condiciones)
|
| 408 |
-
entities = reinforce_entities_with_keywords(entities, query_to_analyze, FOOD_TO_COMPOUND_MAP)
|
| 409 |
st.session_state.entities = entities
|
| 410 |
|
| 411 |
if entities and (entities.get("alimentos") or entities.get("sintomas")):
|
|
|
|
| 151 |
"aceituna": ["olive"], "caf茅": ["coffee"], "caldo": ["broth", "stock"], "cerveza": ["beer"], "chocolate": ["chocolate"], "jengibre": ["ginger"], "c煤rcuma": ["turmeric"], "miel": ["honey"], "mostaza": ["mustard"], "t茅": ["tea"], "vinagre": ["vinegar"], "vino": ["wine", "red wine", "white wine"]
|
| 152 |
}
|
| 153 |
|
| 154 |
+
SYMPTOM_KEYWORD_MAP = {
|
| 155 |
+
"dolor de cabeza": ["dolor de cabeza", "cefalea"],
|
| 156 |
+
"hinchaz贸n": ["hinchaz贸n", "distensi贸n", "inflamado"],
|
| 157 |
+
"gases": ["gases", "flatulencia"],
|
| 158 |
+
"diarrea": ["diarrea"],
|
| 159 |
+
"dolor": ["dolor"],
|
| 160 |
+
"fatiga": ["fatiga", "cansancio"],
|
| 161 |
+
"n谩useas": ["n谩useas", "mareo"],
|
| 162 |
+
"v贸mitos": ["v贸mitos"],
|
| 163 |
+
"erupci贸n": ["erupci贸n", "ronchas", "urticaria"],
|
| 164 |
+
"acidez": ["acidez", "reflujo", "ardor de est贸mago"]
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
def reinforce_entities_with_keywords(entities, query, food_map, symptom_map):
|
| 168 |
+
"""
|
| 169 |
+
Revisa la salida de la IA y a帽ade alimentos Y s铆ntomas clave si fueron omitidos.
|
| 170 |
+
Esta es una red de seguridad robusta contra las fallas de extracci贸n de la IA.
|
| 171 |
+
"""
|
| 172 |
if not entities:
|
| 173 |
entities = {"alimentos": [], "sintomas": []}
|
| 174 |
+
|
| 175 |
query_sanitized = sanitize_text(query)
|
| 176 |
+
|
| 177 |
+
# --- Reforzar Alimentos (L贸gica existente) ---
|
| 178 |
current_foods = entities.get("alimentos", [])
|
| 179 |
current_foods_sanitized = {sanitize_text(f) for f in current_foods}
|
| 180 |
for food_keyword in food_map.keys():
|
| 181 |
+
if food_keyword in query_sanitized and food_keyword not in current_foods_sanitized:
|
| 182 |
+
logger.info(f"Red de seguridad (Alimento): A帽adiendo '{food_keyword}'.")
|
| 183 |
+
current_foods.append(food_keyword)
|
|
|
|
| 184 |
entities["alimentos"] = list(set(current_foods))
|
| 185 |
+
|
| 186 |
+
# --- NUEVA L脫GICA: Reforzar S铆ntomas ---
|
| 187 |
+
current_symptoms = entities.get("sintomas", [])
|
| 188 |
+
# A veces la IA devuelve None en la lista, lo limpiamos por seguridad
|
| 189 |
+
if current_symptoms is None: current_symptoms = []
|
| 190 |
+
current_symptoms_sanitized = {sanitize_text(s) for s in current_symptoms}
|
| 191 |
+
for symptom_keyword in symptom_map.keys():
|
| 192 |
+
if symptom_keyword in query_sanitized and symptom_keyword not in current_symptoms_sanitized:
|
| 193 |
+
logger.info(f"Red de seguridad (S铆ntoma): A帽adiendo '{symptom_keyword}'.")
|
| 194 |
+
current_symptoms.append(symptom_keyword)
|
| 195 |
+
entities["sintomas"] = list(set(current_symptoms))
|
| 196 |
+
|
| 197 |
return entities
|
| 198 |
|
| 199 |
def sanitize_text(text):
|
|
|
|
| 436 |
else:
|
| 437 |
with st.spinner("馃 Interpretando tu caso y buscando pistas con IA..."):
|
| 438 |
entities = extract_and_infer_with_gemini(query_to_analyze, lista_condiciones)
|
| 439 |
+
entities = reinforce_entities_with_keywords(entities, query_to_analyze, FOOD_TO_COMPOUND_MAP, SYMPTOM_KEYWORD_MAP)
|
| 440 |
st.session_state.entities = entities
|
| 441 |
|
| 442 |
if entities and (entities.get("alimentos") or entities.get("sintomas")):
|