Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234267891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181193456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import gradio as grimport pandas as pdimport qrcodeimport os
# Data storage (for prototyping; switch to Salesforce or database for production)customer_data_path = "customer_data.xlsx"order_data_path = "order_data.xlsx"
# Ensure data files existif not os.path.exists(customer_data_path): pd.DataFrame(columns=["Name", "Email", "Phone", "Preferences", "Allergies"]).to_excel(customer_data_path, index=False)
if not os.path.exists(order_data_path): pd.DataFrame(columns=["Table", "Customer", "Order", "Status"]).to_excel(order_data_path, index=False)
# Sample menu (can be dynamically loaded from an external file)menu = { "Bread": ["Roti", "Naan", "Paratha"], "Veg Curries": ["Paneer Butter Masala", "Mix Veg", "Dal Tadka"], "Non-Veg Curries": ["Chicken Curry", "Mutton Korma", "Fish Curry"], "Biryanis": ["Veg Biryani", "Chicken Biryani", "Mutton Biryani"]}
# Generate QR Code for each tabledef generate_qr(table_number): qr = qrcode.make(f"https://your-huggingface-space/{table_number}") qr_path = f"table_{table_number}_qr.png" qr.save(qr_path) return qr_path
# Sign-Up functionalitydef sign_up(name, email, phone, preferences, allergies): customer_data = pd.read_excel(customer_data_path) if email in customer_data["Email"].values: return "Email already exists! Please log in." new_entry = {"Name": name, "Email": email, "Phone": phone, "Preferences": preferences, "Allergies": allergies} customer_data = customer_data.append(new_entry, ignore_index=True) customer_data.to_excel(customer_data_path, index=False) return "Sign-Up successful! Please log in."
# Login functionalitydef login(email): customer_data = pd.read_excel(customer_data_path) if email in customer_data["Email"].values: customer = customer_data[customer_data["Email"] == email].iloc[0] return f"Welcome back, {customer['Name']}!", customer.to_dict() return "Email not found! Please sign up.", None
# Display menu based on preferencesdef display_menu(preferences, allergies): filtered_menu = {} for category, items in menu.items(): filtered_menu[category] = [item for item in items if item not in allergies] return filtered_menu
# Add item to orderdef add_to_order(table, customer, item): order_data = pd.read_excel(order_data_path) new_order = {"Table": table, "Customer": customer, "Order": item, "Status": "Pending"} order_data = order_data.append(new_order, ignore_index=True) order_data.to_excel(order_data_path, index=False) return "Item added to order."
# Gradio Interfacewith gr.Blocks() as app: gr.Markdown("# QR-Based Restaurant Ordering System")
# QR Code Section with gr.Tab("Generate QR Code"): table_input = gr.Number(label="Table Number", value=1) qr_output = gr.Image(label="QR Code") generate_button = gr.Button("Generate QR Code")
generate_button.click(generate_qr, inputs=table_input, outputs=qr_output)
# Sign-Up and Login Section with gr.Tab("Sign Up / Login"): with gr.Row(): with gr.Column(): gr.Markdown("### Sign Up") name_input = gr.Textbox(label="Name") email_input = gr.Textbox(label="Email") phone_input = gr.Textbox(label="Phone") preferences_input = gr.Textbox(label="Preferences (e.g., Vegetarian)") allergies_input = gr.Textbox(label="Allergies (e.g., Peanuts)") sign_up_button = gr.Button("Sign Up") sign_up_output = gr.Textbox(label="Sign-Up Status")
sign_up_button.click(sign_up, inputs=[name_input, email_input, phone_input, preferences_input, allergies_input], outputs=sign_up_output)
with gr.Column(): gr.Markdown("### Login") login_email_input = gr.Textbox(label="Email") login_button = gr.Button("Login") login_output = gr.Textbox(label="Login Status")
login_button.click(login, inputs=login_email_input, outputs=login_output)
# Menu and Ordering Section with gr.Tab("Menu & Order"): preferences_input = gr.Textbox(label="Preferences (from Login)", interactive=False) allergies_input = gr.Textbox(label="Allergies (from Login)", interactive=False) menu_display = gr.JSON(label="Filtered Menu") display_menu_button = gr.Button("Show Menu")
display_menu_button.click(display_menu, inputs=[preferences_input, allergies_input], outputs=menu_display)
item_input = gr.Textbox(label="Item to Add") table_input = gr.Number(label="Table Number", value=1) customer_input = gr.Textbox(label="Customer Name") add_order_button = gr.Button("Add to Order") add_order_output = gr.Textbox(label="Order Status")
add_order_button.click(add_to_order, inputs=[table_input, customer_input, item_input], outputs=add_order_output)
app.launch()# Menu datamenu_data = [ {"name": "Veg Burger", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg", "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.", "price": "$8.99"}, {"name": "Chicken Biryani", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_biryani.jpg", "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.", "price": "$12.99"}, {"name": "Paneer Butter Masala", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/paneer_butter_masala.jpg", "description": "Paneer cooked in a rich and creamy tomato-based gravy.", "price": "$10.99"}]
# Spice levels and extrasspice_levels = ["American Mild", "American Medium", "American Spicy", "Indian Mild", "Indian Medium", "Indian Spicy", "Indian Very Spicy"]extras = [ {"name": "Extra Raitha 4oz", "price": "$1.00"}, {"name": "Extra Onion", "price": "$1.00"}, {"name": "Extra Fried Onion 4oz", "price": "$2.00"}]
# Filter dishes by categorydef filter_dishes(category): if category == "ALL": return menu_data return [dish for dish in menu_data if dish["category"] == category]
# Generate dish cards with "Add" buttondef display_dishes(category): filtered_dishes = filter_dishes(category) html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%; height:100%'>" for dish in filtered_dishes: html_content += f""" <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between; width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9;'> <div style='flex: 1; margin-right: 20px; text-align: left;'> <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4> <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p> <p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p> </div> <div style='text-align: center;'> <img src='{dish['image']}' alt='{dish['name']}' style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'> <button onclick=\"showPopup('{dish['image']}', '{dish['name']}', '{dish['description']}', '{dish['price']}')\" style=\"margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;\">Add</button> </div> </div> """ html_content += "</div>" return html_content
# Main Gradio Appwith gr.Blocks() as demo: gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Pop-up 🍛</h1>")
# Buttons for category selection with gr.Row(): btn_all = gr.Button("ALL") btn_vegan = gr.Button("VEGAN") btn_halal = gr.Button("HALAL")
# Dish display dish_display = gr.HTML(value=display_dishes("ALL"))
# JavaScript for managing the pop-up gr.HTML(f""" <div id=\"popup\" style=\"display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;\"> <img id=\"popup-image\" src=\"\" alt=\"\" style=\"width: 100%; height: auto; border-radius: 10px;\"> <h2 id=\"popup-title\" style=\"margin-top: 20px; color: #444;\"></h2> <p id=\"popup-description\" style=\"margin-top: 10px; font-size: 16px; color: #666;\"></p> <p id=\"popup-price\" style=\"margin-top: 10px; font-size: 16px; color: #444;\"></p> <h4 style=\"text-align: left; margin-top: 20px;\">Choose a spice level</h4> <div style=\"display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;\"> """ + """".join([f'<label style=\"margin-right: 10px; display: block;\"><input type=\"radio\" name=\"spice\" value=\"{spice}\" style=\"margin-right: 5px;\">{spice}</label>' for spice in spice_levels]) + """ </div>
<h4 style=\"text-align: left; margin-top: 20px;\">Choose Extras</h4> <div style=\"display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;\"> """ + """".join([f'<label style=\"margin-right: 10px; display: block;\"><input type=\"checkbox\" name=\"extras\" value=\"{extra["name"]}\" style=\"margin-right: 5px;\">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """ </div>
<button onclick=\"addToCart(document.getElementById('popup-title').textContent, document.querySelector('input[name=spice]:checked').value)\" style=\"margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;\">Add to Cart</button> <button onclick=\"closePopup()\" style=\"margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;\">Close</button> </div> <script> function showPopup(image, title, description, price) { document.getElementById('popup-image').src = image; document.getElementById('popup-title').textContent = title; document.getElementById('popup-description').textContent = description; document.getElementById('popup-price').textContent = "Price: " + price;
// Display the popup document.getElementById('popup').style.display = 'block'; } function closePopup() { document.getElementById('popup').style.display = 'none'; } function addToCart(dishName, spiceLevel) { const selectedExtras = Array.from(document.querySelectorAll('input[name=extras]:checked')).map(cb => cb.value).join(", "); alert(dishName + " with " + spiceLevel + " spice level and extras: " + selectedExtras + " has been added to the cart!"); } </script> """)
# Button click events btn_all.click( lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display ) btn_vegan.click( lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display ) btn_halal.click( lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display )
demo.launch()app.py+131-117