Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -294,12 +294,12 @@ def extract_and_infer_with_gemini(query, condiciones):
|
|
| 294 |
return None
|
| 295 |
def find_best_matches_hybrid(entities, data):
|
| 296 |
if not entities or not data: return []
|
| 297 |
-
|
| 298 |
# 1. Extracci贸n de entidades del usuario (sin cambios)
|
| 299 |
user_symptoms = set(sanitize_text(s) for s in entities.get("sintomas", []))
|
| 300 |
user_foods = set(sanitize_text(f) for f in entities.get("alimentos", []))
|
| 301 |
inferred_condition_raw = sanitize_text(entities.get("condicion_probable", ""))
|
| 302 |
-
|
| 303 |
# 2. Obtenci贸n de t茅rminos de b煤squeda (sin cambios)
|
| 304 |
candidate_terms = set(user_foods)
|
| 305 |
for food in user_foods:
|
|
@@ -307,29 +307,31 @@ def find_best_matches_hybrid(entities, data):
|
|
| 307 |
if food_sanitized in FOOD_TO_COMPOUND_MAP:
|
| 308 |
candidate_terms.update(c.lower() for c in FOOD_TO_COMPOUND_MAP[food_sanitized])
|
| 309 |
|
| 310 |
-
# 3. L贸gica de puntuaci贸n
|
| 311 |
results = []
|
| 312 |
-
# 隆Importante! Siempre iteramos sobre 'data' (la base de datos completa)
|
| 313 |
for entry in data:
|
| 314 |
-
|
|
|
|
|
|
|
| 315 |
|
| 316 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
if inferred_condition_raw:
|
| 318 |
entry_condition_sanitized = sanitize_text(entry.get("condicion_asociada", ""))
|
| 319 |
is_match = (entry_condition_sanitized == inferred_condition_raw)
|
| 320 |
-
# Comprobar tambi茅n sin贸nimos
|
| 321 |
if not is_match and entry_condition_sanitized in CONDITION_SYNONYMS:
|
| 322 |
if inferred_condition_raw in [sanitize_text(s) for s in CONDITION_SYNONYMS[entry_condition_sanitized]]:
|
| 323 |
is_match = True
|
| 324 |
if is_match:
|
| 325 |
score_details['condition'] = 100
|
| 326 |
|
| 327 |
-
# Puntuaci贸n por
|
| 328 |
-
entry_compounds_text = sanitize_text(entry.get("compuesto_alimento", ""))
|
| 329 |
-
if any(term in entry_compounds_text for term in candidate_terms):
|
| 330 |
-
score_details['food'] = 15
|
| 331 |
-
|
| 332 |
-
# Puntuaci贸n por S铆ntomas (sin cambios)
|
| 333 |
entry_symptoms_keys = set(sanitize_text(s) for s in entry.get("sintomas_clave", []))
|
| 334 |
symptom_score = 0
|
| 335 |
matched_symptoms = []
|
|
@@ -344,13 +346,11 @@ def find_best_matches_hybrid(entities, data):
|
|
| 344 |
# C谩lculo final
|
| 345 |
total_score = sum(score_details.values())
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
'matched_symptoms': list(set(matched_symptoms))
|
| 353 |
-
})
|
| 354 |
|
| 355 |
if not results: return []
|
| 356 |
|
|
|
|
| 294 |
return None
|
| 295 |
def find_best_matches_hybrid(entities, data):
|
| 296 |
if not entities or not data: return []
|
| 297 |
+
|
| 298 |
# 1. Extracci贸n de entidades del usuario (sin cambios)
|
| 299 |
user_symptoms = set(sanitize_text(s) for s in entities.get("sintomas", []))
|
| 300 |
user_foods = set(sanitize_text(f) for f in entities.get("alimentos", []))
|
| 301 |
inferred_condition_raw = sanitize_text(entities.get("condicion_probable", ""))
|
| 302 |
+
|
| 303 |
# 2. Obtenci贸n de t茅rminos de b煤squeda (sin cambios)
|
| 304 |
candidate_terms = set(user_foods)
|
| 305 |
for food in user_foods:
|
|
|
|
| 307 |
if food_sanitized in FOOD_TO_COMPOUND_MAP:
|
| 308 |
candidate_terms.update(c.lower() for c in FOOD_TO_COMPOUND_MAP[food_sanitized])
|
| 309 |
|
| 310 |
+
# 3. L贸gica de puntuaci贸n CORREGIDA Y ROBUSTA
|
| 311 |
results = []
|
|
|
|
| 312 |
for entry in data:
|
| 313 |
+
# --- PASO CLAVE: FILTRO OBLIGATORIO POR ALIMENTO ---
|
| 314 |
+
entry_compounds_text = sanitize_text(entry.get("compuesto_alimento", ""))
|
| 315 |
+
food_match = any(term in entry_compounds_text for term in candidate_terms)
|
| 316 |
|
| 317 |
+
# Si NO hay conexi贸n entre la comida del usuario y la condici贸n, se ignora por completo.
|
| 318 |
+
if not food_match:
|
| 319 |
+
continue
|
| 320 |
+
|
| 321 |
+
# --- Si pasa el filtro, procedemos a puntuar ---
|
| 322 |
+
score_details = {'condition': 0, 'food': 15, 'symptoms': 0, 'total': 0} # Damos puntos de comida por defecto
|
| 323 |
+
|
| 324 |
+
# Bono de Condici贸n: Si la IA la sugiri贸, recibe un gran bono.
|
| 325 |
if inferred_condition_raw:
|
| 326 |
entry_condition_sanitized = sanitize_text(entry.get("condicion_asociada", ""))
|
| 327 |
is_match = (entry_condition_sanitized == inferred_condition_raw)
|
|
|
|
| 328 |
if not is_match and entry_condition_sanitized in CONDITION_SYNONYMS:
|
| 329 |
if inferred_condition_raw in [sanitize_text(s) for s in CONDITION_SYNONYMS[entry_condition_sanitized]]:
|
| 330 |
is_match = True
|
| 331 |
if is_match:
|
| 332 |
score_details['condition'] = 100
|
| 333 |
|
| 334 |
+
# Puntuaci贸n por S铆ntomas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
entry_symptoms_keys = set(sanitize_text(s) for s in entry.get("sintomas_clave", []))
|
| 336 |
symptom_score = 0
|
| 337 |
matched_symptoms = []
|
|
|
|
| 346 |
# C谩lculo final
|
| 347 |
total_score = sum(score_details.values())
|
| 348 |
|
| 349 |
+
results.append({
|
| 350 |
+
'entry': entry,
|
| 351 |
+
'score': score_details,
|
| 352 |
+
'matched_symptoms': list(set(matched_symptoms))
|
| 353 |
+
})
|
|
|
|
|
|
|
| 354 |
|
| 355 |
if not results: return []
|
| 356 |
|