Rammohan0504 commited on
Commit
963f7ff
·
verified ·
1 Parent(s): 27ddbc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -37
app.py CHANGED
@@ -3,10 +3,10 @@ import gradio as gr
3
  # Define available dishes and their portion sizes with image paths
4
  dish_data = {
5
  "Pizza": {
6
- "main": "pizza_main.jpg",
7
- "small": "pizza_small.jpg",
8
- "medium": "pizza_medium.jpg",
9
- "large": "pizza_large.jpg",
10
  },
11
  "Burger": {
12
  "main": "images/burger_main.jpg",
@@ -22,47 +22,67 @@ dish_data = {
22
  },
23
  }
24
 
25
- # Function to display images dynamically
26
- def display_images(dish, portion):
27
  if dish not in dish_data:
28
- return None, None # Handle invalid dish selection
29
- main_image = dish_data[dish]["main"]
30
  portion_image = dish_data[dish].get(portion, None)
31
- return main_image, portion_image
 
 
 
 
 
 
32
 
33
  # Define the Gradio interface
34
  with gr.Blocks() as demo:
35
- gr.Markdown("# Food Ordering System with Portion Sizes")
36
-
37
- # Dish selection dropdown
38
  with gr.Row():
39
- dish_dropdown = gr.Dropdown(
40
- choices=list(dish_data.keys()),
41
- label="Select Dish",
42
- value="Pizza"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  )
44
- portion_dropdown = gr.Dropdown(
45
- choices=["small", "medium", "large"],
46
- label="Select Portion Size",
47
- value="small"
 
 
 
 
 
 
 
 
 
 
48
  )
49
-
50
- # Image display section
51
- with gr.Row():
52
- main_image = gr.Image(label="Main Image")
53
- portion_image = gr.Image(label="Portion Size Image")
54
-
55
- # Link dropdowns to the image display function
56
- dish_dropdown.change(
57
- fn=display_images,
58
- inputs=[dish_dropdown, portion_dropdown],
59
- outputs=[main_image, portion_image]
60
- )
61
- portion_dropdown.change(
62
- fn=display_images,
63
- inputs=[dish_dropdown, portion_dropdown],
64
- outputs=[main_image, portion_image]
65
- )
66
 
67
  # Launch the Gradio app
68
  demo.launch()
 
3
  # Define available dishes and their portion sizes with image paths
4
  dish_data = {
5
  "Pizza": {
6
+ "main": "images/pizza_main.jpg",
7
+ "small": "images/pizza_small.jpg",
8
+ "medium": "images/pizza_medium.jpg",
9
+ "large": "images/pizza_large.jpg",
10
  },
11
  "Burger": {
12
  "main": "images/burger_main.jpg",
 
22
  },
23
  }
24
 
25
+ def display_portion(dish, portion):
 
26
  if dish not in dish_data:
27
+ return None # Handle invalid dish selection
 
28
  portion_image = dish_data[dish].get(portion, None)
29
+ return portion_image
30
+
31
+ def display_dish(dish):
32
+ if dish not in dish_data:
33
+ return None, None
34
+ main_image = dish_data[dish]["main"]
35
+ return main_image, dish
36
 
37
  # Define the Gradio interface
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("# Food Ordering System")
40
+
41
+ # Section to display dishes as clickable images
42
  with gr.Row():
43
+ dish_buttons = []
44
+ for dish, data in dish_data.items():
45
+ with gr.Column():
46
+ button = gr.Button(value=dish, elem_id=f"button_{dish}")
47
+ dish_buttons.append((button, dish))
48
+
49
+ # Section to display the main image of the selected dish
50
+ selected_dish = gr.State() # Store the selected dish name
51
+ main_image = gr.Image(label="Dish Image")
52
+ dish_name_display = gr.Text(label="Dish Name")
53
+
54
+ # Section to display portion size buttons and corresponding image
55
+ with gr.Row(visible=False) as portion_row:
56
+ gr.Markdown("## Select Portion Size")
57
+ portion_buttons = []
58
+ for portion in ["small", "medium", "large"]:
59
+ portion_button = gr.Button(value=portion.capitalize())
60
+ portion_buttons.append(portion_button)
61
+
62
+ portion_image = gr.Image(label="Portion Size Image")
63
+
64
+ # Link dish buttons to display the selected dish
65
+ for button, dish in dish_buttons:
66
+ button.click(
67
+ fn=display_dish,
68
+ inputs=[gr.Textbox(value=dish, visible=False)],
69
+ outputs=[main_image, dish_name_display]
70
  )
71
+ button.click(
72
+ fn=lambda x: x,
73
+ inputs=[gr.Textbox(value=dish, visible=False)],
74
+ outputs=[selected_dish],
75
+ show_progress=False
76
+ )
77
+ button.click(None, [], [], _js=f"() => document.querySelector('[visible=false]').style.display = 'flex';")
78
+
79
+ # Link portion buttons to display the portion image
80
+ for portion_button, portion in zip(portion_buttons, ["small", "medium", "large"]):
81
+ portion_button.click(
82
+ fn=display_portion,
83
+ inputs=[selected_dish, gr.Textbox(value=portion, visible=False)],
84
+ outputs=portion_image
85
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  # Launch the Gradio app
88
  demo.launch()