Spaces:
Running
Running
File size: 5,525 Bytes
bc774c2 e424940 bc774c2 e424940 bc774c2 e424940 bc774c2 4d8885f 1d8b98e 4d8885f 1d8b98e e424940 1d8b98e bc774c2 1d8b98e bc774c2 4d8885f a5cfc16 4d8885f a5cfc16 a02c36b a5cfc16 4d8885f bc774c2 4d8885f bc774c2 4d8885f bc774c2 e424940 bc774c2 a5cfc16 bc774c2 a02c36b bc774c2 4d8885f a02c36b bc774c2 4d8885f bc774c2 a5cfc16 bc774c2 a02c36b bc774c2 4d8885f e424940 4d8885f 93d40ac 77685e0 93d40ac e424940 93d40ac bc774c2 77685e0 a02c36b 1d8b98e bc774c2 1d8b98e a02c36b bc774c2 93d40ac 1d8b98e a02c36b bc774c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import gradio as gr
import uuid
import json
import os
# File to store registered user data
USER_DATA_FILE = "users.json"
# Load user data from the file
def load_user_data():
if os.path.exists(USER_DATA_FILE):
with open(USER_DATA_FILE, "r") as file:
return json.load(file)
return {}
# Save user data to the file
def save_user_data(data):
with open(USER_DATA_FILE, "w") as file:
json.dump(data, file, indent=4)
# Initialize user database
customers = load_user_data()
session_data = {}
# Function for user signup
def signup(name, phone):
if phone in customers:
return f"Phone number {phone} is already registered. Please log in."
# Generate unique customer ID
customer_id = f"CUST{str(uuid.uuid4().int)[:6]}"
customers[phone] = {"name": name, "phone": phone, "customer_id": customer_id}
# Save the updated user data
save_user_data(customers)
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."
# Function to add items to the cart
def add_to_cart(item_name):
if 'customer_id' not in session_data:
return "Please log in first to 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 item to cart
cart = session_data.get('cart', [])
cart.append({"item_id": selected_item['id'], "name": selected_item['name'], "quantity": 1, "price": selected_item['price']})
session_data['cart'] = cart
return f"Added 1 x {selected_item['name']} to the cart."
# Function to display the cart
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']:.2f}")
total_price += item['price']
return "\n".join(cart_summary) + f"\n\nTotal Price: ${total_price:.2f}"
# Function to proceed to order
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']:.2f}" for item in cart])
# Sample menu items
menu_items = [
{"id": 1, "name": "Burger", "price": 5.99, "description": "Juicy grilled burger with fresh veggies."},
{"id": 2, "name": "Pizza", "price": 8.99, "description": "Cheesy pizza with a choice of toppings."},
{"id": 3, "name": "Pasta", "price": 7.99, "description": "Creamy Italian pasta with garlic bread."},
{"id": 4, "name": "Salad", "price": 4.99, "description": "Fresh garden salad with vinaigrette dressing."},
{"id": 5, "name": "Soda", "price": 1.99, "description": "Refreshing chilled soda of your choice."}
]
# Function to generate the styled menu
def generate_menu():
with gr.Blocks() as menu_interface:
gr.Markdown("# Menu")
output = gr.Textbox(label="Status") # Declare output component only once
for item in menu_items:
with gr.Row():
with gr.Column(scale=4):
gr.Markdown(f"### {item['name']}\n{item['description']}\n**Price:** ${item['price']:.2f}")
with gr.Column(scale=1):
button = gr.Button("Add")
button.click(fn=add_to_cart, inputs=gr.Textbox(value=item["name"], visible=False), outputs=output)
return menu_interface
# 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 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"
)
# Generate the menu interface
menu_interface = generate_menu()
# 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()
|