Spaces:
Running
Running
Delete pages/dashboard.py
Browse files- pages/dashboard.py +0 -124
pages/dashboard.py
DELETED
@@ -1,124 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import sqlite3
|
3 |
-
import re
|
4 |
-
import bcrypt
|
5 |
-
import numpy as np
|
6 |
-
import cv2
|
7 |
-
from PIL import Image
|
8 |
-
import tensorflow as tf
|
9 |
-
import os
|
10 |
-
import warnings
|
11 |
-
|
12 |
-
# Suppress all warnings
|
13 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
14 |
-
warnings.filterwarnings("ignore")
|
15 |
-
np.seterr(all='ignore')
|
16 |
-
|
17 |
-
# Load model
|
18 |
-
deepfake_model = tf.keras.models.load_model("model_15_64.h5")
|
19 |
-
|
20 |
-
|
21 |
-
# Setup SQLite instead of MySQL
|
22 |
-
conn = sqlite3.connect("users.db", check_same_thread=False)
|
23 |
-
cursor = conn.cursor()
|
24 |
-
|
25 |
-
# Create user_details table in SQLite
|
26 |
-
cursor.execute('''
|
27 |
-
CREATE TABLE IF NOT EXISTS user_details (
|
28 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
29 |
-
NAME TEXT,
|
30 |
-
PHONE TEXT,
|
31 |
-
EMAIL TEXT UNIQUE,
|
32 |
-
GENDER TEXT,
|
33 |
-
PASSWORD TEXT
|
34 |
-
)
|
35 |
-
''')
|
36 |
-
conn.commit()
|
37 |
-
|
38 |
-
# Validation utilities
|
39 |
-
def is_valid_email(email):
|
40 |
-
return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
41 |
-
|
42 |
-
def is_valid_phone(phone):
|
43 |
-
return re.match(r"^[0-9]{10}$", phone)
|
44 |
-
|
45 |
-
def preprocess_image(image):
|
46 |
-
image = np.array(image)
|
47 |
-
image = cv2.resize(image, (128, 128))
|
48 |
-
image = image.astype(np.float32) / 255.0
|
49 |
-
return np.expand_dims(image, axis=0)
|
50 |
-
|
51 |
-
def predict_image(image):
|
52 |
-
preprocessed = preprocess_image(image)
|
53 |
-
prediction = deepfake_model.predict(preprocessed)[0][0]
|
54 |
-
return "β
Real Image" if prediction >= 0.5 else "β οΈ Fake Image"
|
55 |
-
|
56 |
-
# Register user
|
57 |
-
def register_user(name, phone, email, password):
|
58 |
-
if not is_valid_email(email):
|
59 |
-
return "β Invalid email", False
|
60 |
-
if not is_valid_phone(phone):
|
61 |
-
return "β Phone must be 10 digits", False
|
62 |
-
|
63 |
-
cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
|
64 |
-
if cursor.fetchone():
|
65 |
-
return "β οΈ Email already registered", False
|
66 |
-
|
67 |
-
hashed_pw = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
|
68 |
-
cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
|
69 |
-
(name, phone, email, "U", hashed_pw))
|
70 |
-
conn.commit()
|
71 |
-
return "β
Registration successful! Please log in.", True
|
72 |
-
|
73 |
-
# Login user
|
74 |
-
def login_user(email, password):
|
75 |
-
cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
|
76 |
-
result = cursor.fetchone()
|
77 |
-
if result and bcrypt.checkpw(password.encode(), result[0].encode() if isinstance(result[0], str) else result[0]):
|
78 |
-
return "β
Login successful!", True
|
79 |
-
return "β Invalid credentials", False
|
80 |
-
|
81 |
-
# App layout
|
82 |
-
with gr.Blocks() as demo:
|
83 |
-
session = gr.State({})
|
84 |
-
show_login = gr.State(True)
|
85 |
-
|
86 |
-
status = gr.Textbox(label="", interactive=False)
|
87 |
-
|
88 |
-
with gr.Column(visible=True) as login_panel:
|
89 |
-
gr.Markdown("### Login or Sign Up")
|
90 |
-
name = gr.Textbox(label="Name (Sign Up Only)")
|
91 |
-
phone = gr.Textbox(label="Phone (Sign Up Only)")
|
92 |
-
email = gr.Textbox(label="Email")
|
93 |
-
password = gr.Textbox(label="Password", type="password")
|
94 |
-
|
95 |
-
login_btn = gr.Button("Login")
|
96 |
-
signup_btn = gr.Button("Sign Up")
|
97 |
-
|
98 |
-
with gr.Column(visible=False) as prediction_panel:
|
99 |
-
gr.Markdown("## Upload Image for Deepfake Detection")
|
100 |
-
image_input = gr.Image(type="pil")
|
101 |
-
result = gr.Textbox(label="Result")
|
102 |
-
predict_btn = gr.Button("Predict")
|
103 |
-
logout_btn = gr.Button("Logout")
|
104 |
-
|
105 |
-
# Logic
|
106 |
-
def handle_login(e, p):
|
107 |
-
msg, ok = login_user(e, p)
|
108 |
-
return msg, gr.update(visible=not ok), gr.update(visible=ok)
|
109 |
-
|
110 |
-
def handle_signup(n, ph, e, p):
|
111 |
-
msg, ok = register_user(n, ph, e, p)
|
112 |
-
return msg
|
113 |
-
|
114 |
-
def handle_logout():
|
115 |
-
return {}, gr.update(visible=True), gr.update(visible=False)
|
116 |
-
|
117 |
-
login_btn.click(handle_login, [email, password], [status, login_panel, prediction_panel])
|
118 |
-
signup_btn.click(handle_signup, [name, phone, email, password], status)
|
119 |
-
predict_btn.click(predict_image, inputs=image_input, outputs=result)
|
120 |
-
logout_btn.click(handle_logout, outputs=[session, login_panel, prediction_panel])
|
121 |
-
|
122 |
-
# Launch
|
123 |
-
if __name__ == "__main__":
|
124 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|