import gradio as gr import pandas as pd from bcrypt import hashpw, gensalt, checkpw # Path to the Excel file for user storage USER_FILE = "users.xlsx" # Load users from Excel def load_users(): try: df = pd.read_excel(USER_FILE) return {row['Username']: row['Password'] for _, row in df.iterrows()} except FileNotFoundError: df = pd.DataFrame(columns=["Username", "Password"]) df.to_excel(USER_FILE, index=False) return {} # Save users to Excel def save_users(users): df = pd.DataFrame(list(users.items()), columns=["Username", "Password"]) df.to_excel(USER_FILE, index=False) # Signup user def signup_user(username, password): users = load_users() if username in users: return "Username already exists. Please choose a different username." hashed_password = hashpw(password.encode(), gensalt()).decode() users[username] = hashed_password save_users(users) return "Signup successful! Please log in." # Validate login def validate_login(username, password): users = load_users() if username in users and checkpw(password.encode(), users[username].encode()): return True, "Login successful! Redirecting to the menu page..." return False, "Invalid username or password. Please try again." # Session management active_sessions = {} def login_user(username, password): success, message = validate_login(username, password) if success: active_sessions[username] = True return message return message def is_logged_in(username): return active_sessions.get(username, False) def logout_user(username): if username in active_sessions: del active_sessions[username] return f"{username} has been logged out successfully." return "No active session found for this user." # Menu functionality: Your original code will remain here as-is # Keeping all your existing menu, cart, modal logic intact # Function to load the menu data def load_menu(): menu_file = "menu.xlsx" # Ensure this file exists in the same directory try: return pd.read_excel(menu_file) except Exception as e: raise ValueError(f"Error loading menu file: {e}") # Function to filter menu items based on preference def filter_menu(preference): menu_data = load_menu() if preference == "Halal/Non-Veg": filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)] elif preference == "Vegetarian": filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)] elif preference == "Guilt-Free": filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)] else: filtered_data = menu_data html_content = "" for _, item in filtered_data.iterrows(): html_content += f"""

{item['Dish Name']}

${item['Price ($)']}

{item['Description']}

{item['Dish Name']}
""" return html_content # JavaScript for modal and cart behavior modal_and_cart_js = """ """ # Gradio app def app(): with gr.Blocks() as demo: gr.Markdown("## Secure Food Ordering System") # Login and Signup Tabs with gr.Tabs(): with gr.Tab("Login"): username = gr.Textbox(label="Username") password = gr.Textbox(label="Password", type="password") login_btn = gr.Button("Login") login_message = gr.Label() login_btn.click(login_user, [username, password], [login_message]) with gr.Tab("Signup"): signup_username = gr.Textbox(label="Username") signup_password = gr.Textbox(label="Password", type="password") signup_btn = gr.Button("Signup") signup_message = gr.Label() signup_btn.click(signup_user, [signup_username, signup_password], [signup_message]) # Menu Page Tab with gr.Tab("Menu Page"): gr.Markdown("### Menu Page") # Preserve your menu-related code exactly as it was # Add your exact original menu, cart, and modal code here selected_preference = gr.Radio( choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"], value="All", label="Choose a Preference", ) # Output area for menu items menu_output = gr.HTML(value=filter_menu("All")) # Floating cart display cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart") # Final order display final_order_output = gr.HTML(value="", elem_id="final-order") # Modal window modal_window = gr.HTML(""" """) # Update menu dynamically based on preference selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output]) # Layout gr.Row([selected_preference]) gr.Row(menu_output) gr.Row(cart_output) gr.Row(modal_window) gr.Row(final_order_output) gr.HTML(modal_and_cart_js) return demo if __name__ == "__main__": app().launch()