File size: 3,183 Bytes
a82e10a c92c22c a82e10a c92c22c a82e10a c92c22c a82e10a c92c22c a82e10a c92c22c fd89023 a82e10a fd89023 a82e10a c92c22c a82e10a c92c22c a82e10a fd89023 a82e10a fd89023 a82e10a c92c22c a82e10a fd89023 a82e10a fd89023 a82e10a fd89023 a82e10a fd89023 a82e10a c92c22c a82e10a c92c22c a82e10a fd89023 a82e10a fd89023 a82e10a c92c22c fd89023 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 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()
|