Spaces:
Running
Running
import gradio as gr | |
import random | |
import uuid | |
# Sample 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} | |
] | |
# Sample customer data for login (in a real system, use a database) | |
customers = { | |
"user1": {"name": "John Doe", "phone": "1234567890", "customer_id": "CUST001"}, | |
"user2": {"name": "Jane Smith", "phone": "9876543210", "customer_id": "CUST002"} | |
} | |
# Initialize session data | |
session_data = {} | |
def login(phone): | |
for customer_id, details in customers.items(): | |
if details['phone'] == phone: | |
session_data['customer_id'] = details['customer_id'] | |
session_data['customer_name'] = details['name'] | |
return f"Welcome {details['name']}!" | |
return "Customer not found. Please register." | |
def display_menu(): | |
return "\n".join([f"{item['id']}. {item['name']} - ${item['price']}" for item in menu_items]) | |
def add_to_cart(item_id, quantity): | |
cart = session_data.get('cart', []) | |
cart.append({"item_id": item_id, "quantity": quantity}) | |
session_data['cart'] = cart | |
return f"Item added to cart. Cart has {len(cart)} item(s)." | |
def view_cart(): | |
cart = session_data.get('cart', []) | |
cart_items = [] | |
total_price = 0 | |
for item in cart: | |
item_details = next((menu_item for menu_item in menu_items if menu_item['id'] == item['item_id']), None) | |
if item_details: | |
cart_items.append(f"{item_details['name']} x {item['quantity']} - ${item_details['price'] * item['quantity']}") | |
total_price += item_details['price'] * item['quantity'] | |
return "\n".join(cart_items) + f"\nTotal Price: ${total_price}" | |
def proceed_to_order(table_number): | |
order_id = str(uuid.uuid4()) # Generate unique order ID | |
cart = session_data.get('cart', []) | |
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 ID: {order_id}\nTable Number: {table_number}\nOrder Details: {order_details}" | |
# Gradio Interface | |
login_interface = gr.Interface(fn=login, inputs="text", outputs="text", title="Customer Login") | |
menu_interface = gr.Interface(fn=display_menu, inputs=None, outputs="text", title="Menu") | |
cart_interface = gr.Interface(fn=view_cart, inputs=None, outputs="text", title="Cart") | |
order_interface = gr.Interface(fn=proceed_to_order, inputs="text", outputs="text", title="Proceed to Order") | |
# Launch Gradio app | |
demo = gr.TabbedInterface([login_interface, menu_interface, cart_interface, order_interface], ["Login", "Menu", "Cart", "Order"]) | |
demo.launch() | |