Spaces:
Sleeping
Sleeping
Update dashboard.py
Browse files- dashboard.py +31 -42
dashboard.py
CHANGED
@@ -12,16 +12,11 @@ import warnings
|
|
12 |
|
13 |
from pages import about, community, user_guide
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
# --- Initial Setup ---
|
18 |
-
|
19 |
-
# Suppress warnings and TensorFlow logs
|
20 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Changed to 2 to hide some informational logs
|
21 |
warnings.filterwarnings("ignore")
|
22 |
np.seterr(all='ignore')
|
23 |
|
24 |
-
# Load model (or create a dummy one if not found)
|
25 |
MODEL_PATH = "model_15_64.h5"
|
26 |
if not os.path.exists(MODEL_PATH):
|
27 |
print(f"Model file '{MODEL_PATH}' not found. Creating a dummy model for testing.")
|
@@ -30,18 +25,15 @@ if not os.path.exists(MODEL_PATH):
|
|
30 |
tf.keras.layers.Flatten(),
|
31 |
tf.keras.layers.Dense(1, activation='sigmoid')
|
32 |
])
|
33 |
-
# Suppress the warning during saving
|
34 |
with warnings.catch_warnings():
|
35 |
warnings.simplefilter("ignore")
|
36 |
dummy_model.save(MODEL_PATH)
|
37 |
|
38 |
-
# Suppress the "Compiled the loaded model" warning from Keras
|
39 |
with warnings.catch_warnings():
|
40 |
warnings.simplefilter("ignore")
|
41 |
deepfake_model = tf.keras.models.load_model(MODEL_PATH)
|
42 |
|
43 |
-
|
44 |
-
# Database setup
|
45 |
DB_PATH = os.path.abspath("users.db")
|
46 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
47 |
cursor = conn.cursor()
|
@@ -58,16 +50,13 @@ CREATE TABLE IF NOT EXISTS user_details (
|
|
58 |
conn.commit()
|
59 |
conn.close()
|
60 |
|
61 |
-
# ---
|
62 |
-
|
63 |
-
# Validators
|
64 |
def is_valid_email(email):
|
65 |
return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
66 |
|
67 |
def is_valid_phone(phone):
|
68 |
return re.match(r"^[0-9]{10}$", phone)
|
69 |
|
70 |
-
# Image preprocessing
|
71 |
def preprocess_image(image):
|
72 |
if image.mode != 'RGB':
|
73 |
image = image.convert('RGB')
|
@@ -85,7 +74,6 @@ def predict_image(image):
|
|
85 |
label = "✅ Real Image" if prediction >= 0.5 else "⚠️ Fake Image"
|
86 |
return f"{label} (Confidence: {confidence:.2%})"
|
87 |
|
88 |
-
# Auth logic
|
89 |
def register_user(name, phone, email, password):
|
90 |
if not all([name, phone, email, password]):
|
91 |
return "❌ All fields are required for signup."
|
@@ -133,7 +121,7 @@ def login_user(email, password):
|
|
133 |
if conn:
|
134 |
conn.close()
|
135 |
|
136 |
-
# ---
|
137 |
with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
138 |
is_logged_in = gr.State(False)
|
139 |
|
@@ -145,8 +133,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
145 |
|
146 |
with gr.Tabs(selected=LOGIN_TAB_NAME) as tabs:
|
147 |
with gr.Tab(LOGIN_TAB_NAME) as login_tab:
|
148 |
-
# --- THIS IS THE FIX ---
|
149 |
-
# The .style() method was removed. The row will work correctly without it.
|
150 |
with gr.Row():
|
151 |
with gr.Column(scale=1):
|
152 |
gr.Markdown("## Welcome!", "Login to access the detector, or sign up for a new account.")
|
@@ -157,13 +143,14 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
157 |
email_login = gr.Textbox(label="Email", elem_id="login_email")
|
158 |
password_login = gr.Textbox(label="Password", type="password", elem_id="login_pass")
|
159 |
login_btn = gr.Button("Login", variant="primary")
|
160 |
-
|
161 |
with gr.Accordion("New User? Click here to Sign Up", open=False):
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
167 |
|
168 |
with gr.Tab(DETECT_TAB_NAME, visible=False) as detect_tab:
|
169 |
with gr.Row():
|
@@ -181,7 +168,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
181 |
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
182 |
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
183 |
|
184 |
-
# ---
|
185 |
def handle_login(email, password):
|
186 |
if login_user(email, password):
|
187 |
return (
|
@@ -200,44 +187,46 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
200 |
if msg.startswith("✅"):
|
201 |
return (
|
202 |
gr.update(value=msg, visible=True),
|
203 |
-
"", "", "", "", # clear
|
204 |
-
gr.update(
|
205 |
)
|
206 |
else:
|
207 |
return (
|
208 |
gr.update(value=msg, visible=True),
|
209 |
-
name, phone, email, password,
|
210 |
-
gr.update(
|
211 |
)
|
212 |
|
213 |
def handle_logout():
|
214 |
return (
|
215 |
-
False,
|
216 |
-
gr.update(
|
|
|
|
|
217 |
)
|
218 |
|
219 |
-
# --- Event Listeners ---
|
220 |
login_btn.click(
|
221 |
fn=handle_login,
|
222 |
inputs=[email_login, password_login],
|
223 |
outputs=[message_output, is_logged_in, login_tab, detect_tab, tabs]
|
224 |
)
|
225 |
-
|
226 |
signup_btn.click(
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
233 |
)
|
234 |
|
235 |
-
|
236 |
logout_btn.click(
|
237 |
fn=handle_logout,
|
238 |
inputs=[],
|
239 |
outputs=[is_logged_in, login_tab, detect_tab, tabs, email_login, password_login]
|
240 |
)
|
|
|
241 |
predict_btn.click(
|
242 |
fn=predict_image,
|
243 |
inputs=image_input,
|
@@ -245,4 +234,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
|
245 |
)
|
246 |
|
247 |
if __name__ == "__main__":
|
248 |
-
demo.launch()
|
|
|
12 |
|
13 |
from pages import about, community, user_guide
|
14 |
|
15 |
+
# --- Setup ---
|
16 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
|
|
|
|
|
|
|
|
17 |
warnings.filterwarnings("ignore")
|
18 |
np.seterr(all='ignore')
|
19 |
|
|
|
20 |
MODEL_PATH = "model_15_64.h5"
|
21 |
if not os.path.exists(MODEL_PATH):
|
22 |
print(f"Model file '{MODEL_PATH}' not found. Creating a dummy model for testing.")
|
|
|
25 |
tf.keras.layers.Flatten(),
|
26 |
tf.keras.layers.Dense(1, activation='sigmoid')
|
27 |
])
|
|
|
28 |
with warnings.catch_warnings():
|
29 |
warnings.simplefilter("ignore")
|
30 |
dummy_model.save(MODEL_PATH)
|
31 |
|
|
|
32 |
with warnings.catch_warnings():
|
33 |
warnings.simplefilter("ignore")
|
34 |
deepfake_model = tf.keras.models.load_model(MODEL_PATH)
|
35 |
|
36 |
+
# --- DB ---
|
|
|
37 |
DB_PATH = os.path.abspath("users.db")
|
38 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
39 |
cursor = conn.cursor()
|
|
|
50 |
conn.commit()
|
51 |
conn.close()
|
52 |
|
53 |
+
# --- Helpers ---
|
|
|
|
|
54 |
def is_valid_email(email):
|
55 |
return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
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')
|
|
|
74 |
label = "✅ Real Image" if prediction >= 0.5 else "⚠️ Fake 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 |
return "❌ All fields are required for signup."
|
|
|
121 |
if conn:
|
122 |
conn.close()
|
123 |
|
124 |
+
# --- UI ---
|
125 |
with gr.Blocks(theme=gr.themes.Soft(), title="Deepfake Detector") as demo:
|
126 |
is_logged_in = gr.State(False)
|
127 |
|
|
|
133 |
|
134 |
with gr.Tabs(selected=LOGIN_TAB_NAME) as tabs:
|
135 |
with gr.Tab(LOGIN_TAB_NAME) as login_tab:
|
|
|
|
|
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.")
|
|
|
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 |
with gr.Accordion("New User? Click here to Sign Up", open=False):
|
148 |
+
with gr.Group(visible=True) as signup_group:
|
149 |
+
name_signup = gr.Textbox(label="Name")
|
150 |
+
phone_signup = gr.Textbox(label="Phone (10 digits)")
|
151 |
+
email_signup = gr.Textbox(label="Email")
|
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():
|
|
|
168 |
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
169 |
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
170 |
|
171 |
+
# --- Events ---
|
172 |
def handle_login(email, password):
|
173 |
if login_user(email, password):
|
174 |
return (
|
|
|
187 |
if msg.startswith("✅"):
|
188 |
return (
|
189 |
gr.update(value=msg, visible=True),
|
190 |
+
"", "", "", "", # clear inputs
|
191 |
+
gr.update(visible=False) # hide signup form
|
192 |
)
|
193 |
else:
|
194 |
return (
|
195 |
gr.update(value=msg, visible=True),
|
196 |
+
name, phone, email, password,
|
197 |
+
gr.update(visible=True)
|
198 |
)
|
199 |
|
200 |
def handle_logout():
|
201 |
return (
|
202 |
+
False,
|
203 |
+
gr.update(visible=True), gr.update(visible=False),
|
204 |
+
gr.update(selected=LOGIN_TAB_NAME),
|
205 |
+
"", ""
|
206 |
)
|
207 |
|
|
|
208 |
login_btn.click(
|
209 |
fn=handle_login,
|
210 |
inputs=[email_login, password_login],
|
211 |
outputs=[message_output, is_logged_in, login_tab, detect_tab, tabs]
|
212 |
)
|
213 |
+
|
214 |
signup_btn.click(
|
215 |
+
fn=handle_signup,
|
216 |
+
inputs=[name_signup, phone_signup, email_signup, password_signup],
|
217 |
+
outputs=[
|
218 |
+
message_output,
|
219 |
+
name_signup, phone_signup, email_signup, password_signup,
|
220 |
+
signup_group
|
221 |
+
]
|
222 |
)
|
223 |
|
|
|
224 |
logout_btn.click(
|
225 |
fn=handle_logout,
|
226 |
inputs=[],
|
227 |
outputs=[is_logged_in, login_tab, detect_tab, tabs, email_login, password_login]
|
228 |
)
|
229 |
+
|
230 |
predict_btn.click(
|
231 |
fn=predict_image,
|
232 |
inputs=image_input,
|
|
|
234 |
)
|
235 |
|
236 |
if __name__ == "__main__":
|
237 |
+
demo.launch()
|