Spaces:
Sleeping
Sleeping
Update dashboard.py
Browse files- dashboard.py +43 -67
dashboard.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
import sqlite3
|
3 |
import re
|
4 |
import bcrypt
|
5 |
import numpy as np
|
@@ -8,9 +7,22 @@ from PIL import Image
|
|
8 |
import tensorflow as tf
|
9 |
import os
|
10 |
import warnings
|
|
|
|
|
11 |
|
12 |
from pages import about, community, user_guide
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# --- Setup ---
|
15 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
16 |
warnings.filterwarnings("ignore")
|
@@ -32,22 +44,6 @@ with warnings.catch_warnings():
|
|
32 |
warnings.simplefilter("ignore")
|
33 |
deepfake_model = tf.keras.models.load_model(MODEL_PATH)
|
34 |
|
35 |
-
# --- DB (UPDATED) ---
|
36 |
-
AUTH_DB_PATH = os.path.abspath("auth_users.db")
|
37 |
-
with sqlite3.connect(AUTH_DB_PATH) as conn:
|
38 |
-
cursor = conn.cursor()
|
39 |
-
cursor.execute('''
|
40 |
-
CREATE TABLE IF NOT EXISTS auth_user_details (
|
41 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
42 |
-
NAME TEXT NOT NULL,
|
43 |
-
PHONE TEXT NOT NULL,
|
44 |
-
EMAIL TEXT UNIQUE NOT NULL,
|
45 |
-
GENDER TEXT,
|
46 |
-
PASSWORD BLOB NOT NULL
|
47 |
-
)
|
48 |
-
''')
|
49 |
-
conn.commit()
|
50 |
-
|
51 |
# --- Helpers ---
|
52 |
def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
53 |
def is_valid_phone(phone): return re.match(r"^[0-9]{10}$", phone)
|
@@ -71,31 +67,31 @@ def register_user(name, phone, email, password):
|
|
71 |
if not all([name, phone, email, password]): return "β All fields are required for signup."
|
72 |
if not is_valid_email(email): return "β Invalid email format."
|
73 |
if not is_valid_phone(phone): return "β Phone must be 10 digits."
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
def login_user(email, password):
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
if result and bcrypt.checkpw(password.encode('utf-8'), result[0]): return True
|
95 |
-
return False
|
96 |
-
except sqlite3.Error as e:
|
97 |
-
print(f"Database error during login: {e}")
|
98 |
-
return False
|
99 |
|
100 |
# --- UI ---
|
101 |
with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
@@ -115,8 +111,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
115 |
with gr.Column(scale=2):
|
116 |
gr.Markdown("### Login or Sign Up")
|
117 |
message_output = gr.Markdown(visible=False)
|
118 |
-
email_login = gr.Textbox(label="Email"
|
119 |
-
password_login = gr.Textbox(label="Password", type="password"
|
120 |
login_btn = gr.Button("Login", variant="primary")
|
121 |
with gr.Accordion("New User? Click here to Sign Up", open=False) as signup_accordion:
|
122 |
name_signup = gr.Textbox(label="Name")
|
@@ -139,7 +135,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
139 |
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
140 |
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
141 |
|
142 |
-
# --- Events ---
|
143 |
def update_ui_on_auth_change(logged_in_status):
|
144 |
if logged_in_status:
|
145 |
return (
|
@@ -172,30 +167,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
172 |
else:
|
173 |
return gr.update(value=msg, visible=True), name, phone, email, password, gr.update(open=True)
|
174 |
|
175 |
-
login_btn.click(
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
logout_btn.click(
|
182 |
-
fn=handle_logout,
|
183 |
-
inputs=[],
|
184 |
-
outputs=[is_logged_in, email_login, password_login]
|
185 |
-
)
|
186 |
-
|
187 |
-
is_logged_in.change(
|
188 |
-
fn=update_ui_on_auth_change,
|
189 |
-
inputs=is_logged_in,
|
190 |
-
outputs=[login_tab, detect_tab, tabs, message_output]
|
191 |
-
)
|
192 |
-
|
193 |
-
signup_btn.click(
|
194 |
-
fn=handle_signup,
|
195 |
-
inputs=[name_signup, phone_signup, email_signup, password_signup],
|
196 |
-
outputs=[message_output, name_signup, phone_signup, email_signup, password_signup, signup_accordion]
|
197 |
-
)
|
198 |
-
|
199 |
predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
|
200 |
|
201 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import re
|
3 |
import bcrypt
|
4 |
import numpy as np
|
|
|
7 |
import tensorflow as tf
|
8 |
import os
|
9 |
import warnings
|
10 |
+
import requests
|
11 |
+
import json
|
12 |
|
13 |
from pages import about, community, user_guide
|
14 |
|
15 |
+
# --- Config ---
|
16 |
+
SUPABASE_URL = "https://fpbuhzbdtzwomjwytqul.supabase.co"
|
17 |
+
SUPABASE_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZwYnVoemJkdHp3b21qd3l0cXVsIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTE5NDk3NzYsImV4cCI6MjA2NzUyNTc3Nn0.oAa2TNNPQMyOGk63AOMZ7XKcwYvy5m-xoSWyvMZd6FY"
|
18 |
+
SUPABASE_TABLE = "user_details"
|
19 |
+
|
20 |
+
headers = {
|
21 |
+
"apikey": SUPABASE_API_KEY,
|
22 |
+
"Authorization": f"Bearer {SUPABASE_API_KEY}",
|
23 |
+
"Content-Type": "application/json"
|
24 |
+
}
|
25 |
+
|
26 |
# --- Setup ---
|
27 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
28 |
warnings.filterwarnings("ignore")
|
|
|
44 |
warnings.simplefilter("ignore")
|
45 |
deepfake_model = tf.keras.models.load_model(MODEL_PATH)
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# --- Helpers ---
|
48 |
def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
49 |
def is_valid_phone(phone): return re.match(r"^[0-9]{10}$", phone)
|
|
|
67 |
if not all([name, phone, email, password]): return "β All fields are required for signup."
|
68 |
if not is_valid_email(email): return "β Invalid email format."
|
69 |
if not is_valid_phone(phone): return "β Phone must be 10 digits."
|
70 |
+
|
71 |
+
# Check if user exists
|
72 |
+
query_url = f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}?email=eq.{email}"
|
73 |
+
r = requests.get(query_url, headers=headers)
|
74 |
+
if r.status_code == 200 and len(r.json()) > 0:
|
75 |
+
return "β οΈ Email already registered."
|
76 |
+
|
77 |
+
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode()
|
78 |
+
data = {
|
79 |
+
"name": name,
|
80 |
+
"phone": phone,
|
81 |
+
"email": email,
|
82 |
+
"gender": "U",
|
83 |
+
"password": hashed_pw
|
84 |
+
}
|
85 |
+
r = requests.post(f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}", headers=headers, data=json.dumps(data))
|
86 |
+
return "β
Registration successful! Please log in." if r.status_code == 201 else "β Error during registration."
|
87 |
|
88 |
def login_user(email, password):
|
89 |
+
url = f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}?email=eq.{email}"
|
90 |
+
r = requests.get(url, headers=headers)
|
91 |
+
if r.status_code == 200 and r.json():
|
92 |
+
stored_hash = r.json()[0]["password"]
|
93 |
+
return bcrypt.checkpw(password.encode(), stored_hash.encode())
|
94 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
# --- UI ---
|
97 |
with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
|
111 |
with gr.Column(scale=2):
|
112 |
gr.Markdown("### Login or Sign Up")
|
113 |
message_output = gr.Markdown(visible=False)
|
114 |
+
email_login = gr.Textbox(label="Email")
|
115 |
+
password_login = gr.Textbox(label="Password", type="password")
|
116 |
login_btn = gr.Button("Login", variant="primary")
|
117 |
with gr.Accordion("New User? Click here to Sign Up", open=False) as signup_accordion:
|
118 |
name_signup = gr.Textbox(label="Name")
|
|
|
135 |
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
136 |
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
137 |
|
|
|
138 |
def update_ui_on_auth_change(logged_in_status):
|
139 |
if logged_in_status:
|
140 |
return (
|
|
|
167 |
else:
|
168 |
return gr.update(value=msg, visible=True), name, phone, email, password, gr.update(open=True)
|
169 |
|
170 |
+
login_btn.click(fn=handle_login, inputs=[email_login, password_login], outputs=[is_logged_in, message_output])
|
171 |
+
logout_btn.click(fn=handle_logout, inputs=[], outputs=[is_logged_in, email_login, password_login])
|
172 |
+
is_logged_in.change(fn=update_ui_on_auth_change, inputs=is_logged_in, outputs=[login_tab, detect_tab, tabs, message_output])
|
173 |
+
signup_btn.click(fn=handle_signup, inputs=[name_signup, phone_signup, email_signup, password_signup],
|
174 |
+
outputs=[message_output, name_signup, phone_signup, email_signup, password_signup, signup_accordion])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
|
176 |
|
177 |
if __name__ == "__main__":
|