DSatishchandra commited on
Commit
1d8b98e
1 Parent(s): 0ff9b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -20
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import gradio as gr
2
- import random
3
  import uuid
4
 
5
- # Sample menu items
6
  menu_items = [
7
  {"id": 1, "name": "Burger", "price": 5.99},
8
  {"id": 2, "name": "Pizza", "price": 8.99},
@@ -11,23 +10,32 @@ menu_items = [
11
  {"id": 5, "name": "Soda", "price": 1.99}
12
  ]
13
 
14
- # Sample customer data for login (in a real system, use a database)
15
- customers = {
16
- "user1": {"name": "John Doe", "phone": "1234567890", "customer_id": "CUST001"},
17
- "user2": {"name": "Jane Smith", "phone": "9876543210", "customer_id": "CUST002"}
18
- }
19
-
20
- # Initialize session data
21
  session_data = {}
22
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def login(phone):
24
- for customer_id, details in customers.items():
25
- if details['phone'] == phone:
26
- session_data['customer_id'] = details['customer_id']
27
- session_data['customer_name'] = details['name']
28
- return f"Welcome {details['name']}!"
29
- return "Customer not found. Please register."
 
30
 
 
31
  def display_menu():
32
  if 'customer_id' not in session_data:
33
  return "Please log in first to view the menu."
@@ -35,6 +43,7 @@ def display_menu():
35
  menu = "\n".join([f"{item['id']}. {item['name']} - ${item['price']}" for item in menu_items])
36
  return f"Welcome, {session_data['customer_name']}! Here is the menu:\n\n{menu}"
37
 
 
38
  def add_to_cart(item_id, quantity):
39
  if 'customer_id' not in session_data:
40
  return "Please log in first to add items to your cart."
@@ -44,6 +53,7 @@ def add_to_cart(item_id, quantity):
44
  session_data['cart'] = cart
45
  return f"Item added to cart. Cart has {len(cart)} item(s)."
46
 
 
47
  def view_cart():
48
  if 'customer_id' not in session_data:
49
  return "Please log in first to view your cart."
@@ -58,6 +68,7 @@ def view_cart():
58
  total_price += item_details['price'] * item['quantity']
59
  return "\n".join(cart_items) + f"\nTotal Price: ${total_price}"
60
 
 
61
  def proceed_to_order(table_number):
62
  if 'customer_id' not in session_data:
63
  return "Please log in first to proceed with your order."
@@ -74,13 +85,21 @@ def proceed_to_order(table_number):
74
  session_data['cart'] = [] # Clear cart after order
75
  return f"Order ID: {order_id}\nTable Number: {table_number}\nOrder Details: {order_details}"
76
 
77
- # Gradio Interface
78
- login_interface = gr.Interface(fn=login, inputs="text", outputs="text", title="Customer Login")
 
 
 
 
 
79
  menu_interface = gr.Interface(fn=display_menu, inputs=None, outputs="text", title="Menu")
 
 
80
  cart_interface = gr.Interface(fn=view_cart, inputs=None, outputs="text", title="Cart")
 
 
81
  order_interface = gr.Interface(fn=proceed_to_order, inputs="text", outputs="text", title="Proceed to Order")
82
 
83
- # Launch Gradio app
84
- demo = gr.TabbedInterface([login_interface, menu_interface, cart_interface, order_interface], ["Login", "Menu", "Cart", "Order"])
85
  demo.launch()
86
-
 
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
  {"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}."
27
+
28
+ # Function for user login
29
  def login(phone):
30
+ if phone in customers:
31
+ customer = customers[phone]
32
+ session_data['customer_id'] = customer['customer_id']
33
+ session_data['customer_name'] = customer['name']
34
+ return f"Welcome {customer['name']}! You are now logged in."
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."
 
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."
 
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():
58
  if 'customer_id' not in session_data:
59
  return "Please log in first to view your cart."
 
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."
 
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()