Rammohan0504 commited on
Commit
db3a44d
Β·
verified Β·
1 Parent(s): edf766e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -124
app.py CHANGED
@@ -1,129 +1,127 @@
1
  import gradio as gr
2
 
3
- # Define available dishes and their portion sizes with image paths and categories
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
- "category": "All",
11
- },
12
- "Vegan Salad": {
13
- "main": "images/vegan_salad_main.jpg",
14
- "small": "images/vegan_salad_small.jpg",
15
- "medium": "images/vegan_salad_medium.jpg",
16
- "large": "images/vegan_salad_large.jpg",
17
- "category": "Vegan",
18
- },
19
- "Halal Chicken": {
20
- "main": "images/halal_chicken_main.jpg",
21
- "small": "images/halal_chicken_small.jpg",
22
- "medium": "images/halal_chicken_medium.jpg",
23
- "large": "images/halal_chicken_large.jpg",
24
- "category": "Halal",
25
- },
26
- "Allergenic Soup": {
27
- "main": "images/allergenic_soup_main.jpg",
28
- "small": "images/allergenic_soup_small.jpg",
29
- "medium": "images/allergenic_soup_medium.jpg",
30
- "large": "images/allergenic_soup_large.jpg",
31
- "category": "Allergenic",
32
- },
33
- }
34
-
35
  def filter_dishes(category):
36
- return {dish: data for dish, data in dish_data.items() if category == "All" or data["category"] == category}
37
-
38
- def display_dish(dish):
39
- if dish not in dish_data:
40
- return None, dish, gr.update(visible=False), gr.update(visible=False)
41
- main_image = dish_data[dish]["main"]
42
- return main_image, dish, gr.update(visible=True), gr.update(visible=True)
43
-
44
- def display_portion(dish, portion):
45
- if dish not in dish_data:
46
- return None
47
- portion_image = dish_data[dish].get(portion, None)
48
- return portion_image
49
-
50
- def spice_level_selection(spice_level):
51
- return f"Selected spice level: {spice_level}"
52
-
53
- # Define the Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  with gr.Blocks() as demo:
55
- gr.Markdown("# Food Ordering System")
56
-
57
- # Category selection
58
- category_dropdown = gr.Dropdown(
59
- choices=["All", "Vegan", "Halal", "Allergenic"],
60
- value="All",
61
- label="Select Category"
62
- )
63
-
64
- # Dynamic dish buttons
65
- dish_buttons = gr.Row()
66
-
67
- def update_dish_buttons(category):
68
- filtered_dishes = filter_dishes(category)
69
- buttons = []
70
- for dish, data in filtered_dishes.items():
71
- buttons.append(gr.Button(value=dish))
72
- return buttons
73
-
74
- category_dropdown.change(
75
- fn=update_dish_buttons,
76
- inputs=category_dropdown,
77
- outputs=dish_buttons
78
- )
79
-
80
- # Section to display the main image of the selected dish
81
- selected_dish = gr.State() # Store the selected dish
82
- main_image = gr.Image(label="Dish Image")
83
- dish_name_display = gr.Text(label="Dish Name")
84
- portion_row = gr.Row(visible=False) # Portion size buttons section
85
- portion_image = gr.Image(label="Portion Size Image")
86
- spice_row = gr.Row(visible=False) # Spice level buttons section
87
-
88
- # Portion size buttons
89
- with portion_row:
90
- portion_buttons = []
91
- for portion in ["small", "medium", "large"]:
92
- portion_buttons.append(gr.Button(value=portion.capitalize()))
93
-
94
- # Spice level buttons
95
- with spice_row:
96
- spice_buttons = []
97
- for spice in ["Mild", "Medium", "Spicy"]:
98
- spice_buttons.append(gr.Button(value=spice))
99
-
100
- # Link dish buttons to display the selected dish
101
- def create_dish_click_handler(dish):
102
- return lambda: display_dish(dish)
103
-
104
- for dish in dish_data.keys():
105
- button = gr.Button(value=dish, visible=False)
106
- button.click(
107
- fn=create_dish_click_handler(dish),
108
- inputs=[],
109
- outputs=[main_image, dish_name_display, portion_row, spice_row],
110
- )
111
-
112
- # Link portion buttons to display the portion image
113
- for portion_button, portion in zip(portion_buttons, ["small", "medium", "large"]):
114
- portion_button.click(
115
- fn=display_portion,
116
- inputs=[selected_dish, gr.Textbox(value=portion, visible=False)],
117
- outputs=portion_image,
118
- )
119
-
120
- # Link spice level buttons to display the spice level
121
- for spice_button, spice in zip(spice_buttons, ["Mild", "Medium", "Spicy"]):
122
- spice_button.click(
123
- fn=spice_level_selection,
124
- inputs=[gr.Textbox(value=spice, visible=False)],
125
- outputs=dish_name_display
126
- )
127
-
128
- # Launch the Gradio app
129
  demo.launch()
 
1
  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"}
25
+ ]
26
+
27
+ spice_options = ["🌢 Mild", "🌢🌢 Medium", "🌢🌢🌢 High"]
28
+ portion_options = ["πŸ§† Small", "🍽 Medium", "πŸ› Large"]
29
+
30
+ # Function to filter dishes based on category
 
 
 
 
31
  def filter_dishes(category):
32
+ if category == "ALL":
33
+ return menu_data
34
+ return [dish for dish in menu_data if dish["category"] == category]
35
+
36
+ # Function to display dishes with images
37
+ def display_dishes(category):
38
+ filtered_dishes = filter_dishes(category)
39
+ html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
40
+ for dish in filtered_dishes:
41
+ html_content += f"""
42
+ <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
43
+ padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
44
+ onclick='selectDish(\"{dish['name']}\")'>
45
+ <img src='{dish['image']}' alt='{dish['name']}'
46
+ style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
47
+ <h4 style='margin-top: 10px; font-size: 16px; color: #444;'>{dish['name']}</h4>
48
+ </div>
49
+ """
50
+ html_content += "</div>"
51
+ return html_content
52
+
53
+ # Function to fetch details of the selected dish
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"], "🌢 Medium", "🍽 Medium"
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"βœ… Order Confirmed: '{dish_name}' with {spice_level} spice level and {portion_size} portion size."
63
+
64
+ # Main Gradio App
65
  with gr.Blocks() as demo:
66
+ gr.HTML("<h1 style='text-align: center; color: #333;'>πŸ› 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
+ # Dish Details Section
80
+ gr.Markdown("### Dish Details")
81
+ with gr.Row():
82
+ dish_image = gr.Image(label="Dish Image", height=200, width=200)
83
+ with gr.Column():
84
+ dish_name = gr.Textbox(label="Dish Name", interactive=False)
85
+ dish_description = gr.Textbox(label="Description", interactive=False)
86
+ spice_dropdown = gr.Radio(label="Select Spice Level", choices=spice_options, value="🌢 Medium")
87
+ portion_dropdown = gr.Radio(label="Select Portion Size", choices=portion_options, value="🍽 Medium")
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: Update dish display for selected category
94
+ def update_dishes(category):
95
+ return gr.update(value=display_dishes(category))
96
+
97
+ btn_all.click(fn=lambda: update_dishes("ALL"), outputs=dish_display)
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 when a dish is selected
102
+ def show_dish_details(dish_name):
103
+ image, name, description, spice, portion = get_dish_details(dish_name)
104
+ return image, name, description, spice, portion
105
+
106
+ # Hidden input to receive dish name from JavaScript
107
+ selected_dish_name = gr.Textbox(visible=False)
108
+ selected_dish_name.change(fn=show_dish_details, inputs=selected_dish_name,
109
+ outputs=[dish_image, dish_name, dish_description, spice_dropdown, portion_dropdown])
110
+
111
+ # Event: Place order button
112
+ place_order_btn.click(fn=place_order,
113
+ inputs=[dish_name, spice_dropdown, portion_dropdown],
114
+ outputs=order_status)
115
+
116
+ # Inject JavaScript to handle clicks and pass dish names
117
+ gr.HTML("""
118
+ <script>
119
+ function selectDish(dishName) {
120
+ let input = document.querySelector('input[aria-label="selected_dish_name"]');
121
+ input.value = dishName;
122
+ input.dispatchEvent(new Event('input', { bubbles: true }));
123
+ }
124
+ </script>
125
+ """)
126
+
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  demo.launch()