Iris314 commited on
Commit
d03ccba
·
verified ·
1 Parent(s): c30abbb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -77,7 +77,7 @@ RECIPES_DF = load_recipes()
77
  # }
78
 
79
  EXAMPLE_IDS = [
80
- uid for uid in ("user_1", "user_2", "user_3", "user_5")
81
  if (USER_DATA_DIR / uid / "user_profile.json").exists()
82
  ]
83
 
@@ -228,9 +228,13 @@ def render_recommendations(df, user_parents=None):
228
 
229
  # --- Region / Cuisine ---
230
  region = row.get("region", "Unavailable")
 
 
 
 
231
  cuisine = _ensure_iterable(row.get("cuisine_attr"))
232
  cuisine_str = ", ".join(cuisine) if cuisine else "Unavailable"
233
- md_lines.append(f" - Region: {region}")
234
  md_lines.append(f" - Cuisine: {cuisine_str}")
235
 
236
  # --- Nutrition ---
@@ -363,6 +367,13 @@ def run_pipeline(
363
  output_image=str(output_image),
364
  )
365
  Path(upload_path).unlink(missing_ok=True)
 
 
 
 
 
 
 
366
 
367
  #2: Always create/update user profile with current UI values
368
  profile = build_user_profile(
@@ -390,7 +401,27 @@ def run_pipeline(
390
  topk=5,
391
  )
392
 
 
 
 
 
 
 
 
 
393
  ingredient_summary = summarize_ingredients(user_parents, high_conf, low_conf)
 
 
 
 
 
 
 
 
 
 
 
 
394
  recommendation_md, feedback_rows = render_recommendations(ml_top, user_parents)
395
 
396
 
@@ -414,16 +445,14 @@ def run_pipeline(
414
  profile_status,
415
  )
416
  except Exception as exc:
417
- import traceback
418
- error_detail = traceback.format_exc()
419
  return (
420
  None,
 
 
421
  "",
422
- "",
423
- f"⚠️ Error: {exc}\n\nDetails:\n{error_detail}",
424
  gr.update(choices=[], value=None),
425
  [],
426
- f"⚠️ Error: {exc}",
427
  )
428
 
429
 
@@ -579,7 +608,6 @@ with gr.Blocks(title="Smart Fridge Recipe Assistant", theme=gr.themes.Soft()) as
579
  value=10,
580
  step=5,
581
  label="Protein Min (g)",
582
- container=False
583
  )
584
  protein_max_slider = gr.Slider(
585
  minimum=0,
@@ -587,7 +615,6 @@ with gr.Blocks(title="Smart Fridge Recipe Assistant", theme=gr.themes.Soft()) as
587
  value=160,
588
  step=5,
589
  label="Protein Max (g)",
590
- container=False
591
  )
592
  preferred_box = gr.Textbox(
593
  label="Preferred main ingredients",
 
77
  # }
78
 
79
  EXAMPLE_IDS = [
80
+ uid for uid in ("user_1", "user_2", "user_3", "user_4", "user_5")
81
  if (USER_DATA_DIR / uid / "user_profile.json").exists()
82
  ]
83
 
 
228
 
229
  # --- Region / Cuisine ---
230
  region = row.get("region", "Unavailable")
231
+ if isinstance(region, (set, list, tuple)):
232
+ region_str = ", ".join(str(x) for x in region)
233
+ else:
234
+ region_str = str(region)
235
  cuisine = _ensure_iterable(row.get("cuisine_attr"))
236
  cuisine_str = ", ".join(cuisine) if cuisine else "Unavailable"
237
+ md_lines.append(f" - Region: {region_str}")
238
  md_lines.append(f" - Cuisine: {cuisine_str}")
239
 
240
  # --- Nutrition ---
 
367
  output_image=str(output_image),
368
  )
369
  Path(upload_path).unlink(missing_ok=True)
370
+ detection_payload = detection_result.get("recipe_json")
371
+ if not detection_payload:
372
+ raise ValueError("No detection result returned. Please try again with a clearer photo.")
373
+
374
+ ingredients = detection_payload.get("ingredients", [])
375
+ if not isinstance(ingredients, list) or len(ingredients) == 0:
376
+ raise ValueError("No ingredients detected. Please try again with a clearer photo.")
377
 
378
  #2: Always create/update user profile with current UI values
379
  profile = build_user_profile(
 
401
  topk=5,
402
  )
403
 
404
+ if not ml_top.empty:
405
+ for score_col in ["rank_score", "pred", "match_score", "ml_score"]:
406
+ if score_col in ml_top.columns and ml_top[score_col].notna().any():
407
+ max_val = ml_top[score_col].max()
408
+ if max_val > 0:
409
+ ml_top["score"] = ml_top[score_col] / max_val * 100
410
+ break
411
+
412
  ingredient_summary = summarize_ingredients(user_parents, high_conf, low_conf)
413
+ protein_min_val = profile["nutritional_goals"]["protein"]["min"]
414
+ protein_max_val = profile["nutritional_goals"]["protein"]["max"]
415
+ calorie_min_val = profile["nutritional_goals"]["calories"]["min"]
416
+ calorie_max_val = profile["nutritional_goals"]["calories"]["max"]
417
+
418
+ goals_summary = (
419
+ f"### Nutritional Goals\n"
420
+ f"- Calories: {calorie_min_val} - {calorie_max_val} kcal\n"
421
+ f"- Protein: {protein_min_val} - {protein_max_val} g"
422
+ )
423
+
424
+ ingredient_summary = goals_summary + "\n\n" + ingredient_summary
425
  recommendation_md, feedback_rows = render_recommendations(ml_top, user_parents)
426
 
427
 
 
445
  profile_status,
446
  )
447
  except Exception as exc:
 
 
448
  return (
449
  None,
450
+ None,
451
+ f"⚠️ {exc}",
452
  "",
 
 
453
  gr.update(choices=[], value=None),
454
  [],
455
+ f"⚠️ {exc}",
456
  )
457
 
458
 
 
608
  value=10,
609
  step=5,
610
  label="Protein Min (g)",
 
611
  )
612
  protein_max_slider = gr.Slider(
613
  minimum=0,
 
615
  value=160,
616
  step=5,
617
  label="Protein Max (g)",
 
618
  )
619
  preferred_box = gr.Textbox(
620
  label="Preferred main ingredients",