Spaces:
Running
Running
Update dashboard.py
Browse files- dashboard.py +17 -12
dashboard.py
CHANGED
@@ -14,14 +14,16 @@ 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 |
-
# Setup SQLite
|
21 |
-
|
|
|
|
|
22 |
cursor = conn.cursor()
|
23 |
|
24 |
-
# Create user_details table
|
25 |
cursor.execute('''
|
26 |
CREATE TABLE IF NOT EXISTS user_details (
|
27 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
@@ -29,30 +31,32 @@ CREATE TABLE IF NOT EXISTS user_details (
|
|
29 |
PHONE TEXT,
|
30 |
EMAIL TEXT UNIQUE,
|
31 |
GENDER TEXT,
|
32 |
-
PASSWORD
|
33 |
)
|
34 |
''')
|
35 |
conn.commit()
|
36 |
|
37 |
-
# Validation
|
38 |
def is_valid_email(email):
|
39 |
return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
40 |
|
41 |
def is_valid_phone(phone):
|
42 |
return re.match(r"^[0-9]{10}$", phone)
|
43 |
|
|
|
44 |
def preprocess_image(image):
|
45 |
image = np.array(image)
|
46 |
image = cv2.resize(image, (128, 128))
|
47 |
image = image.astype(np.float32) / 255.0
|
48 |
return np.expand_dims(image, axis=0)
|
49 |
|
|
|
50 |
def predict_image(image):
|
51 |
preprocessed = preprocess_image(image)
|
52 |
prediction = deepfake_model.predict(preprocessed)[0][0]
|
53 |
return "β
Real Image" if prediction >= 0.5 else "β οΈ Fake Image"
|
54 |
|
55 |
-
# Register user
|
56 |
def register_user(name, phone, email, password):
|
57 |
if not is_valid_email(email):
|
58 |
return "β Invalid email", False
|
@@ -67,17 +71,18 @@ def register_user(name, phone, email, password):
|
|
67 |
cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
|
68 |
(name, phone, email, "U", hashed_pw))
|
69 |
conn.commit()
|
|
|
70 |
return "β
Registration successful! Please log in.", True
|
71 |
|
72 |
# Login user
|
73 |
def login_user(email, password):
|
74 |
cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
|
75 |
result = cursor.fetchone()
|
76 |
-
if result and bcrypt.checkpw(password.encode(), result[0]
|
77 |
return "β
Login successful!", True
|
78 |
return "β Invalid credentials", False
|
79 |
|
80 |
-
#
|
81 |
with gr.Blocks() as demo:
|
82 |
session = gr.State({})
|
83 |
show_login = gr.State(True)
|
@@ -108,16 +113,16 @@ with gr.Blocks() as demo:
|
|
108 |
|
109 |
def handle_signup(n, ph, e, p):
|
110 |
msg, ok = register_user(n, ph, e, p)
|
111 |
-
return msg
|
112 |
|
113 |
def handle_logout():
|
114 |
return {}, gr.update(visible=True), gr.update(visible=False)
|
115 |
|
116 |
login_btn.click(handle_login, [email, password], [status, login_panel, prediction_panel])
|
117 |
-
signup_btn.click(handle_signup, [name, phone, email, password], status)
|
118 |
predict_btn.click(predict_image, inputs=image_input, outputs=result)
|
119 |
logout_btn.click(handle_logout, outputs=[session, login_panel, prediction_panel])
|
120 |
|
121 |
-
# Launch
|
122 |
if __name__ == "__main__":
|
123 |
demo.launch()
|
|
|
14 |
warnings.filterwarnings("ignore")
|
15 |
np.seterr(all='ignore')
|
16 |
|
17 |
+
# Load TensorFlow model
|
18 |
deepfake_model = tf.keras.models.load_model("model_15_64.h5")
|
19 |
|
20 |
+
# Setup SQLite connection
|
21 |
+
db_path = os.path.abspath("users.db")
|
22 |
+
print(f"β
Using database at: {db_path}")
|
23 |
+
conn = sqlite3.connect(db_path, check_same_thread=False)
|
24 |
cursor = conn.cursor()
|
25 |
|
26 |
+
# Create user_details table if not exists
|
27 |
cursor.execute('''
|
28 |
CREATE TABLE IF NOT EXISTS user_details (
|
29 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
31 |
PHONE TEXT,
|
32 |
EMAIL TEXT UNIQUE,
|
33 |
GENDER TEXT,
|
34 |
+
PASSWORD BLOB
|
35 |
)
|
36 |
''')
|
37 |
conn.commit()
|
38 |
|
39 |
+
# Validation functions
|
40 |
def is_valid_email(email):
|
41 |
return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
42 |
|
43 |
def is_valid_phone(phone):
|
44 |
return re.match(r"^[0-9]{10}$", phone)
|
45 |
|
46 |
+
# Image preprocessing
|
47 |
def preprocess_image(image):
|
48 |
image = np.array(image)
|
49 |
image = cv2.resize(image, (128, 128))
|
50 |
image = image.astype(np.float32) / 255.0
|
51 |
return np.expand_dims(image, axis=0)
|
52 |
|
53 |
+
# Deepfake prediction
|
54 |
def predict_image(image):
|
55 |
preprocessed = preprocess_image(image)
|
56 |
prediction = deepfake_model.predict(preprocessed)[0][0]
|
57 |
return "β
Real Image" if prediction >= 0.5 else "β οΈ Fake Image"
|
58 |
|
59 |
+
# Register new user
|
60 |
def register_user(name, phone, email, password):
|
61 |
if not is_valid_email(email):
|
62 |
return "β Invalid email", False
|
|
|
71 |
cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
|
72 |
(name, phone, email, "U", hashed_pw))
|
73 |
conn.commit()
|
74 |
+
print(f"β
Registered new user: {email}")
|
75 |
return "β
Registration successful! Please log in.", True
|
76 |
|
77 |
# Login user
|
78 |
def login_user(email, password):
|
79 |
cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
|
80 |
result = cursor.fetchone()
|
81 |
+
if result and bcrypt.checkpw(password.encode(), result[0] if isinstance(result[0], bytes) else result[0].encode()):
|
82 |
return "β
Login successful!", True
|
83 |
return "β Invalid credentials", False
|
84 |
|
85 |
+
# Gradio Interface
|
86 |
with gr.Blocks() as demo:
|
87 |
session = gr.State({})
|
88 |
show_login = gr.State(True)
|
|
|
113 |
|
114 |
def handle_signup(n, ph, e, p):
|
115 |
msg, ok = register_user(n, ph, e, p)
|
116 |
+
return msg, gr.update(visible=not ok), gr.update(visible=ok)
|
117 |
|
118 |
def handle_logout():
|
119 |
return {}, gr.update(visible=True), gr.update(visible=False)
|
120 |
|
121 |
login_btn.click(handle_login, [email, password], [status, login_panel, prediction_panel])
|
122 |
+
signup_btn.click(handle_signup, [name, phone, email, password], [status, login_panel, prediction_panel])
|
123 |
predict_btn.click(predict_image, inputs=image_input, outputs=result)
|
124 |
logout_btn.click(handle_logout, outputs=[session, login_panel, prediction_panel])
|
125 |
|
126 |
+
# Launch app
|
127 |
if __name__ == "__main__":
|
128 |
demo.launch()
|