Spaces:
Running
Running
Added dashboard and requirements
Browse files- dashboard.py +123 -0
- requirements.txt +6 -0
dashboard.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 (1).h5")
|
19 |
+
|
20 |
+
# Setup SQLite instead of MySQL
|
21 |
+
conn = sqlite3.connect("users.db", check_same_thread=False)
|
22 |
+
cursor = conn.cursor()
|
23 |
+
|
24 |
+
# Create user_details table in SQLite
|
25 |
+
cursor.execute('''
|
26 |
+
CREATE TABLE IF NOT EXISTS user_details (
|
27 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
28 |
+
NAME TEXT,
|
29 |
+
PHONE TEXT,
|
30 |
+
EMAIL TEXT UNIQUE,
|
31 |
+
GENDER TEXT,
|
32 |
+
PASSWORD TEXT
|
33 |
+
)
|
34 |
+
''')
|
35 |
+
conn.commit()
|
36 |
+
|
37 |
+
# Validation utilities
|
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
|
59 |
+
if not is_valid_phone(phone):
|
60 |
+
return "β Phone must be 10 digits", False
|
61 |
+
|
62 |
+
cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
|
63 |
+
if cursor.fetchone():
|
64 |
+
return "β οΈ Email already registered", False
|
65 |
+
|
66 |
+
hashed_pw = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
|
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].encode() if isinstance(result[0], str) else result[0]):
|
77 |
+
return "β
Login successful!", True
|
78 |
+
return "β Invalid credentials", False
|
79 |
+
|
80 |
+
# App layout
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
session = gr.State({})
|
83 |
+
show_login = gr.State(True)
|
84 |
+
|
85 |
+
status = gr.Textbox(label="", interactive=False)
|
86 |
+
|
87 |
+
with gr.Column(visible=True) as login_panel:
|
88 |
+
gr.Markdown("### Login or Sign Up")
|
89 |
+
name = gr.Textbox(label="Name (Sign Up Only)")
|
90 |
+
phone = gr.Textbox(label="Phone (Sign Up Only)")
|
91 |
+
email = gr.Textbox(label="Email")
|
92 |
+
password = gr.Textbox(label="Password", type="password")
|
93 |
+
|
94 |
+
login_btn = gr.Button("Login")
|
95 |
+
signup_btn = gr.Button("Sign Up")
|
96 |
+
|
97 |
+
with gr.Column(visible=False) as prediction_panel:
|
98 |
+
gr.Markdown("## Upload Image for Deepfake Detection")
|
99 |
+
image_input = gr.Image(type="pil")
|
100 |
+
result = gr.Textbox(label="Result")
|
101 |
+
predict_btn = gr.Button("Predict")
|
102 |
+
logout_btn = gr.Button("Logout")
|
103 |
+
|
104 |
+
# Logic
|
105 |
+
def handle_login(e, p):
|
106 |
+
msg, ok = login_user(e, p)
|
107 |
+
return msg, gr.update(visible=not ok), gr.update(visible=ok)
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
tensorflow
|
3 |
+
opencv-python
|
4 |
+
Pillow
|
5 |
+
bcrypt
|
6 |
+
numpy
|