Food_Order / app.py
neerajkalyank's picture
Update app.py
b3c4436 verified
import gradio as gr
import pandas as pd
import os
# Initialize order and customer database
DATA_FILE = "customer_data.xlsx"
if not os.path.exists(DATA_FILE):
df = pd.DataFrame(columns=["Name", "Phone", "Preferences", "Allergies", "Order"])
df.to_excel(DATA_FILE, index=False)
ORDER_NOTIFICATION_FILE = "orders.xlsx"
if not os.path.exists(ORDER_NOTIFICATION_FILE):
orders_df = pd.DataFrame(columns=["Name", "Phone", "Order"])
orders_df.to_excel(ORDER_NOTIFICATION_FILE, index=False)
# Save customer data
def save_customer_data(name, phone, preferences, allergies):
df = pd.read_excel(DATA_FILE)
new_entry = {"Name": name, "Phone": phone, "Preferences": preferences, "Allergies": allergies, "Order": ""}
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
df.to_excel(DATA_FILE, index=False)
# Save order for notification
def save_order_notification(name, phone, order):
orders_df = pd.read_excel(ORDER_NOTIFICATION_FILE)
new_order = {"Name": name, "Phone": phone, "Order": order}
orders_df = pd.concat([orders_df, pd.DataFrame([new_order])], ignore_index=True)
orders_df.to_excel(ORDER_NOTIFICATION_FILE, index=False)
# Menu data
menu = [
{"name": "Paneer Butter Masala", "type": "Vegetarian"},
{"name": "Chicken Biryani", "type": "Non-Vegetarian"},
{"name": "Dal Tadka", "type": "Vegetarian"},
{"name": "Fish Curry", "type": "Non-Vegetarian"}
]
# Get filtered menu based on preferences
def get_filtered_menu(preferences):
if preferences == "Vegetarian":
return [item["name"] for item in menu if item["type"] == "Vegetarian"]
elif preferences == "Non-Vegetarian":
return [item["name"] for item in menu if item["type"] == "Non-Vegetarian"]
return [item["name"] for item in menu]
# Gradio app
with gr.Blocks() as app:
# Variables to store user data across screens
user_name = gr.State()
user_phone = gr.State()
user_preferences = gr.State()
user_allergies = gr.State()
user_order = gr.State([])
# Login Screen
with gr.Tab("Login"):
gr.Markdown("# Welcome to BH Restaurant")
name_input = gr.Textbox(label="Name", placeholder="Enter your name")
phone_input = gr.Textbox(label="Phone Number", placeholder="Enter your 10-digit phone number")
login_btn = gr.Button("Login")
login_output = gr.Textbox(visible=False)
def login(name, phone):
if not name or not phone:
return "Please enter both Name and Phone Number.", None, None
return "Login successful! Redirecting to preferences...", name, phone
login_btn.click(login, inputs=[name_input, phone_input], outputs=[login_output, user_name, user_phone])
# Preferences Screen
with gr.Tab("Preferences"):
gr.Markdown("# Set Your Preferences")
preferences_input = gr.Radio(
["Vegetarian", "Non-Vegetarian", "Complete Menu"], label="Food Preferences"
)
allergies_input = gr.Textbox(label="Any Allergies?")
save_pref_btn = gr.Button("Save Preferences")
pref_output = gr.Textbox(visible=False)
def save_preferences(preferences, allergies, name, phone):
save_customer_data(name, phone, preferences, allergies)
return "Preferences saved! Redirecting to menu...", preferences, allergies
save_pref_btn.click(
save_preferences,
inputs=[preferences_input, allergies_input, user_name, user_phone],
outputs=[pref_output, user_preferences, user_allergies],
)
# Menu Screen
with gr.Tab("Menu"):
gr.Markdown("# Menu")
menu_dropdown = gr.Dropdown(choices=[], label="Select an Item")
add_to_order_btn = gr.Button("Add to Order")
order_summary = gr.Textbox(label="Current Order", interactive=False)
def load_menu(preferences):
return gr.update(choices=get_filtered_menu(preferences))
def add_to_order(item, order_list):
if item:
order_list.append(item)
return ", ".join(order_list), order_list
menu_dropdown.change(load_menu, inputs=[user_preferences], outputs=[menu_dropdown])
add_to_order_btn.click(add_to_order, inputs=[menu_dropdown, user_order], outputs=[order_summary, user_order])
# Order Summary Screen
with gr.Tab("Order Summary"):
gr.Markdown("# Order Summary")
final_order_summary = gr.Textbox(label="Your Final Order", interactive=False)
place_order_btn = gr.Button("Place Order")
thank_you_output = gr.Textbox(visible=False)
def place_order(order_list, name, phone):
order_str = ", ".join(order_list)
save_order_notification(name, phone, order_str)
return "Thank you for your order! Redirecting to Thank You page."
place_order_btn.click(
place_order,
inputs=[user_order, user_name, user_phone],
outputs=[thank_you_output],
)
# Thank You Page
with gr.Tab("Thank You"):
gr.Markdown("# Thank You!")
gr.Markdown("Your order has been placed successfully. Please wait while the chef prepares your meal.")
# Chef/Waiter Notification Screen
with gr.Tab("Chef/Waiter Notifications"):
gr.Markdown("# Order Notifications")
refresh_btn = gr.Button("Refresh Orders")
order_list = gr.Textbox(label="Orders", interactive=False)
def load_orders():
orders_df = pd.read_excel(ORDER_NOTIFICATION_FILE)
return orders_df.to_string(index=False)
refresh_btn.click(load_orders, outputs=[order_list])
# Launch the app
app.launch()