First-Project / app.py
Rammohan0504's picture
Update app.py
37bd1d3 verified
raw
history blame
8.5 kB
import gradio as gr
from cart import Cart # Import Cart from cart.py
# Menu data
menu_data = [
{"name": "Samosa", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Potato-Samosa.jpg", "description": "A delicious potato samosa with plant-based potato, lettuce.", "price": 8.99},
{"name": "Chicken Biryani", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Chicken-Biryani.jpg", "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.", "price": 12.99},
{"name": "Paneer Butter Masala", "category": "VEGETARIAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Paneer-Butter-Masala.jpg", "description": "Soft paneer cubes simmered in a creamy tomato-based gravy, enriched with butter and spices.", "price": 10.99},
{"name": "Chilli Chicken", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/chilli-chicken-recipe.jpg", "description": "A spicy Indo-Chinese dish featuring fried chicken pieces tossed with peppers and chilies.", "price": 13.99},
{"name": "Veg Manchurian", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Veg_manchurian.jpg", "description": "Deep-fried vegetable balls tossed in a tangy and spicy Indo-Chinese sauce.", "price": 9.99},
{"name": "Fish Curry", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/fish_curry.jpg", "description": "A flavorful curry made with tender fish pieces simmered in a spiced coconut gravy.", "price": 14.99},
{"name": "Mutton Biryani", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Mutton_Biryani.jpg", "description": "Fragrant rice layered with tender mutton and aromatic spices, cooked to perfection.", "price": 15.99},
{"name": "Paneer Biryani", "category": "VEGETARIAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Paneer_Biryani.jpg", "description": "A delightful biryani made with marinated paneer cubes and basmati rice, infused with spices.", "price": 11.99},
{"name": "Onion Pakoda", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/onion-pakoda.jpg", "description": "Crispy deep-fried onion fritters, seasoned with spices, a popular tea-time snack.", "price": 5.99},
{"name": "Chilli Gobi", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/chilly_gobi.jpg", "description": "Crispy cauliflower florets tossed in a spicy and tangy sauce, an Indo-Chinese favorite.", "price": 9.99},
{"name": "Potato Samosa", "category": "VEGETARIAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Potato-Samosa.jpg", "description": "Crispy pastry filled with a spiced potato mixture, deep-fried to golden perfection.", "price": 4.99},
{"name": "Prawn Fry", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Pawn_Fry.jpg", "description": "Spicy and crispy fried prawns, marinated with a blend of spices and herbs.", "price": 14.99},
{"name": "Sukka Gosht (Goat)", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Sukka_Gosht%20(Goat).jpg", "description": "Dry goat meat preparation cooked with a medley of spices, offering a rich and hearty flavor.", "price": 16.99},
{"name": "Channa Masala", "category": "VEGETARIAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/channa_masala.jpg", "description": "A hearty curry made with chickpeas simmered in a spiced tomato gravy.", "price": 8.99},
{"name": "Vegetable Biryani", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Veg-Biryani-Recipe.jpg", "description": "A medley of vegetables and basmati rice cooked with aromatic spices, a vegetarian delight.", "price": 10.99},
{"name": "Chicken Manchurian", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Chicken_manchurian.jpg", "description": "A popular Indo-Chinese dish featuring fried chicken pieces tossed in a tangy and spicy sauce.", "price": 13.99},
]
# Initialize the cart
cart = Cart()
# Filter dishes by category
def filter_dishes(category):
if category == "ALL":
return menu_data
return [dish for dish in menu_data if dish["category"] == category]
# Add item to the cart
def add_to_cart(item_name):
item = next((dish for dish in menu_data if dish["name"] == item_name), None)
if item:
message = cart.add_item(item)
return message, cart.view_cart(), f"Total: ${cart.total_price():.2f}"
return "Item not found!", cart.view_cart(), f"Total: ${cart.total_price():.2f}"
# Generate pop-up content for a selected dish
def generate_popup(dish_name):
item = next((dish for dish in menu_data if dish["name"] == dish_name), None)
if item:
popup_html = f"""
<div style="text-align: center;">
<img src="{item['image']}" alt="{item['name']}" style="width: 300px; height: 200px; object-fit: cover; border-radius: 10px;">
<h2 style="color: #444;">{item['name']}</h2>
<p style="font-size: 16px; color: #666;">{item['description']}</p>
<p style="font-size: 18px; color: #444;">Price: ${item['price']}</p>
</div>
"""
return popup_html
return "Dish not found!"
# Main Gradio App
with gr.Blocks() as demo:
gr.HTML("<h1 style='text-align: center;'>🍛 Biryani Hub Menu 🍛</h1>")
# Category buttons
with gr.Row():
btn_all = gr.Button("ALL")
btn_vegan = gr.Button("VEGAN")
btn_halal = gr.Button("HALAL")
# Dish display and cart
dish_display = gr.HTML()
popup_display = gr.HTML()
cart_display = gr.HTML("Cart is empty.")
cart_total = gr.Textbox(value="Total: $0.00", interactive=False)
# Event to update dishes based on category
def display_dishes(category):
filtered_dishes = filter_dishes(category)
html_content = "<div style='display: flex; flex-wrap: wrap; gap: 20px; justify-content: center;'>"
for dish in filtered_dishes:
html_content += f"""
<div style="width: 200px; border: 1px solid #ddd; border-radius: 10px; padding: 10px; text-align: center; background-color: #f9f9f9;">
<img src="{dish['image']}" alt="{dish['name']}" style="width: 100%; height: 150px; object-fit: cover; border-radius: 10px;">
<h4 style="margin: 10px 0; color: #444;">{dish['name']}</h4>
<button onclick="showPopup('{dish['name']}')" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">View</button>
</div>
"""
html_content += "</div>"
return html_content
# JavaScript for managing the pop-up
gr.HTML("""
<script>
function showPopup(dishName) {
fetch("/generate_popup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: dishName })
})
.then(response => response.json())
.then(data => {
document.getElementById("popup").innerHTML = data.html;
document.getElementById("popup").style.display = "block";
});
}
function closePopup() {
document.getElementById("popup").style.display = "none";
}
</script>
""")
# Dish display logic
btn_all.click(lambda: display_dishes("ALL"), outputs=dish_display)
btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
# Cart display
gr.Button("View Cart").click(lambda: cart.view_cart(), outputs=cart_display)
# Placeholder for pop-up
gr.HTML(id="popup", value="", visible=False)
demo.launch()