Spaces:
Sleeping
Sleeping
File size: 4,149 Bytes
c053032 33eba6b 444fe60 c053032 444fe60 c053032 8f55c48 c053032 444fe60 c053032 3ad292c c053032 3ad292c c053032 3ad292c c053032 444fe60 3ad292c c053032 444fe60 c053032 33eba6b c053032 444fe60 c053032 33eba6b 444fe60 c053032 444fe60 33eba6b c053032 444fe60 c053032 444fe60 c053032 |
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 |
import gradio as gr
from models.salesforce import fetch_menu_items, place_order_in_salesforce
from models.cart import add_to_cart, view_cart, clear_cart
from models.user import login_user, signup_user
# Global user state
current_user = {"email": None, "name": None}
# Login Interface
def login(email, password):
success, message = login_user(email, password)
if success:
current_user["email"] = email
current_user["name"] = message
return "Login successful! Redirecting to menu..."
return f"Login failed: {message}"
# Signup Interface
def signup(name, email, phone, password):
success, message = signup_user(name, email, phone, password)
if success:
return "Signup successful! Please login now."
return f"Signup failed: {message}"
# Load Menu Items
def load_menu(preference):
items = fetch_menu_items()
print(f"Fetched menu items: {items}") # Debugging output
filtered_items = [
item for item in items
if preference == "All"
or (preference == "Veg" and item["Veg_NonVeg__c"] == "Veg")
or (preference == "Non-Veg" and item["Veg_NonVeg__c"] == "Non-Veg")
]
print(f"Filtered menu items: {filtered_items}") # Debugging output
return filtered_items
# Add to Cart
def add_to_cart_interface(item_name, price):
add_to_cart(item_name, price)
return f"Added {item_name} to cart!"
# View Cart
def view_cart_interface():
cart, total = view_cart()
return cart, total
# Place Order
def place_order_interface():
email = current_user["email"]
if not email:
return "Error: Please login to place an order."
cart, total = view_cart()
if not cart:
return "Error: Cart is empty!"
order_details = "\n".join([f"{item['Name']} - ${item['Price']} x {item['Quantity']}" for item in cart])
try:
place_order_in_salesforce(email, order_details, total)
clear_cart()
return f"Order placed successfully! Total: ${total}"
except Exception as e:
return f"Error placing order: {str(e)}"
# Gradio Interfaces
with gr.Blocks() as app:
with gr.Tab("Login"):
email = gr.Textbox(label="Email", placeholder="Enter your email")
password = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
login_button = gr.Button("Login")
login_output = gr.Textbox(label="Login Status")
login_button.click(login, inputs=[email, password], outputs=login_output)
with gr.Tab("Signup"):
name = gr.Textbox(label="Name", placeholder="Enter your name")
signup_email = gr.Textbox(label="Email", placeholder="Enter your email")
phone = gr.Textbox(label="Phone", placeholder="Enter your phone number")
signup_password = gr.Textbox(label="Password", type="password", placeholder="Create a password")
signup_button = gr.Button("Signup")
signup_output = gr.Textbox(label="Signup Status")
signup_button.click(signup, inputs=[name, signup_email, phone, signup_password], outputs=signup_output)
with gr.Tab("Menu"):
preference = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Menu", value="All")
menu_output = gr.Dataframe(headers=["Name", "Description", "Price"])
preference.change(lambda p: load_menu(p), inputs=[preference], outputs=menu_output)
item_name = gr.Textbox(label="Item Name")
item_price = gr.Textbox(label="Price")
add_button = gr.Button("Add to Cart")
cart_status = gr.Textbox(label="Cart Status")
add_button.click(add_to_cart_interface, inputs=[item_name, item_price], outputs=cart_status)
with gr.Tab("Cart"):
view_button = gr.Button("View Cart")
cart_output = gr.Dataframe(headers=["Name", "Price", "Quantity"])
total_output = gr.Textbox(label="Total Amount")
view_button.click(view_cart_interface, outputs=[cart_output, total_output])
place_order_button = gr.Button("Place Order")
order_status = gr.Textbox(label="Order Status")
place_order_button.click(place_order_interface, outputs=order_status)
app.launch()
|