Rammohan0504 commited on
Commit
4a94f34
Β·
verified Β·
1 Parent(s): 3d6c5bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -79
app.py CHANGED
@@ -22,27 +22,16 @@ menu_data = [
22
  spice_options = ["🌢 Mild", "🌢🌢 Medium", "🌢🌢🌢 High"]
23
  portion_options = ["πŸ§† Small", "🍽 Medium", "πŸ› Large"]
24
 
25
- # Function to display dishes dynamically based on category
26
- def display_dishes(category):
27
- filtered_dishes = menu_data if category == "ALL" else [dish for dish in menu_data if dish["category"] == category]
28
- html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
29
- for dish in filtered_dishes:
30
- html_content += f"""
31
- <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
32
- padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
33
- onclick='selectDish("{dish['name']}")'>
34
- <img src='{dish['image']}' alt='{dish['name']}'
35
- style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
36
- <h4 style='margin-top: 10px; font-size: 16px; color: #444;'>{dish['name']}</h4>
37
- </div>
38
- """
39
- html_content += "</div>"
40
- return html_content
41
 
42
  # Function to fetch dish details
43
  def get_dish_details(dish_name):
44
  for dish in menu_data:
45
- if dish["name"] == dish_name.strip():
46
  return dish["image"], dish["name"], dish["description"]
47
  return "https://via.placeholder.com/300x200", "Unknown Dish", "No description available."
48
 
@@ -53,65 +42,49 @@ def place_order(dish_name, spice_level, portion_size):
53
  return "⚠️ Please select a dish before placing an order."
54
 
55
  # Main Gradio App
56
- with gr.Blocks() as demo:
57
- gr.HTML("<h1 style='text-align: center;'>πŸ› Biryani Hub Menu πŸ›</h1>")
58
-
59
- # Buttons for category selection
60
- gr.Markdown("### Select a Category")
61
- with gr.Row():
62
- btn_all = gr.Button("ALL")
63
- btn_vegan = gr.Button("VEGAN")
64
- btn_halal = gr.Button("HALAL")
65
-
66
- # Dish display section
67
- dish_display = gr.HTML(value=display_dishes("ALL"))
68
-
69
- # Dish Details Section
70
- gr.Markdown("### Dish Details")
71
- with gr.Row():
72
- dish_image = gr.Image(label="Dish Image", height=200, width=200)
73
- with gr.Column():
74
- dish_name = gr.Textbox(label="Dish Name", interactive=False)
75
- dish_description = gr.Textbox(label="Description", interactive=False)
76
- spice_dropdown = gr.Radio(label="Select Spice Level", choices=spice_options, value="🌢 Medium")
77
- portion_dropdown = gr.Radio(label="Select Portion Size", choices=portion_options, value="🍽 Medium")
78
- place_order_btn = gr.Button("Place Order")
79
- order_status = gr.Textbox(label="Order Status", interactive=False)
80
-
81
- # Event: Update Dish Display for Selected Category
82
- def update_category(category):
83
- return display_dishes(category)
84
-
85
- btn_all.click(fn=lambda: update_category("ALL"), outputs=dish_display)
86
- btn_vegan.click(fn=lambda: update_category("VEGAN"), outputs=dish_display)
87
- btn_halal.click(fn=lambda: update_category("HALAL"), outputs=dish_display)
88
-
89
- # Hidden textbox to pass dish name dynamically
90
- selected_dish_name = gr.Textbox(visible=False)
91
-
92
- # Event: Show Dish Details
93
- def update_dish_details(selected_dish):
94
- image, name, description = get_dish_details(selected_dish)
95
- return image, name, description
96
-
97
- selected_dish_name.change(fn=update_dish_details,
98
- inputs=selected_dish_name,
99
- outputs=[dish_image, dish_name, dish_description])
100
-
101
- # Place Order
102
- place_order_btn.click(fn=place_order,
103
- inputs=[dish_name, spice_dropdown, portion_dropdown],
104
- outputs=order_status)
105
-
106
- # Inject JavaScript to pass dish name dynamically
107
- gr.HTML("""
108
- <script>
109
- function selectDish(dishName) {
110
- const input = document.querySelector('input[aria-label="selected_dish_name"]');
111
- input.value = dishName;
112
- input.dispatchEvent(new Event('input', { bubbles: true }));
113
- }
114
- </script>
115
- """)
116
-
117
- demo.launch()
 
22
  spice_options = ["🌢 Mild", "🌢🌢 Medium", "🌢🌢🌢 High"]
23
  portion_options = ["πŸ§† Small", "🍽 Medium", "πŸ› Large"]
24
 
25
+ # Function to filter dishes by category
26
+ def get_dish_list(category):
27
+ if category == "ALL":
28
+ return [dish["name"] for dish in menu_data]
29
+ return [dish["name"] for dish in menu_data if dish["category"] == category]
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Function to fetch dish details
32
  def get_dish_details(dish_name):
33
  for dish in menu_data:
34
+ if dish["name"] == dish_name:
35
  return dish["image"], dish["name"], dish["description"]
36
  return "https://via.placeholder.com/300x200", "Unknown Dish", "No description available."
37
 
 
42
  return "⚠️ Please select a dish before placing an order."
43
 
44
  # Main Gradio App
45
+ def gradio_app():
46
+ with gr.Blocks() as demo:
47
+ gr.HTML("<h1 style='text-align: center;'>πŸ› Biryani Hub Menu πŸ›</h1>")
48
+
49
+ # Category Selection
50
+ gr.Markdown("### Select a Category")
51
+ category_dropdown = gr.Radio(choices=["ALL", "VEGAN", "HALAL"], label="Categories", value="ALL")
52
+
53
+ # Dish Selection
54
+ gr.Markdown("### Select a Dish")
55
+ dish_selector = gr.Radio(label="Available Dishes", choices=get_dish_list("ALL"))
56
+
57
+ # Dish Details Section
58
+ gr.Markdown("### Dish Details")
59
+ with gr.Row():
60
+ dish_image = gr.Image(label="Dish Image", height=200, width=200)
61
+ with gr.Column():
62
+ dish_name = gr.Textbox(label="Dish Name", interactive=False)
63
+ dish_description = gr.Textbox(label="Description", interactive=False)
64
+ spice_dropdown = gr.Radio(label="Select Spice Level", choices=spice_options, value="🌢 Medium")
65
+ portion_dropdown = gr.Radio(label="Select Portion Size", choices=portion_options, value="🍽 Medium")
66
+ place_order_btn = gr.Button("Place Order")
67
+ order_status = gr.Textbox(label="Order Status", interactive=False)
68
+
69
+ # Update dish list dynamically
70
+ def update_dish_list(category):
71
+ return gr.update(choices=get_dish_list(category))
72
+
73
+ category_dropdown.change(fn=update_dish_list, inputs=category_dropdown, outputs=dish_selector)
74
+
75
+ # Update dish details dynamically
76
+ def update_dish_details(dish_name_input):
77
+ return get_dish_details(dish_name_input)
78
+
79
+ dish_selector.change(fn=update_dish_details, inputs=dish_selector,
80
+ outputs=[dish_image, dish_name, dish_description])
81
+
82
+ # Place order functionality
83
+ place_order_btn.click(fn=place_order,
84
+ inputs=[dish_name, spice_dropdown, portion_dropdown],
85
+ outputs=order_status)
86
+
87
+ return demo
88
+
89
+ # Launch the app
90
+ gradio_app().launch()