Rammohan0504 commited on
Commit
3986c73
·
verified ·
1 Parent(s): 670a97d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -45
app.py CHANGED
@@ -2,23 +2,23 @@ import gradio as gr
2
 
3
  # Extended menu data with dish details
4
  menu_data = [
5
- {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
7
  "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.",
8
  "spice_level": "Medium", "portion_size": "Regular"},
9
- {"name": "Chicken Biryani", "category": "HALAL",
10
  "image": "https://upload.wikimedia.org/wikipedia/commons/7/79/Chicken_Biryani.jpg",
11
  "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.",
12
  "spice_level": "High", "portion_size": "Large"},
13
- {"name": "Paneer Butter Masala", "category": "VEGAN",
14
  "image": "https://upload.wikimedia.org/wikipedia/commons/3/3d/Paneer_Butter_Masala.jpg",
15
  "description": "Paneer cooked in a rich and creamy tomato-based gravy.",
16
  "spice_level": "Mild", "portion_size": "Medium"},
17
- {"name": "Beef Steak", "category": "HALAL",
18
  "image": "https://upload.wikimedia.org/wikipedia/commons/d/d5/Beef_Steak.jpg",
19
  "description": "Juicy beef steak served with mashed potatoes and grilled vegetables.",
20
  "spice_level": "Medium", "portion_size": "Large"},
21
- {"name": "Mushroom Soup", "category": "VEGAN",
22
  "image": "https://upload.wikimedia.org/wikipedia/commons/3/3c/Mushroom_Soup.jpg",
23
  "description": "Creamy mushroom soup with fresh herbs.",
24
  "spice_level": "Mild", "portion_size": "Small"}
@@ -54,10 +54,10 @@ def display_dishes(category):
54
  def get_dish_details(dish_name):
55
  for dish in menu_data:
56
  if dish["name"] == dish_name:
57
- return (dish["image"], dish["name"], dish["description"], dish["spice_level"], dish["portion_size"])
58
- return ("https://via.placeholder.com/300x200", "Unknown Dish", "No description available.", "N/A", "N/A")
59
 
60
- # Function to simulate placing an order with customizations
61
  def place_order(dish_name, spice_level, portion_size):
62
  return f"Your order for '{dish_name}' with {spice_level} spice level and {portion_size} portion size has been placed successfully!"
63
 
@@ -65,32 +65,27 @@ def place_order(dish_name, spice_level, portion_size):
65
  with gr.Blocks() as demo:
66
  gr.HTML("<h1 style='text-align: center;'>Biryani Hub Menu</h1>")
67
 
68
- # Buttons for category selection
69
- gr.Markdown("### Select a Category")
70
  with gr.Row():
71
  btn_all = gr.Button("ALL")
72
  btn_vegan = gr.Button("VEGAN")
73
  btn_halal = gr.Button("HALAL")
74
 
75
- # Dish display section
76
- gr.Markdown("### Available Dishes")
77
  dish_display = gr.HTML(value=display_dishes("ALL"))
78
 
79
- # Detailed Dish View
80
- gr.Markdown("### Dish Details")
81
- dish_image = gr.Image(label="Dish Image", interactive=False)
82
- dish_name = gr.Textbox(label="Dish Name", interactive=False)
83
- dish_description = gr.Textbox(label="Description", interactive=False)
84
-
85
- # Dropdowns for Spice Level and Portion Size
86
- spice_dropdown = gr.Dropdown(label="Select Spice Level", choices=spice_options)
87
- portion_dropdown = gr.Dropdown(label="Select Portion Size", choices=portion_options)
88
-
89
- # Place Order Button
90
- place_order_btn = gr.Button("Place Order")
91
- order_status = gr.Textbox(label="Order Status", interactive=False)
92
-
93
- # Event: Display dishes for selected category
94
  def update_dishes(category):
95
  return gr.update(value=display_dishes(category))
96
 
@@ -98,32 +93,33 @@ with gr.Blocks() as demo:
98
  btn_vegan.click(fn=lambda: update_dishes("VEGAN"), outputs=dish_display)
99
  btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=dish_display)
100
 
101
- # Event: Show dish details
102
  def show_dish_details(dish_name):
103
  details = get_dish_details(dish_name)
104
- return details + (details[3], details[4]) # Return spice level and portion size
105
 
106
- # Dummy HTML input for JS trigger
107
  selected_dish_name = gr.Textbox(visible=False)
108
-
109
- # Bind the selected dish name to the details display and prefill dropdowns
110
- selected_dish_name.change(fn=show_dish_details, inputs=selected_dish_name,
111
- outputs=[dish_image, dish_name, dish_description, spice_dropdown, portion_dropdown])
112
-
113
- # Event: Place Order Button
114
- place_order_btn.click(fn=place_order,
115
- inputs=[dish_name, spice_dropdown, portion_dropdown],
116
- outputs=order_status)
117
-
118
- # Inject JavaScript to handle clicks and pass dish names
 
 
119
  gr.HTML("""
120
  <script>
121
  function selectDish(dishName) {
122
- document.querySelector('input[aria-label="selected_dish_name"]').value = dishName;
123
- document.querySelector('input[aria-label="selected_dish_name"]').dispatchEvent(new Event('input'));
 
124
  }
125
  </script>
126
  """)
127
 
128
- # Launch the app
129
- demo.launch()
 
2
 
3
  # Extended menu data with dish details
4
  menu_data = [
5
+ {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
7
  "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.",
8
  "spice_level": "Medium", "portion_size": "Regular"},
9
+ {"name": "Chicken Biryani", "category": "HALAL",
10
  "image": "https://upload.wikimedia.org/wikipedia/commons/7/79/Chicken_Biryani.jpg",
11
  "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.",
12
  "spice_level": "High", "portion_size": "Large"},
13
+ {"name": "Paneer Butter Masala", "category": "VEGAN",
14
  "image": "https://upload.wikimedia.org/wikipedia/commons/3/3d/Paneer_Butter_Masala.jpg",
15
  "description": "Paneer cooked in a rich and creamy tomato-based gravy.",
16
  "spice_level": "Mild", "portion_size": "Medium"},
17
+ {"name": "Beef Steak", "category": "HALAL",
18
  "image": "https://upload.wikimedia.org/wikipedia/commons/d/d5/Beef_Steak.jpg",
19
  "description": "Juicy beef steak served with mashed potatoes and grilled vegetables.",
20
  "spice_level": "Medium", "portion_size": "Large"},
21
+ {"name": "Mushroom Soup", "category": "VEGAN",
22
  "image": "https://upload.wikimedia.org/wikipedia/commons/3/3c/Mushroom_Soup.jpg",
23
  "description": "Creamy mushroom soup with fresh herbs.",
24
  "spice_level": "Mild", "portion_size": "Small"}
 
54
  def get_dish_details(dish_name):
55
  for dish in menu_data:
56
  if dish["name"] == dish_name:
57
+ return dish["image"], dish["name"], dish["description"], dish["spice_level"], dish["portion_size"]
58
+ return "https://via.placeholder.com/300x200", "Unknown Dish", "No description available.", "N/A", "N/A"
59
 
60
+ # Function to simulate placing an order
61
  def place_order(dish_name, spice_level, portion_size):
62
  return f"Your order for '{dish_name}' with {spice_level} spice level and {portion_size} portion size has been placed successfully!"
63
 
 
65
  with gr.Blocks() as demo:
66
  gr.HTML("<h1 style='text-align: center;'>Biryani Hub Menu</h1>")
67
 
68
+ # Category Buttons
 
69
  with gr.Row():
70
  btn_all = gr.Button("ALL")
71
  btn_vegan = gr.Button("VEGAN")
72
  btn_halal = gr.Button("HALAL")
73
 
74
+ # Dish Display Section
 
75
  dish_display = gr.HTML(value=display_dishes("ALL"))
76
 
77
+ # Dish Details Section
78
+ with gr.Row(visible=False) as dish_details:
79
+ dish_image = gr.Image(label="Dish Image", height=200, width=200)
80
+ with gr.Column():
81
+ dish_name = gr.Textbox(label="Dish Name", interactive=False)
82
+ dish_description = gr.Textbox(label="Description", interactive=False)
83
+ spice_dropdown = gr.Dropdown(label="Select Spice Level", choices=spice_options)
84
+ portion_dropdown = gr.Dropdown(label="Select Portion Size", choices=portion_options)
85
+ place_order_btn = gr.Button("Place Order")
86
+ order_status = gr.Textbox(label="Order Status", interactive=False)
87
+
88
+ # Event to update dishes based on category
 
 
 
89
  def update_dishes(category):
90
  return gr.update(value=display_dishes(category))
91
 
 
93
  btn_vegan.click(fn=lambda: update_dishes("VEGAN"), outputs=dish_display)
94
  btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=dish_display)
95
 
96
+ # Event to show dish details
97
  def show_dish_details(dish_name):
98
  details = get_dish_details(dish_name)
99
+ return gr.update(visible=True), details[0], details[1], details[2], details[3], details[4]
100
 
 
101
  selected_dish_name = gr.Textbox(visible=False)
102
+ selected_dish_name.change(
103
+ fn=show_dish_details,
104
+ inputs=selected_dish_name,
105
+ outputs=[dish_details, dish_image, dish_name, dish_description, spice_dropdown, portion_dropdown]
106
+ )
107
+
108
+ place_order_btn.click(
109
+ fn=place_order,
110
+ inputs=[dish_name, spice_dropdown, portion_dropdown],
111
+ outputs=order_status
112
+ )
113
+
114
+ # Inject JavaScript for interaction
115
  gr.HTML("""
116
  <script>
117
  function selectDish(dishName) {
118
+ let input = document.querySelector('input[aria-label="selected_dish_name"]');
119
+ input.value = dishName;
120
+ input.dispatchEvent(new Event('input', { bubbles: true }));
121
  }
122
  </script>
123
  """)
124
 
125
+ demo.launch()