Spaces:
Build error
Build error
Update app.py
Browse files4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394041218 unmodified lines2602612622632642652662672682692702712722732 unmodified lines2762772782792802812822832842852862872882892902914 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 {}# 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 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 "Signup successful! Redirecting to the login page...","login"# 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 the menu page...","menu"return "Invalid email or password.", "login"# Function to load the menu datadef load_menu():218 unmodified lines# Gradio appdef app():with gr.Blocks() as demo:gr.Markdown("## Secure Food Ordering System")# State variables to track the current page and active usercurrent_page = gr.State("signup") # Default page is Signupactive_user = gr.State(None) # No user is active initially# Signup Pagewith gr.Row(visible=lambda current_page: current_page == "signup") as signup_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()# Login Pagewith gr.Row(visible=lambda current_page: current_page == "login") as login_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()# Menu Pagewith gr.Row(visible=lambda current_page: current_page == "menu") as menu_page:gr.Markdown("### Menu Page (Accessible Only After Login)")# Adding your original code here without any changes4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394041424344454647218 unmodified lines2662672682692702712722732742752 unmodified lines2782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143154 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['Password'] 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 {}# Save users to Exceldef save_users(users):df = pd.DataFrame([{"Name": name, "Phone": phone, "Email": email, "Password": password} for email, (name, phone, password) in users.items()],columns=["Name", "Phone", "Email", "Password"])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 log in."# Hash the password before savinghashed_password = hashpw(password.encode(), gensalt()).decode()users[email] = (name, phone, hashed_password)save_users(users)return "Signup successful! Redirecting to login..."# Validate logindef validate_login(email, password):users = load_users()if email in users and checkpw(password.encode(), users[email][2].encode()):return True, "Login successful! Redirecting to the menu page..."return False, "Invalid email or password. Please try again."# Function to load the menu datadef load_menu():218 unmodified lines# Gradio appdef app():with gr.Blocks() as demo:current_page = gr.State(value="signup") # Track current visible 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()def handle_signup(name, phone, email, password):message = signup_user(name, phone, email, password)return "login", messagesignup_btn.click(handle_signup,inputs=[name, phone, email, password],outputs=[current_page, 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()def handle_login(email, password):success, message = validate_login(email, password)if success:return "menu", messagereturn "login", messagelogin_btn.click(handle_login,inputs=[login_email, login_password],outputs=[current_page, login_message],)# Menu Pagewith gr.Row(visible=lambda state: state == "menu", state=current_page):gr.Markdown("### Menu Page (Accessible Only After Login)")# Adding your original code here without any changes
app.py+41-17