Spaces:
Sleeping
Sleeping
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() | |
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") | |
] | |
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() | |