Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
-
import sqlite3
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
DB_PATH = "
|
| 6 |
-
current_user = None # global to store logged-in user
|
| 7 |
|
| 8 |
-
#
|
| 9 |
def init_db():
|
| 10 |
-
conn = sqlite3.connect(DB_PATH
|
| 11 |
cursor = conn.cursor()
|
| 12 |
cursor.execute("""
|
| 13 |
CREATE TABLE IF NOT EXISTS users (
|
|
@@ -19,121 +18,80 @@ def init_db():
|
|
| 19 |
conn.commit()
|
| 20 |
conn.close()
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
def signup(username, password):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 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!
|
| 32 |
except sqlite3.IntegrityError:
|
| 33 |
conn.close()
|
| 34 |
-
return "
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 50 |
user = cursor.fetchone()
|
| 51 |
conn.close()
|
|
|
|
| 52 |
if user:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
# Logout function
|
| 58 |
def logout():
|
| 59 |
-
|
| 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 |
-
|
| 71 |
-
def get_db_file():
|
| 72 |
if os.path.exists(DB_PATH):
|
| 73 |
return DB_PATH
|
| 74 |
return None
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
with gr.Blocks(
|
| 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 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 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()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import sqlite3
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
DB_PATH = "temp.db"
|
|
|
|
| 6 |
|
| 7 |
+
# --- Database Setup ---
|
| 8 |
def init_db():
|
| 9 |
+
conn = sqlite3.connect(DB_PATH)
|
| 10 |
cursor = conn.cursor()
|
| 11 |
cursor.execute("""
|
| 12 |
CREATE TABLE IF NOT EXISTS users (
|
|
|
|
| 18 |
conn.commit()
|
| 19 |
conn.close()
|
| 20 |
|
| 21 |
+
init_db()
|
| 22 |
+
|
| 23 |
+
# --- Functions ---
|
| 24 |
def signup(username, password):
|
| 25 |
+
if not username or not password:
|
| 26 |
+
return "β οΈ Please enter both username and password."
|
| 27 |
+
conn = sqlite3.connect(DB_PATH)
|
| 28 |
cursor = conn.cursor()
|
| 29 |
try:
|
| 30 |
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
| 31 |
conn.commit()
|
| 32 |
conn.close()
|
| 33 |
+
return "β
Signup successful! Please login."
|
| 34 |
except sqlite3.IntegrityError:
|
| 35 |
conn.close()
|
| 36 |
+
return "β Username already exists. Try another."
|
| 37 |
|
|
|
|
| 38 |
def login(username, password):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
if username == "1234" and password == "1234":
|
| 40 |
+
return "β
Admin logged in! You can download the database.", "Admin"
|
| 41 |
+
|
| 42 |
+
conn = sqlite3.connect(DB_PATH)
|
| 43 |
+
cursor = conn.cursor()
|
| 44 |
cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 45 |
user = cursor.fetchone()
|
| 46 |
conn.close()
|
| 47 |
+
|
| 48 |
if user:
|
| 49 |
+
return f"β
Welcome, {username}!", username
|
| 50 |
+
else:
|
| 51 |
+
return "β Invalid credentials.", None
|
| 52 |
|
|
|
|
| 53 |
def logout():
|
| 54 |
+
return "β
Logged out successfully!", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
def download_db():
|
|
|
|
| 57 |
if os.path.exists(DB_PATH):
|
| 58 |
return DB_PATH
|
| 59 |
return None
|
| 60 |
|
| 61 |
+
# --- Gradio UI ---
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("## π Simple Login System with Signup & DB Download")
|
| 64 |
+
|
| 65 |
+
with gr.Tab("Signup"):
|
| 66 |
+
signup_user = gr.Textbox(label="Username")
|
| 67 |
+
signup_pass = gr.Textbox(label="Password", type="password")
|
| 68 |
+
signup_btn = gr.Button("Signup")
|
| 69 |
+
signup_out = gr.Textbox(label="Signup Status", interactive=False)
|
| 70 |
+
|
| 71 |
+
signup_btn.click(signup, inputs=[signup_user, signup_pass], outputs=signup_out)
|
| 72 |
+
|
| 73 |
+
with gr.Tab("Login"):
|
| 74 |
+
login_user = gr.Textbox(label="Username")
|
| 75 |
+
login_pass = gr.Textbox(label="Password", type="password")
|
| 76 |
+
login_btn = gr.Button("Login")
|
| 77 |
+
login_status = gr.Textbox(label="Login Status", interactive=False)
|
| 78 |
+
current_user = gr.State() # store logged-in user
|
| 79 |
+
|
| 80 |
+
login_btn.click(login, inputs=[login_user, login_pass], outputs=[login_status, current_user])
|
| 81 |
+
|
| 82 |
+
with gr.Tab("Home"):
|
| 83 |
+
home_status = gr.Textbox(label="Home Status", interactive=False)
|
| 84 |
+
logout_btn = gr.Button("Logout")
|
| 85 |
+
download_btn = gr.File(label="Download DB", interactive=False)
|
| 86 |
+
|
| 87 |
+
# logout clears user
|
| 88 |
+
logout_btn.click(logout, outputs=[home_status, current_user])
|
| 89 |
+
|
| 90 |
+
# allow admin to download DB
|
| 91 |
+
login_btn.click(fn=lambda u: "Welcome Home!" if u else "β Please login first.",
|
| 92 |
+
inputs=current_user, outputs=home_status)
|
| 93 |
+
|
| 94 |
+
login_btn.click(fn=lambda u: download_db() if u == "Admin" else None,
|
| 95 |
+
inputs=current_user, outputs=download_btn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
demo.launch()
|