Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,109 +50,66 @@ spice_options = ["πΆ Mild", "πΆπΆ Medium", "πΆπΆπΆ High"]
|
|
| 50 |
portion_options = ["π§ Small", "π½ Medium", "π Large"]
|
| 51 |
|
| 52 |
# Function to filter dishes based on category
|
| 53 |
-
def filter_dishes(category):
|
| 54 |
-
if category == "ALL":
|
| 55 |
-
return menu_data
|
| 56 |
-
return [dish for dish in menu_data if dish["category"] == category]
|
| 57 |
-
|
| 58 |
-
# Function to display dishes with images
|
| 59 |
-
def display_dishes(category):
|
| 60 |
-
filtered_dishes = filter_dishes(category)
|
| 61 |
-
html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
|
| 62 |
-
for dish in filtered_dishes:
|
| 63 |
-
html_content += f"""
|
| 64 |
-
<div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
|
| 65 |
-
padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
|
| 66 |
-
onclick='selectDish(\"{dish['name']}\")'>
|
| 67 |
-
<img src='{dish['image']}' alt='{dish['name']}'
|
| 68 |
-
style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
|
| 69 |
-
<h4 style='margin-top: 10px; font-size: 16px; color: #444;'>{dish['name']}</h4>
|
| 70 |
-
</div>
|
| 71 |
-
"""
|
| 72 |
-
html_content += "</div>"
|
| 73 |
-
return html_content
|
| 74 |
-
|
| 75 |
-
# Function to fetch details of the selected dish
|
| 76 |
def get_dish_details(dish_name):
|
| 77 |
if dish_name in dish_data:
|
| 78 |
main_image = dish_data[dish_name]["main"]
|
| 79 |
-
return main_image, dish_name, "Select
|
| 80 |
-
return
|
| 81 |
|
| 82 |
-
# Function to display
|
| 83 |
def display_portion(dish_name, portion):
|
| 84 |
if dish_name in dish_data:
|
| 85 |
-
return dish_data[dish_name].get(portion, None)
|
| 86 |
return None
|
| 87 |
|
| 88 |
# Function to simulate placing an order
|
| 89 |
def place_order(dish_name, spice_level, portion_size):
|
| 90 |
return f"β
Order Confirmed: '{dish_name}' with {spice_level} spice level and {portion_size} portion size."
|
| 91 |
|
| 92 |
-
#
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
-
gr.
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
gr.Markdown("### Select a Category")
|
| 98 |
with gr.Row():
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
with gr.Column():
|
|
|
|
|
|
|
| 112 |
spice_dropdown = gr.Radio(label="Select Spice Level", choices=spice_options, value="πΆ Medium")
|
| 113 |
-
portion_dropdown = gr.Radio(label="Select Portion Size", choices=
|
| 114 |
portion_image = gr.Image(label="Portion Size Image")
|
| 115 |
|
| 116 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
place_order_btn = gr.Button("Place Order")
|
| 118 |
order_status = gr.Textbox(label="Order Status", interactive=False)
|
| 119 |
|
| 120 |
-
# Event:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=dish_display)
|
| 127 |
-
|
| 128 |
-
# Event: Show dish details in the modal when a dish is selected
|
| 129 |
-
def show_dish_details(dish_name):
|
| 130 |
-
image, name, description = get_dish_details(dish_name)
|
| 131 |
-
dish_modal.show()
|
| 132 |
-
return image, name, description
|
| 133 |
-
|
| 134 |
-
# Hidden input to receive dish name from JavaScript
|
| 135 |
-
selected_dish_name = gr.Textbox(visible=False)
|
| 136 |
-
selected_dish_name.change(fn=show_dish_details, inputs=selected_dish_name,
|
| 137 |
-
outputs=[dish_image, dish_name, dish_modal])
|
| 138 |
-
|
| 139 |
-
# Event: Display portion size image
|
| 140 |
-
portion_dropdown.change(fn=display_portion, inputs=[selected_dish_name, portion_dropdown], outputs=portion_image)
|
| 141 |
-
|
| 142 |
-
# Event: Place order button
|
| 143 |
-
place_order_btn.click(fn=place_order,
|
| 144 |
-
inputs=[dish_name, spice_dropdown, portion_dropdown],
|
| 145 |
-
outputs=order_status)
|
| 146 |
-
|
| 147 |
-
# Inject JavaScript to handle clicks and pass dish names
|
| 148 |
-
gr.HTML("""
|
| 149 |
-
<script>
|
| 150 |
-
function selectDish(dishName) {
|
| 151 |
-
let input = document.querySelector('input[aria-label="selected_dish_name"]');
|
| 152 |
-
input.value = dishName;
|
| 153 |
-
input.dispatchEvent(new Event('input', { bubbles: true }));
|
| 154 |
-
}
|
| 155 |
-
</script>
|
| 156 |
-
""")
|
| 157 |
|
| 158 |
-
demo.launch()
|
|
|
|
| 50 |
portion_options = ["π§ Small", "π½ Medium", "π Large"]
|
| 51 |
|
| 52 |
# Function to filter dishes based on category
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def get_dish_details(dish_name):
|
| 54 |
if dish_name in dish_data:
|
| 55 |
main_image = dish_data[dish_name]["main"]
|
| 56 |
+
return main_image, dish_name, "Select portion size and spice level below:", gr.update(visible=True)
|
| 57 |
+
return None, "Unknown Dish", "No description available.", gr.update(visible=False)
|
| 58 |
|
| 59 |
+
# Function to display portion size image
|
| 60 |
def display_portion(dish_name, portion):
|
| 61 |
if dish_name in dish_data:
|
| 62 |
+
return dish_data[dish_name].get(portion.lower(), None)
|
| 63 |
return None
|
| 64 |
|
| 65 |
# Function to simulate placing an order
|
| 66 |
def place_order(dish_name, spice_level, portion_size):
|
| 67 |
return f"β
Order Confirmed: '{dish_name}' with {spice_level} spice level and {portion_size} portion size."
|
| 68 |
|
| 69 |
+
# Gradio App
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("# Food Ordering System")
|
| 72 |
|
| 73 |
+
# Dish selection buttons
|
|
|
|
| 74 |
with gr.Row():
|
| 75 |
+
for dish in dish_data.keys():
|
| 76 |
+
gr.Button(dish).click(
|
| 77 |
+
fn=get_dish_details,
|
| 78 |
+
inputs=[gr.Textbox(value=dish, visible=False)],
|
| 79 |
+
outputs=[
|
| 80 |
+
gr.Image(label="Dish Image"),
|
| 81 |
+
gr.Text(label="Dish Name"),
|
| 82 |
+
gr.Text(label="Dish Description"),
|
| 83 |
+
gr.Row(visible=False),
|
| 84 |
+
],
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Dish details section (simulated modal)
|
| 88 |
+
with gr.Row(visible=False) as details_section:
|
| 89 |
+
dish_image = gr.Image(label="Dish Image")
|
| 90 |
with gr.Column():
|
| 91 |
+
dish_name = gr.Textbox(label="Dish Name", interactive=False)
|
| 92 |
+
dish_description = gr.Textbox(label="Dish Description", interactive=False)
|
| 93 |
spice_dropdown = gr.Radio(label="Select Spice Level", choices=spice_options, value="πΆ Medium")
|
| 94 |
+
portion_dropdown = gr.Radio(label="Select Portion Size", choices=portion_options, value="Medium")
|
| 95 |
portion_image = gr.Image(label="Portion Size Image")
|
| 96 |
|
| 97 |
+
# Event: Update portion size image
|
| 98 |
+
portion_dropdown.change(
|
| 99 |
+
fn=display_portion,
|
| 100 |
+
inputs=[dish_name, portion_dropdown],
|
| 101 |
+
outputs=portion_image,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Place Order button
|
| 105 |
place_order_btn = gr.Button("Place Order")
|
| 106 |
order_status = gr.Textbox(label="Order Status", interactive=False)
|
| 107 |
|
| 108 |
+
# Event: Place order
|
| 109 |
+
place_order_btn.click(
|
| 110 |
+
fn=place_order,
|
| 111 |
+
inputs=[dish_name, spice_dropdown, portion_dropdown],
|
| 112 |
+
outputs=order_status,
|
| 113 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
demo.launch()
|