|
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 |
|
|
|
|
|
current_user = {"email": None, "name": None} |
|
|
|
|
|
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}" |
|
|
|
|
|
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}" |
|
|
|
|
|
def load_menu(preference): |
|
items = fetch_menu_items() |
|
print(f"Fetched menu items: {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") |
|
] |
|
print(f"Filtered menu items: {filtered_items}") |
|
return filtered_items |
|
|
|
def display_menu(preference): |
|
menu_items = fetch_menu_items() |
|
|
|
if preference == "Veg": |
|
filtered_items = [item for item in menu_items if item["Veg_NonVeg"] == "Veg"] |
|
elif preference == "Non-Veg": |
|
filtered_items = [item for item in menu_items if item["Veg_NonVeg"] == "Non veg"] |
|
else: |
|
filtered_items = menu_items |
|
|
|
|
|
return [[item["Name"], item["Price"], item["Description"]] for item in filtered_items] |
|
|
|
|
|
|
|
|
|
def add_to_cart_interface(item_name, price): |
|
add_to_cart(item_name, price) |
|
return f"Added {item_name} to cart!" |
|
|
|
|
|
def view_cart_interface(): |
|
cart, total = view_cart() |
|
return cart, total |
|
|
|
|
|
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)}" |
|
|
|
|
|
with gr.Blocks() as app: |
|
preference_radio = gr.Radio(["All", "Veg", "Non-Veg"], label="Filter Menu", value="All") |
|
menu_data_table = gr.Dataframe(headers=["Name", "Price", "Description"], datatype=["str", "number", "str"]) |
|
|
|
|
|
menu_button = gr.Button("Show Menu") |
|
|
|
|
|
menu_button.click( |
|
display_menu, |
|
inputs=preference_radio, |
|
outputs=menu_data_table |
|
) |
|
|
|
|
|
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() |
|
|