benjac8 commited on
Commit
cbf6858
·
verified ·
1 Parent(s): 83f4713

Fix Gradio state schema so all UI actions work

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -628,7 +628,7 @@ def read_watch_screenshot(image):
628
  if image is None:
629
  return (
630
  notice_html("Upload a WHOOP or watch screenshot first.", "error"),
631
- gr.update(), gr.update(), gr.update(), gr.update(), {},
632
  )
633
  if pytesseract is None:
634
  return (
@@ -636,7 +636,7 @@ def read_watch_screenshot(image):
636
  "Screenshot reading is temporarily unavailable. You can still enter the values manually.",
637
  "error",
638
  ),
639
- gr.update(), gr.update(), gr.update(), gr.update(), {},
640
  )
641
  try:
642
  source_image = image if isinstance(image, Image.Image) else Image.fromarray(image)
@@ -684,7 +684,7 @@ def read_watch_screenshot(image):
684
  gr.update(value=parsed["sleep"] if parsed["sleep"] is not None else 7),
685
  gr.update(value=parsed["strain"] if parsed["strain"] is not None else 10),
686
  gr.update(value=parsed["hrv"] if parsed["hrv"] is not None else 45),
687
- source_info,
688
  )
689
  except Exception as exc: # OCR failure must never block manual entry.
690
  print(f"OCR FAILED [{type(exc).__name__}]: {exc}")
@@ -693,7 +693,7 @@ def read_watch_screenshot(image):
693
  "I could not read that screenshot. Try a tighter crop or use manual entry.",
694
  "error",
695
  ),
696
- gr.update(), gr.update(), gr.update(), gr.update(), {},
697
  )
698
 
699
 
@@ -717,7 +717,7 @@ def reset_form():
717
  return (
718
  None, "", [], 7, 10, 45, [], "", "Any", 30, 2,
719
  "No preference", [], [], "No preference", "Any equipment", "", "",
720
- "", "", "", "", {},
721
  )
722
 
723
 
@@ -878,8 +878,12 @@ def run(state, feelings, metric_selection, main_ingredient, include_ingredients,
878
  "safe deterministic recipe is shown instead.")
879
 
880
  img = fetch_dish_image(obj["Recipe_Name"])
881
- source_label = html.escape(str((wearable_source or {}).get("source", "Manual entry")))
882
- timestamp = html.escape(str((wearable_source or {}).get("timestamp", "This session")))
 
 
 
 
883
  row_ids = ", ".join(html.escape(str(value)) for value in top3["row_id"].tolist())
884
  generation_mode = "AI-generated recipe" if not note else "validated deterministic fallback"
885
  technical = f"""
@@ -918,17 +922,17 @@ QUICK_STARTERS = {
918
  "strength": (
919
  "Heavy CrossFit workout today", ["Sore muscles", "Low energy"],
920
  ["Sleep", "Strain", "HRV"], "Steak / beef", ["Potatoes"], [],
921
- "Keep it simple", "omnivore", 25, 4, 18, 32, {},
922
  ),
923
  "endurance": (
924
  "I ran a half marathon this morning", ["Low energy", "Dehydrated"],
925
  ["Sleep", "Strain", "HRV"], "Eggs", ["Rice", "Sweet potato"], [],
926
- "Carb-heavy", "vegetarian", 40, 7, 19, 41, {},
927
  ),
928
  "stress": (
929
  "A very stressful week at work", ["Stressed", "Low energy"],
930
  ["Sleep", "Strain", "HRV"], "Tofu", ["Spinach"], ["Caffeine"],
931
- "One-pan meal", "Any", 30, 5, 8, 29, {},
932
  ),
933
  }
934
 
@@ -984,7 +988,10 @@ function() {
984
  """
985
 
986
  with gr.Blocks(title="Bio-Bite", theme=THEME, css=CSS, js=FORCE_DARK) as demo:
987
- wearable_source = gr.State({})
 
 
 
988
  gr.HTML(
989
  f"""
990
  <div id="bb-hero">
 
628
  if image is None:
629
  return (
630
  notice_html("Upload a WHOOP or watch screenshot first.", "error"),
631
+ gr.update(), gr.update(), gr.update(), gr.update(), "{}",
632
  )
633
  if pytesseract is None:
634
  return (
 
636
  "Screenshot reading is temporarily unavailable. You can still enter the values manually.",
637
  "error",
638
  ),
639
+ gr.update(), gr.update(), gr.update(), gr.update(), "{}",
640
  )
641
  try:
642
  source_image = image if isinstance(image, Image.Image) else Image.fromarray(image)
 
684
  gr.update(value=parsed["sleep"] if parsed["sleep"] is not None else 7),
685
  gr.update(value=parsed["strain"] if parsed["strain"] is not None else 10),
686
  gr.update(value=parsed["hrv"] if parsed["hrv"] is not None else 45),
687
+ json.dumps(source_info),
688
  )
689
  except Exception as exc: # OCR failure must never block manual entry.
690
  print(f"OCR FAILED [{type(exc).__name__}]: {exc}")
 
693
  "I could not read that screenshot. Try a tighter crop or use manual entry.",
694
  "error",
695
  ),
696
+ gr.update(), gr.update(), gr.update(), gr.update(), "{}",
697
  )
698
 
699
 
 
717
  return (
718
  None, "", [], 7, 10, 45, [], "", "Any", 30, 2,
719
  "No preference", [], [], "No preference", "Any equipment", "", "",
720
+ "", "", "", "", "{}",
721
  )
722
 
723
 
 
878
  "safe deterministic recipe is shown instead.")
879
 
880
  img = fetch_dish_image(obj["Recipe_Name"])
881
+ try:
882
+ source_info = json.loads(wearable_source or "{}")
883
+ except (TypeError, json.JSONDecodeError):
884
+ source_info = {}
885
+ source_label = html.escape(str(source_info.get("source", "Manual entry")))
886
+ timestamp = html.escape(str(source_info.get("timestamp", "This session")))
887
  row_ids = ", ".join(html.escape(str(value)) for value in top3["row_id"].tolist())
888
  generation_mode = "AI-generated recipe" if not note else "validated deterministic fallback"
889
  technical = f"""
 
922
  "strength": (
923
  "Heavy CrossFit workout today", ["Sore muscles", "Low energy"],
924
  ["Sleep", "Strain", "HRV"], "Steak / beef", ["Potatoes"], [],
925
+ "Keep it simple", "omnivore", 25, 4, 18, 32, "{}",
926
  ),
927
  "endurance": (
928
  "I ran a half marathon this morning", ["Low energy", "Dehydrated"],
929
  ["Sleep", "Strain", "HRV"], "Eggs", ["Rice", "Sweet potato"], [],
930
+ "Carb-heavy", "vegetarian", 40, 7, 19, 41, "{}",
931
  ),
932
  "stress": (
933
  "A very stressful week at work", ["Stressed", "Low energy"],
934
  ["Sleep", "Strain", "HRV"], "Tofu", ["Spinach"], ["Caffeine"],
935
+ "One-pan meal", "Any", 30, 5, 8, 29, "{}",
936
  ),
937
  }
938
 
 
988
  """
989
 
990
  with gr.Blocks(title="Bio-Bite", theme=THEME, css=CSS, js=FORCE_DARK) as demo:
991
+ # Keep State JSON-serializable as a string. Gradio 5.9's API-schema builder
992
+ # crashes on an unconstrained dict (additionalProperties: true), which makes
993
+ # every UI event appear as "No API found" even though the app has started.
994
+ wearable_source = gr.State("{}")
995
  gr.HTML(
996
  f"""
997
  <div id="bb-hero">