Spaces:
Running
Running
Update dashboard.py
Browse files- dashboard.py +94 -119
dashboard.py
CHANGED
@@ -35,39 +35,33 @@ with warnings.catch_warnings():
|
|
35 |
|
36 |
# --- DB ---
|
37 |
DB_PATH = os.path.abspath("users.db")
|
38 |
-
|
39 |
-
cursor = conn.cursor()
|
40 |
-
cursor.execute('''
|
41 |
-
CREATE TABLE IF NOT EXISTS user_details (
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
)
|
49 |
-
''')
|
50 |
-
conn.commit()
|
51 |
-
conn.close()
|
52 |
|
53 |
# --- Helpers ---
|
54 |
-
def is_valid_email(email):
|
55 |
-
|
56 |
-
|
57 |
-
def is_valid_phone(phone):
|
58 |
-
return re.match(r"^[0-9]{10}$", phone)
|
59 |
|
60 |
def preprocess_image(image):
|
61 |
-
if image.mode != 'RGB':
|
62 |
-
image = image.convert('RGB')
|
63 |
image_arr = np.array(image)
|
64 |
image_arr = cv2.resize(image_arr, (128, 128))
|
65 |
image_arr = image_arr.astype(np.float32) / 255.0
|
66 |
return np.expand_dims(image_arr, axis=0)
|
67 |
|
68 |
def predict_image(image):
|
69 |
-
if image is None:
|
70 |
-
return "Please upload an image first."
|
71 |
preprocessed = preprocess_image(image)
|
72 |
prediction = deepfake_model.predict(preprocessed)[0][0]
|
73 |
confidence = prediction if prediction >= 0.5 else 1 - prediction
|
@@ -75,51 +69,34 @@ def predict_image(image):
|
|
75 |
return f"{label} (Confidence: {confidence:.2%})"
|
76 |
|
77 |
def register_user(name, phone, email, password):
|
78 |
-
if not all([name, phone, email, password]):
|
79 |
-
|
80 |
-
if not
|
81 |
-
return "β Invalid email format."
|
82 |
-
if not is_valid_phone(phone):
|
83 |
-
return "β Phone must be 10 digits."
|
84 |
-
|
85 |
try:
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
"INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
|
95 |
-
(name, phone, email, "U", hashed_pw)
|
96 |
-
)
|
97 |
-
conn.commit()
|
98 |
-
return "β
Registration successful! Please log in."
|
99 |
except sqlite3.Error as e:
|
100 |
print(f"Database error: {e}")
|
101 |
-
return "β A database error occurred.
|
102 |
-
finally:
|
103 |
-
if conn:
|
104 |
-
conn.close()
|
105 |
|
106 |
def login_user(email, password):
|
107 |
-
if not email or not password:
|
108 |
-
return False
|
109 |
try:
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
return
|
116 |
-
return False
|
117 |
except sqlite3.Error as e:
|
118 |
print(f"Database error during login: {e}")
|
119 |
return False
|
120 |
-
finally:
|
121 |
-
if conn:
|
122 |
-
conn.close()
|
123 |
|
124 |
# --- UI ---
|
125 |
with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
@@ -136,107 +113,105 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
136 |
with gr.Row():
|
137 |
with gr.Column(scale=1):
|
138 |
gr.Markdown("## Welcome!", "Login to access the detector, or sign up for a new account.")
|
139 |
-
gr.Markdown("Our tool uses advanced AI to analyze images and determine their authenticity, helping you combat misinformation.")
|
140 |
with gr.Column(scale=2):
|
141 |
gr.Markdown("### Login or Sign Up")
|
142 |
message_output = gr.Markdown(visible=False)
|
143 |
email_login = gr.Textbox(label="Email", elem_id="login_email")
|
144 |
password_login = gr.Textbox(label="Password", type="password", elem_id="login_pass")
|
145 |
login_btn = gr.Button("Login", variant="primary")
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
password_signup = gr.Textbox(label="Create Password", type="password")
|
153 |
-
signup_btn = gr.Button("Sign Up")
|
154 |
|
155 |
with gr.Tab(DETECT_TAB_NAME, visible=False) as detect_tab:
|
156 |
with gr.Row():
|
157 |
gr.Markdown("## Deepfake Detector")
|
158 |
logout_btn = gr.Button("Logout")
|
159 |
-
gr.Markdown("Upload an image to check if it's a real photograph or an AI-generated deepfake.")
|
160 |
with gr.Row():
|
161 |
-
|
162 |
-
|
163 |
-
predict_btn = gr.Button("Predict", variant="primary")
|
164 |
-
with gr.Column():
|
165 |
result = gr.Textbox(label="Prediction Result", interactive=False)
|
|
|
166 |
|
167 |
with gr.Tab(ABOUT_TAB_NAME): about.layout()
|
168 |
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
169 |
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
170 |
|
171 |
# --- Events ---
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
174 |
return (
|
175 |
-
gr.update(
|
176 |
-
True,
|
177 |
-
gr.update(
|
178 |
-
gr.update(visible=True)
|
179 |
-
gr.update(selected=DETECT_TAB_NAME) # switch to detect tab
|
180 |
)
|
181 |
else:
|
|
|
182 |
return (
|
183 |
-
gr.update(
|
184 |
-
False,
|
185 |
-
gr.update(),
|
186 |
-
|
|
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
|
|
189 |
def handle_signup(name, phone, email, password):
|
190 |
msg = register_user(name, phone, email, password)
|
191 |
if msg.startswith("β
"):
|
192 |
-
|
193 |
-
|
194 |
-
"", "", "", "", # clear inputs
|
195 |
-
gr.update(visible=False) # hide signup form
|
196 |
-
)
|
197 |
else:
|
198 |
-
|
199 |
-
|
200 |
-
name, phone, email, password,
|
201 |
-
gr.update(visible=True)
|
202 |
-
)
|
203 |
|
204 |
-
def handle_logout():
|
205 |
-
return (
|
206 |
-
False,
|
207 |
-
gr.update(visible=True), gr.update(visible=False),
|
208 |
-
gr.update(selected=LOGIN_TAB_NAME),
|
209 |
-
"", ""
|
210 |
-
)
|
211 |
|
|
|
|
|
|
|
212 |
login_btn.click(
|
213 |
fn=handle_login,
|
214 |
inputs=[email_login, password_login],
|
215 |
-
outputs=[
|
216 |
-
)
|
217 |
-
|
218 |
-
|
219 |
-
signup_btn.click(
|
220 |
-
fn=handle_signup,
|
221 |
-
inputs=[name_signup, phone_signup, email_signup, password_signup],
|
222 |
-
outputs=[
|
223 |
-
message_output,
|
224 |
-
name_signup, phone_signup, email_signup, password_signup,
|
225 |
-
signup_group
|
226 |
-
]
|
227 |
)
|
228 |
|
229 |
logout_btn.click(
|
230 |
fn=handle_logout,
|
231 |
inputs=[],
|
232 |
-
outputs=[is_logged_in,
|
233 |
)
|
234 |
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
239 |
)
|
240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
if __name__ == "__main__":
|
242 |
-
demo.launch()
|
|
|
35 |
|
36 |
# --- DB ---
|
37 |
DB_PATH = os.path.abspath("users.db")
|
38 |
+
with sqlite3.connect(DB_PATH) as conn:
|
39 |
+
cursor = conn.cursor()
|
40 |
+
cursor.execute('''
|
41 |
+
CREATE TABLE IF NOT EXISTS user_details (
|
42 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
43 |
+
NAME TEXT NOT NULL,
|
44 |
+
PHONE TEXT NOT NULL,
|
45 |
+
EMAIL TEXT UNIQUE NOT NULL,
|
46 |
+
GENDER TEXT,
|
47 |
+
PASSWORD BLOB NOT NULL
|
48 |
+
)
|
49 |
+
''')
|
50 |
+
conn.commit()
|
|
|
51 |
|
52 |
# --- Helpers ---
|
53 |
+
def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
54 |
+
def is_valid_phone(phone): return re.match(r"^[0-9]{10}$", phone)
|
|
|
|
|
|
|
55 |
|
56 |
def preprocess_image(image):
|
57 |
+
if image.mode != 'RGB': image = image.convert('RGB')
|
|
|
58 |
image_arr = np.array(image)
|
59 |
image_arr = cv2.resize(image_arr, (128, 128))
|
60 |
image_arr = image_arr.astype(np.float32) / 255.0
|
61 |
return np.expand_dims(image_arr, axis=0)
|
62 |
|
63 |
def predict_image(image):
|
64 |
+
if image is None: return "Please upload an image first."
|
|
|
65 |
preprocessed = preprocess_image(image)
|
66 |
prediction = deepfake_model.predict(preprocessed)[0][0]
|
67 |
confidence = prediction if prediction >= 0.5 else 1 - prediction
|
|
|
69 |
return f"{label} (Confidence: {confidence:.2%})"
|
70 |
|
71 |
def register_user(name, phone, email, password):
|
72 |
+
if not all([name, phone, email, password]): return "β All fields are required for signup."
|
73 |
+
if not is_valid_email(email): return "β Invalid email format."
|
74 |
+
if not is_valid_phone(phone): return "β Phone must be 10 digits."
|
|
|
|
|
|
|
|
|
75 |
try:
|
76 |
+
with sqlite3.connect(DB_PATH) as conn:
|
77 |
+
cursor = conn.cursor()
|
78 |
+
cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
|
79 |
+
if cursor.fetchone(): return "β οΈ Email already registered."
|
80 |
+
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
81 |
+
cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)", (name, phone, email, "U", hashed_pw))
|
82 |
+
conn.commit()
|
83 |
+
return "β
Registration successful! Please log in."
|
|
|
|
|
|
|
|
|
|
|
84 |
except sqlite3.Error as e:
|
85 |
print(f"Database error: {e}")
|
86 |
+
return "β A database error occurred."
|
|
|
|
|
|
|
87 |
|
88 |
def login_user(email, password):
|
89 |
+
if not email or not password: return False
|
|
|
90 |
try:
|
91 |
+
with sqlite3.connect(DB_PATH) as conn:
|
92 |
+
cursor = conn.cursor()
|
93 |
+
cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
|
94 |
+
result = cursor.fetchone()
|
95 |
+
if result and bcrypt.checkpw(password.encode('utf-8'), result[0]): return True
|
96 |
+
return False
|
|
|
97 |
except sqlite3.Error as e:
|
98 |
print(f"Database error during login: {e}")
|
99 |
return False
|
|
|
|
|
|
|
100 |
|
101 |
# --- UI ---
|
102 |
with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=1):
|
115 |
gr.Markdown("## Welcome!", "Login to access the detector, or sign up for a new account.")
|
|
|
116 |
with gr.Column(scale=2):
|
117 |
gr.Markdown("### Login or Sign Up")
|
118 |
message_output = gr.Markdown(visible=False)
|
119 |
email_login = gr.Textbox(label="Email", elem_id="login_email")
|
120 |
password_login = gr.Textbox(label="Password", type="password", elem_id="login_pass")
|
121 |
login_btn = gr.Button("Login", variant="primary")
|
122 |
+
with gr.Accordion("New User? Click here to Sign Up", open=False) as signup_accordion:
|
123 |
+
name_signup = gr.Textbox(label="Name")
|
124 |
+
phone_signup = gr.Textbox(label="Phone (10 digits)")
|
125 |
+
email_signup = gr.Textbox(label="Email")
|
126 |
+
password_signup = gr.Textbox(label="Create Password", type="password")
|
127 |
+
signup_btn = gr.Button("Sign Up")
|
|
|
|
|
128 |
|
129 |
with gr.Tab(DETECT_TAB_NAME, visible=False) as detect_tab:
|
130 |
with gr.Row():
|
131 |
gr.Markdown("## Deepfake Detector")
|
132 |
logout_btn = gr.Button("Logout")
|
|
|
133 |
with gr.Row():
|
134 |
+
image_input = gr.Image(type="pil", label="Upload Image", scale=1)
|
135 |
+
with gr.Column(scale=1):
|
|
|
|
|
136 |
result = gr.Textbox(label="Prediction Result", interactive=False)
|
137 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
138 |
|
139 |
with gr.Tab(ABOUT_TAB_NAME): about.layout()
|
140 |
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
141 |
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
142 |
|
143 |
# --- Events ---
|
144 |
+
|
145 |
+
# This function is now the single point for UI changes based on login state
|
146 |
+
def update_ui_on_auth_change(logged_in_status):
|
147 |
+
if logged_in_status:
|
148 |
+
# User has logged in
|
149 |
return (
|
150 |
+
gr.update(visible=False), # Hide login tab
|
151 |
+
gr.update(visible=True), # Show detect tab
|
152 |
+
gr.update(selected=DETECT_TAB_NAME), # Switch to detect tab
|
153 |
+
gr.update(value="β
Login successful!", visible=True)
|
|
|
154 |
)
|
155 |
else:
|
156 |
+
# User has logged out
|
157 |
return (
|
158 |
+
gr.update(visible=True), # Show login tab
|
159 |
+
gr.update(visible=False), # Hide detect tab
|
160 |
+
gr.update(selected=LOGIN_TAB_NAME), # Go back to login tab
|
161 |
+
gr.update(value="", visible=False)
|
162 |
+
)
|
163 |
|
164 |
+
# Login button now only updates the login state and provides a message
|
165 |
+
def handle_login(email, password):
|
166 |
+
if login_user(email, password):
|
167 |
+
return True, gr.update(value="β
Login successful!", visible=True)
|
168 |
+
else:
|
169 |
+
return False, gr.update(value="β Invalid email or password.", visible=True)
|
170 |
+
|
171 |
+
# Logout button only needs to set the state to False
|
172 |
+
def handle_logout():
|
173 |
+
return False, "", "" # logged_in=False, clear email/pass fields
|
174 |
|
175 |
+
# Signup remains mostly the same, but can clear inputs and close accordion
|
176 |
def handle_signup(name, phone, email, password):
|
177 |
msg = register_user(name, phone, email, password)
|
178 |
if msg.startswith("β
"):
|
179 |
+
# On success, clear inputs and close the accordion
|
180 |
+
return gr.update(value=msg, visible=True), "", "", "", "", gr.update(open=False)
|
|
|
|
|
|
|
181 |
else:
|
182 |
+
# On failure, show message but don't clear or close
|
183 |
+
return gr.update(value=msg, visible=True), name, phone, email, password, gr.update(open=True)
|
|
|
|
|
|
|
184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
+
# --- Event Listeners Wiring ---
|
187 |
+
|
188 |
+
# 1. Login/Logout buttons update the `is_logged_in` state.
|
189 |
login_btn.click(
|
190 |
fn=handle_login,
|
191 |
inputs=[email_login, password_login],
|
192 |
+
outputs=[is_logged_in, message_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
)
|
194 |
|
195 |
logout_btn.click(
|
196 |
fn=handle_logout,
|
197 |
inputs=[],
|
198 |
+
outputs=[is_logged_in, email_login, password_login]
|
199 |
)
|
200 |
|
201 |
+
# 2. A change in `is_logged_in` triggers the main UI update. THIS IS THE KEY.
|
202 |
+
is_logged_in.change(
|
203 |
+
fn=update_ui_on_auth_change,
|
204 |
+
inputs=is_logged_in,
|
205 |
+
outputs=[login_tab, detect_tab, tabs, message_output]
|
206 |
)
|
207 |
|
208 |
+
signup_btn.click(
|
209 |
+
fn=handle_signup,
|
210 |
+
inputs=[name_signup, phone_signup, email_signup, password_signup],
|
211 |
+
outputs=[message_output, name_signup, phone_signup, email_signup, password_signup, signup_accordion]
|
212 |
+
)
|
213 |
+
|
214 |
+
predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
|
215 |
+
|
216 |
if __name__ == "__main__":
|
217 |
+
demo.launch()
|