Spaces:
Sleeping
Sleeping
import gradio as gr | |
import uuid | |
# Sample data for menu items (You can replace these with your actual menu items) | |
menu_items = [ | |
{"id": 1, "name": "Burger", "price": 5.99}, | |
{"id": 2, "name": "Pizza", "price": 8.99}, | |
{"id": 3, "name": "Pasta", "price": 7.99}, | |
{"id": 4, "name": "Salad", "price": 4.99}, | |
{"id": 5, "name": "Soda", "price": 1.99} | |
] | |
# Simulated database (In real applications, use a real database) | |
customers = {} # Stores customers by phone number | |
session_data = {} | |
# Function for user registration (signup) | |
def signup(name, phone): | |
if phone in customers: | |
return f"Phone number {phone} is already registered. Please log in." | |
# Create a unique customer ID | |
customer_id = f"CUST{str(uuid.uuid4().int)[:6]}" # Shortened unique customer ID | |
customers[phone] = {"name": name, "phone": phone, "customer_id": customer_id} | |
return f"Signup successful! Welcome {name}. Your customer ID is {customer_id}." | |
# Function for user login | |
def login(phone): | |
if phone in customers: | |
customer = customers[phone] | |
session_data['customer_id'] = customer['customer_id'] | |
session_data['customer_name'] = customer['name'] | |
return f"Welcome {customer['name']}! You are now logged in." | |
else: | |
return "Phone number not found. Please register first." | |
# Display menu and allow adding items to cart | |
def menu(item_name, quantity): | |
if 'customer_id' not in session_data: | |
return "Please log in first to view the menu and add items to the cart." | |
# Find the selected item in the menu | |
selected_item = next((item for item in menu_items if item["name"] == item_name), None) | |
if not selected_item: | |
return f"Item '{item_name}' not found in the menu." | |
# Add the item to the cart | |
cart = session_data.get('cart', []) | |
cart.append({"item_id": selected_item['id'], "name": selected_item['name'], "quantity": quantity, "price": selected_item['price']}) | |
session_data['cart'] = cart | |
return f"Added {quantity} x {selected_item['name']} to the cart." | |
# View cart contents | |
def view_cart(): | |
if 'customer_id' not in session_data: | |
return "Please log in first to view your cart." | |
cart = session_data.get('cart', []) | |
if not cart: | |
return "Your cart is empty." | |
cart_summary = [] | |
total_price = 0 | |
for item in cart: | |
cart_summary.append(f"{item['name']} x {item['quantity']} - ${item['price'] * item['quantity']:.2f}") | |
total_price += item['price'] * item['quantity'] | |
return "\n".join(cart_summary) + f"\n\nTotal Price: ${total_price:.2f}" | |
# Proceed to order function | |
def proceed_to_order(table_number): | |
if 'customer_id' not in session_data: | |
return "Please log in first to proceed with your order." | |
cart = session_data.get('cart', []) | |
if not cart: | |
return "Your cart is empty. Add items to your cart before proceeding." | |
order_id = str(uuid.uuid4()) # Generate unique order ID | |
order_details = { | |
"order_id": order_id, | |
"customer_id": session_data['customer_id'], | |
"customer_name": session_data['customer_name'], | |
"table_number": table_number, | |
"items": cart | |
} | |
session_data['cart'] = [] # Clear cart after order | |
return f"Order placed successfully!\n\nOrder ID: {order_id}\nTable Number: {table_number}\n\nOrder Details:\n" + "\n".join([f"{item['name']} x {item['quantity']} - ${item['price'] * item['quantity']:.2f}" for item in cart]) | |
# Gradio Interface for Signup | |
signup_interface = gr.Interface(fn=signup, inputs=["text", "text"], outputs="text", title="Signup") | |
# Gradio Interface for Login | |
login_interface = gr.Interface(fn=login, inputs="text", outputs="text", title="Login") | |
# Gradio Interface for Menu | |
menu_interface = gr.Interface( | |
fn=menu, | |
inputs=[ | |
gr.Dropdown(choices=[item["name"] for item in menu_items], label="Select Item"), | |
gr.Number(label="Quantity", value=1, precision=0) | |
], | |
outputs="text", | |
title="Menu" | |
) | |
# Gradio Interface for Cart | |
cart_interface = gr.Interface(fn=view_cart, inputs=None, outputs="text", title="Cart") | |
# Gradio Interface for Proceed to Order | |
order_interface = gr.Interface( | |
fn=proceed_to_order, | |
inputs=gr.Textbox(label="Table Number"), | |
outputs="text", | |
title="Proceed to Order" | |
) | |
# Launch Gradio app with multiple tabs | |
demo = gr.TabbedInterface( | |
[signup_interface, login_interface, menu_interface, cart_interface, order_interface], | |
["Signup", "Login", "Menu", "Cart", "Order"] | |
) | |
demo.launch() | |