Liori25 commited on
Commit
ecabad9
·
verified ·
1 Parent(s): a3e9b9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -102
app.py CHANGED
@@ -114,10 +114,11 @@ def format_recipe(json_data):
114
  def ui_update_pipeline(image_path):
115
  # SAFEGUARD: Check if image exists
116
  if image_path is None:
117
- return "⚠️ Please upload an image first.", "", gr.update(visible=False), gr.update(visible=False), "", gr.update(visible=False), ""
 
118
 
119
  if not hf_token:
120
- return "Error: HF_TOKEN missing", "", gr.update(), gr.update(), "", gr.update(), ""
121
  try:
122
  os.environ["HF_TOKEN"] = hf_token
123
  digitizer = RecipeDigitalizerPipeline()
@@ -128,10 +129,15 @@ def ui_update_pipeline(image_path):
128
  else:
129
  sim_list = ["No query generated.", "", ""]
130
 
131
- # Returning updates for GROUPS, not Rows
132
- return (readable, sim_list[0], gr.update(visible=True), gr.update(visible=True), sim_list[1], gr.update(visible=True), sim_list[2])
 
 
 
 
 
133
  except Exception as e:
134
- return f"Error: {e}", "Error", gr.update(), gr.update(), "", gr.update(), ""
135
 
136
  # ==========================================
137
  # 4. UI THEME & CSS
@@ -419,32 +425,33 @@ with gr.Blocks(title="CookBook") as demo:
419
  gr.Markdown("### 🔍 Similar Recipes")
420
 
421
  with gr.Row():
422
- # Recipe 1 - Wrapped in Group to be valid output
423
- with gr.Column(scale=1, min_width=200):
424
- with gr.Group(elem_classes=["fb-card"], visible=False) as c1_group:
 
425
  sim1 = gr.HTML("Waiting for results...")
426
  with gr.Row(elem_classes=["post-actions"]):
427
  gr.Button("Save", elem_classes=["action-btn"], size="sm")
428
 
429
  # Recipe 2
430
- with gr.Column(scale=1, min_width=200):
431
- with gr.Group(elem_classes=["fb-card"], visible=False) as c2_group:
432
  sim2 = gr.HTML("Waiting for results...")
433
  with gr.Row(elem_classes=["post-actions"]):
434
  gr.Button("Save", elem_classes=["action-btn"], size="sm")
435
 
436
  # Recipe 3
437
- with gr.Column(scale=1, min_width=200):
438
- with gr.Group(elem_classes=["fb-card"], visible=False) as c3_group:
439
  sim3 = gr.HTML("Waiting for results...")
440
  with gr.Row(elem_classes=["post-actions"]):
441
  gr.Button("Save", elem_classes=["action-btn"], size="sm")
442
 
443
- # Update the Click logic to output to GROUPS, not rows
444
  magic_btn.click(
445
  ui_update_pipeline,
446
  input_img,
447
- [out_text, sim1, c1_group, sim2, c2_group, sim3, c3_group]
448
  )
449
 
450
  # --- VIEW 2: FEED ---
@@ -483,91 +490,4 @@ with gr.Blocks(title="CookBook") as demo:
483
  with gr.Row(elem_classes=["post-actions"]):
484
  gr.Button("👍 Like", elem_classes=["action-btn"], size="sm")
485
  gr.Button("💬 Comment", elem_classes=["action-btn"], size="sm")
486
- gr.Button("↪ Share", elem_classes=["action-btn"], size="sm")
487
-
488
- gr.Button("Load More...", variant="secondary")
489
- else:
490
- gr.Markdown("⚠️ Database is empty.")
491
-
492
- # --- VIEW 3: SAVED ---
493
- with gr.Column(visible=False) as saved_view:
494
- gr.Markdown("### 🔖 Saved Items")
495
- if not df_recipes.empty:
496
- saved_batch = df_recipes.head(20)
497
- for index, row in saved_batch.iterrows():
498
- with gr.Group(elem_classes=["fb-card"]):
499
- with gr.Row():
500
- gr.HTML("<div style='width:80px; height:80px; background:#eee; border-radius:8px;'></div>")
501
- with gr.Column():
502
- gr.Markdown(f"**{row['Title']}**")
503
- gr.HTML("<span style='color:#65676B; font-size:12px;'>Saved from News Feed</span>")
504
- gr.Button("View Collection", size="sm", variant="secondary")
505
-
506
- # --- VIEW 4: ABOUT ---
507
- with gr.Group(visible=False) as about_view:
508
- with gr.Group(elem_classes=["fb-card"]):
509
- gr.Markdown("# About CookBook")
510
- gr.Markdown("Developed by **Shahar Firshtman** and **Lior Feinstein**.")
511
- gr.HTML(f"""
512
- <div style="margin-top: 20px;">
513
- <img src="data:image/jpeg;base64,{process_b64}" style="width: 100%; border-radius: 8px;">
514
- </div>
515
- """)
516
-
517
- # === RIGHT COLUMN (Contacts) ===
518
- with gr.Column(elem_classes=["col-right"]):
519
- gr.Markdown("### Sponsored")
520
- with gr.Group(elem_classes=["fb-card"]):
521
- gr.Markdown("**Culinary School**\nJoin 10,000+ students learning to cook today!")
522
- gr.HTML(f"""
523
- <img src="data:image/webp;base64,{sponsored_b64}" style="width:100%; height:auto; border-radius:8px; display:block; margin-top:10px;">
524
- """)
525
-
526
- gr.HTML("<hr style='border:0; border-top:1px solid #ced0d4; margin: 10px 0;'>")
527
-
528
- gr.Markdown("### Contacts")
529
- contacts = ["Elon Musk", "Gordon Ramsay", "Jamie Oliver", "Martha Stewart"]
530
- for contact in contacts:
531
- gr.HTML(f"""
532
- <div class="contact-row">
533
- <div style="position:relative;">
534
- <img src="https://ui-avatars.com/api/?name={contact}&background=random" style="width:36px; height:36px; border-radius:50%;">
535
- <div style="position:absolute; bottom:0; right:0; width:10px; height:10px; background:#31a24c; border-radius:50%; border:2px solid white;"></div>
536
- </div>
537
- <div class="contact-name">{contact}</div>
538
- </div>
539
- """)
540
-
541
- # ==========================================
542
- # 6. NAVIGATION LOGIC
543
- # ==========================================
544
- def go_digi():
545
- return (
546
- gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
547
- gr.update(elem_classes=["nav-btn", "selected"]), gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn"])
548
- )
549
- def go_feed():
550
- return (
551
- gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
552
- gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn", "selected"]), gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn"])
553
- )
554
- def go_saved():
555
- return (
556
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),
557
- gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn", "selected"]), gr.update(elem_classes=["nav-btn"])
558
- )
559
- def go_about():
560
- return (
561
- gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True),
562
- gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn"]), gr.update(elem_classes=["nav-btn", "selected"])
563
- )
564
-
565
- outputs_ui = [digitalizer_view, feed_view, saved_view, about_view, nav_digital, nav_feed, nav_saved, nav_about]
566
-
567
- nav_digital.click(go_digi, None, outputs_ui)
568
- nav_feed.click(go_feed, None, outputs_ui)
569
- nav_saved.click(go_saved, None, outputs_ui)
570
- nav_about.click(go_about, None, outputs_ui)
571
-
572
- if __name__ == "__main__":
573
- demo.launch(theme=theme, css=facebook_css)
 
114
  def ui_update_pipeline(image_path):
115
  # SAFEGUARD: Check if image exists
116
  if image_path is None:
117
+ # Return hidden updates if no image
118
+ return "⚠️ Please upload an image first.", "", gr.update(visible=False), "", gr.update(visible=False), "", gr.update(visible=False)
119
 
120
  if not hf_token:
121
+ return "Error: HF_TOKEN missing", "", gr.update(), "", gr.update(), "", gr.update()
122
  try:
123
  os.environ["HF_TOKEN"] = hf_token
124
  digitizer = RecipeDigitalizerPipeline()
 
129
  else:
130
  sim_list = ["No query generated.", "", ""]
131
 
132
+ # Returning updates for COLUMNS (c1_col, c2_col, c3_col) which are valid output components
133
+ return (
134
+ readable,
135
+ sim_list[0], gr.update(visible=True),
136
+ sim_list[1], gr.update(visible=True),
137
+ sim_list[2], gr.update(visible=True)
138
+ )
139
  except Exception as e:
140
+ return f"Error: {e}", "Error", gr.update(), "Error", gr.update(), "Error", gr.update()
141
 
142
  # ==========================================
143
  # 4. UI THEME & CSS
 
425
  gr.Markdown("### 🔍 Similar Recipes")
426
 
427
  with gr.Row():
428
+
429
+ # Recipe 1 - Wrapped in COLUMN to be valid output
430
+ with gr.Column(scale=1, min_width=200, visible=False) as c1_col:
431
+ with gr.Group(elem_classes=["fb-card"]):
432
  sim1 = gr.HTML("Waiting for results...")
433
  with gr.Row(elem_classes=["post-actions"]):
434
  gr.Button("Save", elem_classes=["action-btn"], size="sm")
435
 
436
  # Recipe 2
437
+ with gr.Column(scale=1, min_width=200, visible=False) as c2_col:
438
+ with gr.Group(elem_classes=["fb-card"]):
439
  sim2 = gr.HTML("Waiting for results...")
440
  with gr.Row(elem_classes=["post-actions"]):
441
  gr.Button("Save", elem_classes=["action-btn"], size="sm")
442
 
443
  # Recipe 3
444
+ with gr.Column(scale=1, min_width=200, visible=False) as c3_col:
445
+ with gr.Group(elem_classes=["fb-card"]):
446
  sim3 = gr.HTML("Waiting for results...")
447
  with gr.Row(elem_classes=["post-actions"]):
448
  gr.Button("Save", elem_classes=["action-btn"], size="sm")
449
 
450
+ # Updated Click Logic using COLUMNS as valid outputs
451
  magic_btn.click(
452
  ui_update_pipeline,
453
  input_img,
454
+ [out_text, sim1, c1_col, sim2, c2_col, sim3, c3_col]
455
  )
456
 
457
  # --- VIEW 2: FEED ---
 
490
  with gr.Row(elem_classes=["post-actions"]):
491
  gr.Button("👍 Like", elem_classes=["action-btn"], size="sm")
492
  gr.Button("💬 Comment", elem_classes=["action-btn"], size="sm")
493
+ gr