Spaces:
Build error
Build error
| import gradio as gr | |
| import pandas as pd | |
| import os | |
| USER_FILE = "user_details.xlsx" | |
| def initialize_user_file(): | |
| if not os.path.exists(USER_FILE): | |
| df = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"]) | |
| df.to_excel(USER_FILE, index=False) | |
| print("Created new user details file.") | |
| def save_user(name, phone, email, password): | |
| initialize_user_file() | |
| df = pd.read_excel(USER_FILE) | |
| if email in df["Email"].values: | |
| return False | |
| new_user = pd.DataFrame([{"Name": name, "Phone": phone, "Email": email, "Password": password}]) | |
| df = pd.concat([df, new_user], ignore_index=True) | |
| df.to_excel(USER_FILE, index=False) | |
| return True | |
| def check_credentials(email, password): | |
| initialize_user_file() | |
| df = pd.read_excel(USER_FILE) | |
| user = df[(df["Email"] == email) & (df["Password"] == password)] | |
| return not user.empty | |
| # Cart to hold selected items | |
| cart = [] | |
| def filter_menu(preference): | |
| # Dummy menu data for demonstration | |
| menu_data = pd.DataFrame([ | |
| {"Dish Name": "Chicken Biryani", "Price ($)": 14, "Description": "Delicious biryani with tender chicken.", "Ingredients": "Chicken, Rice"}, | |
| {"Dish Name": "Paneer Tikka", "Price ($)": 12, "Description": "Grilled paneer with spices.", "Ingredients": "Paneer, Spices"}, | |
| {"Dish Name": "Veg Salad", "Price ($)": 10, "Description": "Healthy mix of vegetables.", "Ingredients": "Carrot, Cucumber"}, | |
| ]) | |
| if preference == "Halal/Non-Veg": | |
| filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken", case=False, na=False)] | |
| elif preference == "Vegetarian": | |
| filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken", case=False, na=False)] | |
| else: | |
| filtered_data = menu_data | |
| html_content = "" | |
| for index, item in filtered_data.iterrows(): | |
| html_content += f""" | |
| <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px;"> | |
| <div style="flex: 1; margin-right: 15px;"> | |
| <h3 style="margin: 0;">{item['Dish Name']}</h3> | |
| <p>${item['Price ($)']}</p> | |
| <p>{item['Description']}</p> | |
| </div> | |
| <div> | |
| <button onclick="addItemToCart({index})" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"> | |
| Add | |
| </button> | |
| </div> | |
| </div> | |
| """ | |
| return html_content | |
| def view_cart(): | |
| if not cart: | |
| return "Your cart is empty." | |
| cart_content = "<h3>Your Cart:</h3><ul>" | |
| total_price = 0 | |
| for item in cart: | |
| total_price += item["price"] | |
| cart_content += f"<li>{item['name']} - ${item['price']}</li>" | |
| cart_content += f"</ul><p><strong>Total: ${total_price}</strong></p>" | |
| return cart_content | |
| def add_to_cart(index): | |
| # Dummy menu data for demonstration | |
| menu_data = pd.DataFrame([ | |
| {"Dish Name": "Chicken Biryani", "Price ($)": 14, "Description": "Delicious biryani with tender chicken.", "Ingredients": "Chicken, Rice"}, | |
| {"Dish Name": "Paneer Tikka", "Price ($)": 12, "Description": "Grilled paneer with spices.", "Ingredients": "Paneer, Spices"}, | |
| {"Dish Name": "Veg Salad", "Price ($)": 10, "Description": "Healthy mix of vegetables.", "Ingredients": "Carrot, Cucumber"}, | |
| ]) | |
| item = menu_data.iloc[index] | |
| cart.append({"name": item["Dish Name"], "price": item["Price ($)"]}) | |
| return f"{item['Dish Name']} added to cart!" | |
| def app_interface(): | |
| def authenticate_user(email, password): | |
| if check_credentials(email, password): | |
| return gr.update(visible=False), gr.update(visible=True), "" | |
| else: | |
| return gr.update(visible=True), gr.update(visible=False), "Invalid email or password. Try again." | |
| def create_account(name, phone, email, password): | |
| if save_user(name, phone, email, password): | |
| return "Account created successfully!", gr.update(visible=True), gr.update(visible=False) | |
| else: | |
| return "Email already exists!", gr.update(visible=False), gr.update(visible=True) | |
| with gr.Blocks() as app: | |
| # Login Section | |
| with gr.Column(visible=True) as login_section: | |
| gr.Markdown("# Login Page") | |
| email = gr.Textbox(label="Email", placeholder="Enter your email") | |
| password = gr.Textbox(label="Password", placeholder="Enter your password", type="password") | |
| error_box = gr.Markdown("", visible=False) | |
| login_btn = gr.Button("Login") | |
| create_account_btn = gr.Button("Create an Account") | |
| # Sign-Up Section | |
| with gr.Column(visible=False) as signup_section: | |
| gr.Markdown("# Sign Up Page") | |
| name = gr.Textbox(label="Name", placeholder="Enter your full name") | |
| phone = gr.Textbox(label="Phone", placeholder="Enter your phone number") | |
| signup_email = gr.Textbox(label="Email", placeholder="Enter your email") | |
| signup_password = gr.Textbox(label="Password", placeholder="Enter a password", type="password") | |
| success_message = gr.Markdown("", visible=False) | |
| submit_btn = gr.Button("Sign Up") | |
| back_to_login_btn = gr.Button("Back to Login") | |
| # Menu Section | |
| with gr.Column(visible=False) as menu_section: | |
| gr.Markdown("## Menu") | |
| selected_preference = gr.Radio( | |
| choices=["All", "Vegetarian", "Halal/Non-Veg"], | |
| value="All", | |
| label="Choose a Preference", | |
| ) | |
| menu_output = gr.HTML(value=filter_menu("All")) | |
| cart_btn = gr.Button("View Cart") | |
| selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output]) | |
| gr.Row([selected_preference]) | |
| gr.Row(menu_output) | |
| gr.Row(cart_btn) | |
| # Cart Section | |
| with gr.Column(visible=False) as cart_section: | |
| gr.Markdown("## Your Cart") | |
| cart_content = gr.HTML(value=view_cart()) | |
| back_to_menu_btn = gr.Button("Back to Menu") | |
| gr.Row([cart_content]) | |
| gr.Row([back_to_menu_btn]) | |
| # Button Actions | |
| login_btn.click(authenticate_user, inputs=[email, password], outputs=[login_section, menu_section, error_box]) | |
| create_account_btn.click(lambda: (gr.update(visible=False), gr.update(visible=True)), inputs=[], outputs=[login_section, signup_section]) | |
| submit_btn.click(create_account, inputs=[name, phone, signup_email, signup_password], outputs=[success_message, signup_section, login_section]) | |
| back_to_login_btn.click(lambda: (gr.update(visible=True), gr.update(visible=False)), inputs=[], outputs=[login_section, signup_section]) | |
| cart_btn.click(lambda: (gr.update(visible=False), gr.update(visible=True)), inputs=[], outputs=[menu_section, cart_section]) | |
| back_to_menu_btn.click(lambda: (gr.update(visible=True), gr.update(visible=False)), inputs=[], outputs=[menu_section, cart_section]) | |
| return app | |
| if __name__ == "__main__": | |
| app = app_interface() | |
| app.launch() | |