Spaces:
Build error
Build error
Update app.py
Browse files4 unmodified lines567891011121314151617187 unmodified lines2627282930313233343536373839404142434445212 unmodified lines2582592602612622632642652662672682692702712722732742752762772782792802812822832842852862 unmodified lines28929029129229329429529629729829930030130230356 unmodified lines3603613623634 unmodified lines# Path to the Excel file for user storageUSER_FILE = "users.xlsx"# Load users from Excel# Load users from Exceldef load_users():try:df = pd.read_excel(USER_FILE)return {row['Email']: row for _, row in df.iterrows()}except FileNotFoundError:# Create the file if it doesn't existdf = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])df.to_excel(USER_FILE, index=False)return {}7 unmodified linesdef signup_user(name, phone, email, password):users = load_users()if email in users:return "Email already exists. Please use a different email.", gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)hashed_password = hashpw(password.encode(), gensalt()).decode()users[email] = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}save_users(users)return "Signup successful! Redirecting to login page...", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)# Validate logindef validate_login(email, password):users = load_users()if email in users and checkpw(password.encode(), users[email]["Password"].encode()):return "Login successful! Redirecting to menu page...", gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)return "Invalid email or password. Please try again.", gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)# Function to load the menu datadef load_menu():menu_file = "menu.xlsx" # Ensure this file exists in the same directory212 unmodified lines}</script>"""def app():with gr.Blocks() as demo:# Define visibility stateslogin_visible = gr.State(True)signup_visible = gr.State(False)menu_visible = gr.State(False)# Login Pagewith gr.Row(visible=login_visible):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(validate_login,inputs=[login_email, login_password],outputs=[login_message, login_visible, signup_visible, menu_visible],)# Signup Pagewith gr.Row(visible=signup_visible):gr.Markdown("### Signup Page")name = gr.Textbox(label="Name")phone = gr.Textbox(label="Phone Number")2 unmodified linessignup_btn = gr.Button("Signup")signup_message = gr.Label()signup_btn.click(signup_user,inputs=[name, phone, email, password],outputs=[signup_message, login_visible, signup_visible, menu_visible],)# Menu Pagewith gr.Row(visible=menu_visible):gr.Markdown("### Menu Page")selected_preference = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",56 unmodified linesif __name__ == "__main__":app().launch()4 unmodified lines56789101112131415167 unmodified lines2425262728293031323334353637383940414243444546474849505152535455212 unmodified lines2682692702712722732742752762772782792802812822832842852862 unmodified lines28929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233356 unmodified lines3903913923934 unmodified lines# Path to the Excel file for user storageUSER_FILE = "users.xlsx"# Load users from Exceldef 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 {}7 unmodified linesdef signup_user(name, phone, email, password):users = load_users()if email in users:return False, "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 True, "Signup successful! Redirecting to the login page..."# 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! Redirecting to the menu page..."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 True, messagereturn False, messagedef is_logged_in(email):return active_sessions.get(email, False)# Function to load the menu datadef load_menu():menu_file = "menu.xlsx" # Ensure this file exists in the same directory212 unmodified lines}</script>"""# Gradio appdef app():with gr.Blocks() as demo:gr.Markdown("## Secure Food Ordering System")# State variables to track redirectionwith gr.State() as current_page: # Holds the current visible pagecurrent_page.set("signup") # Default to signup page# Signup Pagewith gr.Row(visible=lambda state: state == "signup", state=current_page):gr.Markdown("### Signup Page")name = gr.Textbox(label="Name")phone = gr.Textbox(label="Phone Number")2 unmodified linessignup_btn = gr.Button("Signup")signup_message = gr.Label()# Handle Signupdef handle_signup(name, phone, email, password):success, message = signup_user(name, phone, email, password)if success:current_page.set("login") # Redirect to login pagereturn messagesignup_btn.click(handle_signup,inputs=[name, phone, email, password],outputs=[signup_message],)# Login Pagewith gr.Row(visible=lambda state: state == "login", state=current_page):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()# Handle Logindef handle_login(email, password):success, message = login_user(email, password)if success:current_page.set("menu") # Redirect to menu pagereturn messagelogin_btn.click(handle_login,inputs=[login_email, login_password],outputs=[login_message],)# Menu Pagewith gr.Row(visible=lambda state: state == "menu", state=current_page):gr.Markdown("### Menu Page (Accessible Only After Login)")# Radio button for selecting preferenceselected_preference = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",56 unmodified linesif __name__ == "__main__":app().launch()
app.py+59-29