DSatishchandra commited on
Commit
a02c36b
1 Parent(s): 1d8b98e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -24
app.py CHANGED
@@ -35,23 +35,21 @@ def login(phone):
35
  else:
36
  return "Phone number not found. Please register first."
37
 
38
- # Display menu after login
39
- def display_menu():
40
  if 'customer_id' not in session_data:
41
- return "Please log in first to view the menu."
42
 
43
- menu = "\n".join([f"{item['id']}. {item['name']} - ${item['price']}" for item in menu_items])
44
- return f"Welcome, {session_data['customer_name']}! Here is the menu:\n\n{menu}"
45
-
46
- # Add item to cart
47
- def add_to_cart(item_id, quantity):
48
- if 'customer_id' not in session_data:
49
- return "Please log in first to add items to your cart."
50
 
 
51
  cart = session_data.get('cart', [])
52
- cart.append({"item_id": item_id, "quantity": quantity})
53
  session_data['cart'] = cart
54
- return f"Item added to cart. Cart has {len(cart)} item(s)."
55
 
56
  # View cart contents
57
  def view_cart():
@@ -59,22 +57,26 @@ def view_cart():
59
  return "Please log in first to view your cart."
60
 
61
  cart = session_data.get('cart', [])
62
- cart_items = []
 
 
 
63
  total_price = 0
64
  for item in cart:
65
- item_details = next((menu_item for menu_item in menu_items if menu_item['id'] == item['item_id']), None)
66
- if item_details:
67
- cart_items.append(f"{item_details['name']} x {item['quantity']} - ${item_details['price'] * item['quantity']}")
68
- total_price += item_details['price'] * item['quantity']
69
- return "\n".join(cart_items) + f"\nTotal Price: ${total_price}"
70
 
71
  # Proceed to order function
72
  def proceed_to_order(table_number):
73
  if 'customer_id' not in session_data:
74
  return "Please log in first to proceed with your order."
75
 
76
- order_id = str(uuid.uuid4()) # Generate unique order ID
77
  cart = session_data.get('cart', [])
 
 
 
 
78
  order_details = {
79
  "order_id": order_id,
80
  "customer_id": session_data['customer_id'],
@@ -83,23 +85,39 @@ def proceed_to_order(table_number):
83
  "items": cart
84
  }
85
  session_data['cart'] = [] # Clear cart after order
86
- return f"Order ID: {order_id}\nTable Number: {table_number}\nOrder Details: {order_details}"
87
 
88
- # Gradio Interface for Signup (Registration)
89
  signup_interface = gr.Interface(fn=signup, inputs=["text", "text"], outputs="text", title="Signup")
90
 
91
  # Gradio Interface for Login
92
  login_interface = gr.Interface(fn=login, inputs="text", outputs="text", title="Login")
93
 
94
  # Gradio Interface for Menu
95
- menu_interface = gr.Interface(fn=display_menu, inputs=None, outputs="text", title="Menu")
 
 
 
 
 
 
 
 
96
 
97
  # Gradio Interface for Cart
98
  cart_interface = gr.Interface(fn=view_cart, inputs=None, outputs="text", title="Cart")
99
 
100
  # Gradio Interface for Proceed to Order
101
- order_interface = gr.Interface(fn=proceed_to_order, inputs="text", outputs="text", title="Proceed to Order")
 
 
 
 
 
102
 
103
  # Launch Gradio app with multiple tabs
104
- demo = gr.TabbedInterface([signup_interface, login_interface, menu_interface, cart_interface, order_interface], ["Signup", "Login", "Menu", "Cart", "Order"])
 
 
 
105
  demo.launch()
 
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():
 
57
  return "Please log in first to view your cart."
58
 
59
  cart = session_data.get('cart', [])
60
+ if not cart:
61
+ return "Your cart is empty."
62
+
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."
74
 
 
75
  cart = session_data.get('cart', [])
76
+ if not cart:
77
+ return "Your cart is empty. Add items to your cart before proceeding."
78
+
79
+ order_id = str(uuid.uuid4()) # Generate unique order ID
80
  order_details = {
81
  "order_id": order_id,
82
  "customer_id": session_data['customer_id'],
 
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")
92
 
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")
109
 
110
  # Gradio Interface for Proceed to Order
111
+ order_interface = gr.Interface(
112
+ fn=proceed_to_order,
113
+ inputs=gr.Textbox(label="Table Number"),
114
+ outputs="text",
115
+ title="Proceed to Order"
116
+ )
117
 
118
  # Launch Gradio app with multiple tabs
119
+ demo = gr.TabbedInterface(
120
+ [signup_interface, login_interface, menu_interface, cart_interface, order_interface],
121
+ ["Signup", "Login", "Menu", "Cart", "Order"]
122
+ )
123
  demo.launch()