Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -294,51 +294,70 @@ 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 |
user_symptoms = set(sanitize_text(s) for s in entities.get("sintomas", []))
|
| 298 |
user_foods = set(sanitize_text(f) for f in entities.get("alimentos", []))
|
| 299 |
inferred_condition_raw = sanitize_text(entities.get("condicion_probable", ""))
|
|
|
|
|
|
|
| 300 |
candidate_terms = set(user_foods)
|
| 301 |
for food in user_foods:
|
| 302 |
food_sanitized = sanitize_text(food)
|
| 303 |
if food_sanitized in FOOD_TO_COMPOUND_MAP:
|
| 304 |
candidate_terms.update(c.lower() for c in FOOD_TO_COMPOUND_MAP[food_sanitized])
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
filtered_data = []
|
| 308 |
-
for entry in data:
|
| 309 |
-
entry_condition = sanitize_text(entry.get("condicion_asociada", ""))
|
| 310 |
-
if entry_condition == inferred_condition_raw:
|
| 311 |
-
filtered_data.append(entry)
|
| 312 |
-
continue
|
| 313 |
-
if entry_condition in CONDITION_SYNONYMS:
|
| 314 |
-
if inferred_condition_raw in [sanitize_text(s) for s in CONDITION_SYNONYMS[entry_condition]]:
|
| 315 |
-
filtered_data.append(entry)
|
| 316 |
-
if filtered_data:
|
| 317 |
-
target_data = filtered_data
|
| 318 |
results = []
|
| 319 |
-
|
|
|
|
| 320 |
score_details = {'condition': 0, 'food': 0, 'symptoms': 0, 'total': 0}
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
entry_compounds_text = sanitize_text(entry.get("compuesto_alimento", ""))
|
| 325 |
if any(term in entry_compounds_text for term in candidate_terms):
|
| 326 |
score_details['food'] = 15
|
|
|
|
|
|
|
| 327 |
entry_symptoms_keys = set(sanitize_text(s) for s in entry.get("sintomas_clave", []))
|
| 328 |
symptom_score = 0
|
|
|
|
| 329 |
for user_symptom in user_symptoms:
|
| 330 |
for key in entry_symptoms_keys:
|
| 331 |
if key in user_symptom or user_symptom in key:
|
| 332 |
symptom_score += 10
|
| 333 |
matched_symptoms.append(key)
|
| 334 |
-
break
|
| 335 |
score_details['symptoms'] = symptom_score
|
|
|
|
|
|
|
| 336 |
total_score = sum(score_details.values())
|
|
|
|
|
|
|
| 337 |
if total_score > 0:
|
| 338 |
-
results.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
if not results: return []
|
|
|
|
|
|
|
| 340 |
sorted_results = sorted(results, key=lambda x: x['score']['total'], reverse=True)
|
| 341 |
return sorted_results
|
|
|
|
| 342 |
def generate_detailed_analysis(query, match):
|
| 343 |
if not model: return "Error: El modelo de IA no est谩 disponible."
|
| 344 |
prompt_parts = [
|
|
|
|
| 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:
|
| 306 |
food_sanitized = sanitize_text(food)
|
| 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 MEJORADA (el cambio clave est谩 aqu铆)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
results = []
|
| 312 |
+
# 隆Importante! Siempre iteramos sobre 'data' (la base de datos completa)
|
| 313 |
+
for entry in data:
|
| 314 |
score_details = {'condition': 0, 'food': 0, 'symptoms': 0, 'total': 0}
|
| 315 |
+
|
| 316 |
+
# Bono de Condici贸n: Si la entrada coincide con la inferencia de la IA, recibe un gran bono.
|
| 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 Alimento/Compuesto (sin cambios)
|
| 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 = []
|
| 336 |
for user_symptom in user_symptoms:
|
| 337 |
for key in entry_symptoms_keys:
|
| 338 |
if key in user_symptom or user_symptom in key:
|
| 339 |
symptom_score += 10
|
| 340 |
matched_symptoms.append(key)
|
| 341 |
+
break
|
| 342 |
score_details['symptoms'] = symptom_score
|
| 343 |
+
|
| 344 |
+
# C谩lculo final
|
| 345 |
total_score = sum(score_details.values())
|
| 346 |
+
|
| 347 |
+
# Solo a帽adimos resultados que tengan alguna puntuaci贸n
|
| 348 |
if total_score > 0:
|
| 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 |
+
|
| 357 |
+
# 4. Ordenar y devolver (sin cambios)
|
| 358 |
sorted_results = sorted(results, key=lambda x: x['score']['total'], reverse=True)
|
| 359 |
return sorted_results
|
| 360 |
+
|
| 361 |
def generate_detailed_analysis(query, match):
|
| 362 |
if not model: return "Error: El modelo de IA no est谩 disponible."
|
| 363 |
prompt_parts = [
|