foodmenu / app.py
Subbu1304's picture
Update app.py
fd89023 verified
import gradio as gr
import os
# File to store user credentials
USER_FILE = "users.txt"
# Ensure the file exists
def initialize_user_file():
if not os.path.exists(USER_FILE):
with open(USER_FILE, "w") as file:
pass
initialize_user_file()
# Function to check if user exists
def user_exists(phone):
with open(USER_FILE, "r") as file:
users = file.readlines()
for user in users:
stored_phone, _, _, _ = user.strip().split(",")
if stored_phone == phone:
return True
return False
# Function to validate login credentials
def validate_user(phone, password):
with open(USER_FILE, "r") as file:
users = file.readlines()
for user in users:
stored_phone, _, _, stored_password = user.strip().split(",")
if stored_phone == phone and stored_password == password:
return True
return False
# Function to handle login
def login(phone, password):
if validate_user(phone, password):
return "Login successful! Redirecting to the main page...", gr.update(visible=True), gr.update(visible=False)
else:
return "Invalid phone number or password. Please try again.", gr.update(visible=False), gr.update(visible=True)
# Function to handle signup
def signup(phone, name, email, password):
if user_exists(phone):
return "Phone number already exists. Please use a different phone number."
else:
with open(USER_FILE, "a") as file:
file.write(f"{phone},{name},{email},{password}\n")
return "Signup successful! You can now login."
# Main Page
def main_page():
with gr.Blocks() as main:
gr.Markdown("# Welcome to the Main Page")
gr.Text("You are successfully logged in!")
return main
# Login Interface
def login_interface():
with gr.Blocks() as login_demo:
gr.Markdown("# Login")
phone = gr.Textbox(label="Phone Number")
password = gr.Textbox(label="Password", type="password")
output = gr.Textbox(label="Output")
login_button = gr.Button("Login")
main_content = main_page()
main_content.visible = False
login_button.click(login, inputs=[phone, password], outputs=[output, main_content, login_demo])
return login_demo, main_content
# Signup Interface
def signup_interface():
with gr.Blocks() as signup_demo:
gr.Markdown("# Signup")
phone = gr.Textbox(label="Phone Number")
name = gr.Textbox(label="Name")
email = gr.Textbox(label="Email")
password = gr.Textbox(label="Password", type="password")
output = gr.Textbox(label="Output")
signup_button = gr.Button("Signup")
signup_button.click(signup, inputs=[phone, name, email, password], outputs=output)
return signup_demo
# Main interface
with gr.Blocks() as main_interface:
gr.Markdown("# Login and Signup System")
with gr.Tabs():
with gr.Tab("Login"):
login_demo, main_content = login_interface()
main_interface.append(main_content)
with gr.Tab("Signup"):
signup_interface()
if __name__ == "__main__":
main_interface.launch()