DSatishchandra commited on
Commit
4d8885f
1 Parent(s): a02c36b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import uuid
3
 
4
- # Sample data for menu items (You can replace these with your actual menu items)
5
  menu_items = [
6
  {"id": 1, "name": "Burger", "price": 5.99},
7
  {"id": 2, "name": "Pizza", "price": 8.99},
@@ -10,17 +10,17 @@ menu_items = [
10
  {"id": 5, "name": "Soda", "price": 1.99}
11
  ]
12
 
13
- # Simulated database (In real applications, use a real database)
14
- customers = {} # Stores customers by phone number
15
  session_data = {}
16
 
17
- # Function for user registration (signup)
18
  def signup(name, phone):
19
  if phone in customers:
20
  return f"Phone number {phone} is already registered. Please log in."
21
 
22
- # Create a unique customer ID
23
- customer_id = f"CUST{str(uuid.uuid4().int)[:6]}" # Shortened unique customer ID
24
  customers[phone] = {"name": name, "phone": phone, "customer_id": customer_id}
25
 
26
  return f"Signup successful! Welcome {name}. Your customer ID is {customer_id}."
@@ -35,23 +35,23 @@ def login(phone):
35
  else:
36
  return "Phone number not found. Please register first."
37
 
38
- # Display menu and allow adding items to cart
39
- def menu(item_name, quantity):
40
  if 'customer_id' not in session_data:
41
- return "Please log in first to view the menu and add items to the cart."
42
 
43
  # Find the selected item in the menu
44
  selected_item = next((item for item in menu_items if item["name"] == item_name), None)
45
  if not selected_item:
46
  return f"Item '{item_name}' not found in the menu."
47
 
48
- # Add the item to the cart
49
  cart = session_data.get('cart', [])
50
- cart.append({"item_id": selected_item['id'], "name": selected_item['name'], "quantity": quantity, "price": selected_item['price']})
51
  session_data['cart'] = cart
52
- return f"Added {quantity} x {selected_item['name']} to the cart."
53
 
54
- # View cart contents
55
  def view_cart():
56
  if 'customer_id' not in session_data:
57
  return "Please log in first to view your cart."
@@ -63,11 +63,11 @@ def view_cart():
63
  cart_summary = []
64
  total_price = 0
65
  for item in cart:
66
- cart_summary.append(f"{item['name']} x {item['quantity']} - ${item['price'] * item['quantity']:.2f}")
67
- total_price += item['price'] * item['quantity']
68
  return "\n".join(cart_summary) + f"\n\nTotal Price: ${total_price:.2f}"
69
 
70
- # Proceed to order function
71
  def proceed_to_order(table_number):
72
  if 'customer_id' not in session_data:
73
  return "Please log in first to proceed with your order."
@@ -85,7 +85,15 @@ def proceed_to_order(table_number):
85
  "items": cart
86
  }
87
  session_data['cart'] = [] # Clear cart after order
88
- 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])
 
 
 
 
 
 
 
 
89
 
90
  # Gradio Interface for Signup
91
  signup_interface = gr.Interface(fn=signup, inputs=["text", "text"], outputs="text", title="Signup")
@@ -93,16 +101,15 @@ signup_interface = gr.Interface(fn=signup, inputs=["text", "text"], outputs="tex
93
  # Gradio Interface for Login
94
  login_interface = gr.Interface(fn=login, inputs="text", outputs="text", title="Login")
95
 
96
- # Gradio Interface for Menu
97
- menu_interface = gr.Interface(
98
- fn=menu,
99
- inputs=[
100
- gr.Dropdown(choices=[item["name"] for item in menu_items], label="Select Item"),
101
- gr.Number(label="Quantity", value=1, precision=0)
102
- ],
103
- outputs="text",
104
- title="Menu"
105
- )
106
 
107
  # Gradio Interface for Cart
108
  cart_interface = gr.Interface(fn=view_cart, inputs=None, outputs="text", title="Cart")
 
1
  import gradio as gr
2
  import uuid
3
 
4
+ # Sample menu items (Add your actual items here)
5
  menu_items = [
6
  {"id": 1, "name": "Burger", "price": 5.99},
7
  {"id": 2, "name": "Pizza", "price": 8.99},
 
10
  {"id": 5, "name": "Soda", "price": 1.99}
11
  ]
12
 
13
+ # Simulated database for customers and session management
14
+ customers = {}
15
  session_data = {}
16
 
17
+ # Function for user signup
18
  def signup(name, phone):
19
  if phone in customers:
20
  return f"Phone number {phone} is already registered. Please log in."
21
 
22
+ # Generate unique customer ID
23
+ customer_id = f"CUST{str(uuid.uuid4().int)[:6]}"
24
  customers[phone] = {"name": name, "phone": phone, "customer_id": customer_id}
25
 
26
  return f"Signup successful! Welcome {name}. Your customer ID is {customer_id}."
 
35
  else:
36
  return "Phone number not found. Please register first."
37
 
38
+ # Function to add items to the cart
39
+ def add_to_cart(item_name):
40
  if 'customer_id' not in session_data:
41
+ return "Please log in first to add items to the cart."
42
 
43
  # Find the selected item in the menu
44
  selected_item = next((item for item in menu_items if item["name"] == item_name), None)
45
  if not selected_item:
46
  return f"Item '{item_name}' not found in the menu."
47
 
48
+ # Add item to cart
49
  cart = session_data.get('cart', [])
50
+ cart.append({"item_id": selected_item['id'], "name": selected_item['name'], "quantity": 1, "price": selected_item['price']})
51
  session_data['cart'] = cart
52
+ return f"Added 1 x {selected_item['name']} to the cart."
53
 
54
+ # Function to display cart
55
  def view_cart():
56
  if 'customer_id' not in session_data:
57
  return "Please log in first to view your cart."
 
63
  cart_summary = []
64
  total_price = 0
65
  for item in cart:
66
+ cart_summary.append(f"{item['name']} x {item['quantity']} - ${item['price']:.2f}")
67
+ total_price += item['price']
68
  return "\n".join(cart_summary) + f"\n\nTotal Price: ${total_price:.2f}"
69
 
70
+ # Function to proceed to order
71
  def proceed_to_order(table_number):
72
  if 'customer_id' not in session_data:
73
  return "Please log in first to proceed with your order."
 
85
  "items": cart
86
  }
87
  session_data['cart'] = [] # Clear cart after order
88
+ 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])
89
+
90
+ # Dynamic menu interface with add buttons
91
+ def generate_menu():
92
+ menu_elements = []
93
+ for item in menu_items:
94
+ button = gr.Button(f"Add {item['name']} (${item['price']:.2f})")
95
+ menu_elements.append((button, lambda name=item['name']: add_to_cart(name)))
96
+ return menu_elements
97
 
98
  # Gradio Interface for Signup
99
  signup_interface = gr.Interface(fn=signup, inputs=["text", "text"], outputs="text", title="Signup")
 
101
  # Gradio Interface for Login
102
  login_interface = gr.Interface(fn=login, inputs="text", outputs="text", title="Login")
103
 
104
+ # Gradio Interface for Menu with Add Buttons
105
+ menu_buttons = generate_menu()
106
+ menu_interface = gr.Blocks()
107
+
108
+ with menu_interface:
109
+ with gr.Row():
110
+ gr.Markdown("# Menu")
111
+ for button, function in menu_buttons:
112
+ button.click(fn=function, outputs="text")
 
113
 
114
  # Gradio Interface for Cart
115
  cart_interface = gr.Interface(fn=view_cart, inputs=None, outputs="text", title="Cart")