Spaces:
Build error
Build error
Update app.py
Browse files8 unmodified lines91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697024 unmodified lines9596979899100101174 unmodified lines2762772782792802811 unmodified line2832842852862872882892902912922932942952962972982993003013023033043053063073083093103119 unmodified lines32132232332432532632735 unmodified lines3633643653663673683698 unmodified linesdef 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 Exceldef save_users(users):df = pd.DataFrame(list(users.items()), columns=["Username", "Password"])df.to_excel(USER_FILE, index=False)# Signup userdef 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_passwordsave_users(users)return "Signup successful! Please log in."# Validate logindef 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 managementactive_sessions = {}def login_user(username, password):success, message = validate_login(username, password)if success:active_sessions[username] = Truereturn messagereturn messagedef 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 datadef load_menu():menu_file = "menu.xlsx" # Ensure this file exists in the same directorytry: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 preferencedef filter_menu(preference):24 unmodified lines"""return html_content# JavaScript for modal and cart behaviormodal_and_cart_js = """<style>.cart-container {174 unmodified lines</script>"""# Gradio appdef app():with gr.Blocks() as demo:1 unmodified line# Login and Signup Tabswith 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 Tabwith 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 hereselected_preference = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",9 unmodified lines# Final order displayfinal_order_output = gr.HTML(value="", elem_id="final-order")# Modal windowmodal_window = gr.HTML("""<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;"><div style="text-align: right;">35 unmodified linesgr.Row(modal_window)gr.Row(final_order_output)gr.HTML(modal_and_cart_js)return demoif __name__ == "__main__":app().launch()8 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596024 unmodified lines85868788899091174 unmodified lines2662672682692702712721 unmodified line2742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293309 unmodified lines34034134234334434534635 unmodified lines3823833843853863873883893908 unmodified linesdef load_users():try:df = pd.read_excel(USER_FILE)return {row['Email']: row for _, row in df.iterrows()}except FileNotFoundError:df = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])df.to_excel(USER_FILE, index=False)return {}# Save users to Exceldef save_users(users):df = pd.DataFrame(users.values())df.to_excel(USER_FILE, index=False)# Signup userdef signup_user(name, phone, email, password):users = load_users()if email in users:return "Email already exists. Please use a different email."hashed_password = hashpw(password.encode(), gensalt()).decode()users[email] = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}save_users(users)return "Signup successful! Please log in."# Validate logindef validate_login(email, password):users = load_users()if email in users and checkpw(password.encode(), users[email]["Password"].encode()):return True, "Login successful!"return False, "Invalid email or password."# Session managementactive_sessions = {}def login_user(email, password):success, message = validate_login(email, password)if success:active_sessions[email] = Truereturn messagereturn messagedef is_logged_in(email):return active_sessions.get(email, False)def logout_user(email):if email in active_sessions:del active_sessions[email]return "Logged out successfully!"return "No active session found for this email."# Function to filter menu items based on preferencedef filter_menu(preference):24 unmodified lines"""return html_content# Preserving your JavaScript for modal and cart functionality (Your original JavaScript logic)modal_and_cart_js = """<style>.cart-container {174 unmodified lines</script>"""# Gradio appdef app():with gr.Blocks() as demo:1 unmodified line# Login and Signup Tabswith gr.Tabs():# Signup Tabwith gr.Tab("Signup"):gr.Markdown("### Signup Page")name = gr.Textbox(label="Name")phone = gr.Textbox(label="Phone Number")email = gr.Textbox(label="Email")password = gr.Textbox(label="Password", type="password")signup_btn = gr.Button("Signup")signup_message = gr.Label()signup_btn.click(signup_user,inputs=[name, phone, email, password],outputs=[signup_message],)# Login Tabwith gr.Tab("Login"):gr.Markdown("### Login Page")login_email = gr.Textbox(label="Email")login_password = gr.Textbox(label="Password", type="password")login_btn = gr.Button("Login")login_message = gr.Label()login_btn.click(login_user,inputs=[login_email, login_password],outputs=[login_message],)# Menu Pagewith gr.Tab("Menu Page"):gr.Markdown("### Menu Page (Accessible Only After Login)")# Input for user email to verify loginemail_input = gr.Textbox(label="Enter Email to Verify Access")verify_btn = gr.Button("Load Menu")menu_output = gr.HTML(value="") # Output for menu itemsdef verify_and_load_menu(email):if is_logged_in(email):return filter_menu("All") # Filtered menu based on preferencereturn "Access Denied! Please log in."verify_btn.click(verify_and_load_menu,inputs=[email_input],outputs=[menu_output],)# Radio button for selecting preferenceselected_preference = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",9 unmodified lines# Final order displayfinal_order_output = gr.HTML(value="", elem_id="final-order")# Modal windowmodal_window = gr.HTML("""<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;"><div style="text-align: right;">35 unmodified linesgr.Row(modal_window)gr.Row(final_order_output)gr.HTML(modal_and_cart_js)return demoif __name__ == "__main__":app().launch()
app.py+70-49