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|
+
# Initialize DB with users table
|
| 9 |
+
def init_db():
|
| 10 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 11 |
+
cursor = conn.cursor()
|
| 12 |
+
cursor.execute("""
|
| 13 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 14 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 15 |
+
username TEXT UNIQUE,
|
| 16 |
+
password TEXT
|
| 17 |
+
)
|
| 18 |
+
""")
|
| 19 |
+
conn.commit()
|
| 20 |
+
conn.close()
|
| 21 |
+
|
| 22 |
+
# Signup function
|
| 23 |
+
def signup(username, password):
|
| 24 |
+
init_db()
|
| 25 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 26 |
+
cursor = conn.cursor()
|
| 27 |
+
try:
|
| 28 |
+
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
| 29 |
+
conn.commit()
|
| 30 |
+
conn.close()
|
| 31 |
+
return "β
Signup successful! You can now login."
|
| 32 |
+
except sqlite3.IntegrityError:
|
| 33 |
+
conn.close()
|
| 34 |
+
return "β οΈ Username already exists. Try another."
|
| 35 |
+
|
| 36 |
+
# Login function
|
| 37 |
+
def login(username, password):
|
| 38 |
+
global current_user
|
| 39 |
+
init_db()
|
| 40 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 41 |
+
cursor = conn.cursor()
|
| 42 |
+
|
| 43 |
+
# Special case: admin 1234/1234
|
| 44 |
+
if username == "1234" and password == "1234":
|
| 45 |
+
current_user = "Admin"
|
| 46 |
+
conn.close()
|
| 47 |
+
return "admin"
|
| 48 |
+
|
| 49 |
+
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 50 |
+
user = cursor.fetchone()
|
| 51 |
+
conn.close()
|
| 52 |
+
if user:
|
| 53 |
+
current_user = username
|
| 54 |
+
return "home"
|
| 55 |
+
return "fail"
|
| 56 |
+
|
| 57 |
+
# Logout function
|
| 58 |
+
def logout():
|
| 59 |
+
global current_user
|
| 60 |
+
current_user = None
|
| 61 |
+
return "πͺ You have been logged out.", home_page(), gr.update(visible=False)
|
| 62 |
+
|
| 63 |
+
# Home page view
|
| 64 |
+
def home_page():
|
| 65 |
+
if current_user:
|
| 66 |
+
return f"π Welcome, **{current_user}**! You are logged in."
|
| 67 |
+
else:
|
| 68 |
+
return "π Welcome to the Home Page! Please signup or login to continue."
|
| 69 |
+
|
| 70 |
+
# Download DB file
|
| 71 |
+
def get_db_file():
|
| 72 |
+
if os.path.exists(DB_PATH):
|
| 73 |
+
return DB_PATH
|
| 74 |
+
return None
|
| 75 |
+
|
| 76 |
+
# Build Gradio app
|
| 77 |
+
with gr.Blocks(css="""
|
| 78 |
+
body {background-color: #f5f7fa; font-family: 'Segoe UI', sans-serif;}
|
| 79 |
+
.gr-button {border-radius: 10px !important; font-weight: bold;}
|
| 80 |
+
.gr-textbox, .gr-number {border-radius: 8px !important;}
|
| 81 |
+
h1, h2 {color: #2c3e50;}
|
| 82 |
+
""") as demo:
|
| 83 |
+
|
| 84 |
+
gr.Markdown("<h1 style='text-align:center;'>π Simple Login System</h1>")
|
| 85 |
+
|
| 86 |
+
with gr.Tabs():
|
| 87 |
+
with gr.Tab("π Home"):
|
| 88 |
+
home_out = gr.Textbox(label="Message", value=home_page(), interactive=False)
|
| 89 |
+
logout_btn = gr.Button("πͺ Logout")
|
| 90 |
+
logout_status = gr.Textbox(label="Logout Status")
|
| 91 |
+
|
| 92 |
+
logout_btn.click(logout, outputs=[logout_status, home_out, None])
|
| 93 |
+
|
| 94 |
+
with gr.Tab("π Signup"):
|
| 95 |
+
su_user = gr.Textbox(label="New Username")
|
| 96 |
+
su_pass = gr.Textbox(label="New Password", type="password")
|
| 97 |
+
su_btn = gr.Button("Signup")
|
| 98 |
+
su_out = gr.Textbox(label="Status")
|
| 99 |
+
su_btn.click(signup, inputs=[su_user, su_pass], outputs=su_out)
|
| 100 |
+
|
| 101 |
+
with gr.Tab("π Login"):
|
| 102 |
+
lg_user = gr.Textbox(label="Username")
|
| 103 |
+
lg_pass = gr.Textbox(label="Password", type="password")
|
| 104 |
+
lg_btn = gr.Button("Login")
|
| 105 |
+
lg_status = gr.Textbox(label="Status")
|
| 106 |
+
|
| 107 |
+
file_dl = gr.File(label="β¬οΈ Download temp.db (Admin only)", type="file", visible=False)
|
| 108 |
+
|
| 109 |
+
def handle_login(username, password):
|
| 110 |
+
result = login(username, password)
|
| 111 |
+
if result == "admin":
|
| 112 |
+
return (
|
| 113 |
+
"β
Logged in as Admin. You can download the DB.",
|
| 114 |
+
get_db_file(),
|
| 115 |
+
gr.update(visible=True),
|
| 116 |
+
home_page()
|
| 117 |
+
)
|
| 118 |
+
elif result == "home":
|
| 119 |
+
return (
|
| 120 |
+
"β
Login successful!",
|
| 121 |
+
None,
|
| 122 |
+
gr.update(visible=False),
|
| 123 |
+
home_page()
|
| 124 |
+
)
|
| 125 |
+
else:
|
| 126 |
+
return (
|
| 127 |
+
"β Invalid credentials. Try again.",
|
| 128 |
+
None,
|
| 129 |
+
gr.update(visible=False),
|
| 130 |
+
home_page()
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
lg_btn.click(
|
| 134 |
+
handle_login,
|
| 135 |
+
inputs=[lg_user, lg_pass],
|
| 136 |
+
outputs=[lg_status, file_dl, file_dl, home_out]
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
demo.launch()
|