Spaces:
Sleeping
Sleeping
Update dashboard.py
Browse files- dashboard.py +54 -36
dashboard.py
CHANGED
|
@@ -9,12 +9,20 @@ import tensorflow as tf
|
|
| 9 |
import os
|
| 10 |
import warnings
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 14 |
warnings.filterwarnings("ignore")
|
| 15 |
np.seterr(all='ignore')
|
| 16 |
|
| 17 |
-
# Load
|
| 18 |
deepfake_model = tf.keras.models.load_model("model_15_64.h5")
|
| 19 |
|
| 20 |
# Setup SQLite connection
|
|
@@ -36,10 +44,11 @@ CREATE TABLE IF NOT EXISTS user_details (
|
|
| 36 |
''')
|
| 37 |
conn.commit()
|
| 38 |
|
| 39 |
-
#
|
| 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 |
|
|
@@ -50,7 +59,7 @@ def preprocess_image(image):
|
|
| 50 |
image = image.astype(np.float32) / 255.0
|
| 51 |
return np.expand_dims(image, axis=0)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
def predict_image(image):
|
| 55 |
preprocessed = preprocess_image(image)
|
| 56 |
prediction = deepfake_model.predict(preprocessed)[0][0]
|
|
@@ -74,7 +83,7 @@ def register_user(name, phone, email, password):
|
|
| 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()
|
|
@@ -82,47 +91,56 @@ def login_user(email, password):
|
|
| 82 |
return "β
Login successful!", True
|
| 83 |
return "β Invalid credentials", False
|
| 84 |
|
| 85 |
-
# Gradio
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
session = gr.State({})
|
| 88 |
-
show_login = gr.State(True)
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
result = gr.Textbox(label="Result")
|
| 106 |
-
predict_btn = gr.Button("Predict")
|
| 107 |
-
logout_btn = gr.Button("Logout")
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
msg, ok = login_user(e, p)
|
| 112 |
-
return msg, gr.update(visible=not ok), gr.update(visible=ok)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch()
|
|
|
|
| 9 |
import os
|
| 10 |
import warnings
|
| 11 |
|
| 12 |
+
# Custom page modules (ensure these files exist)
|
| 13 |
+
import home
|
| 14 |
+
import about
|
| 15 |
+
import examples
|
| 16 |
+
import community
|
| 17 |
+
import user_guide
|
| 18 |
+
import install
|
| 19 |
+
|
| 20 |
+
# Suppress all warnings and unnecessary logs
|
| 21 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 22 |
warnings.filterwarnings("ignore")
|
| 23 |
np.seterr(all='ignore')
|
| 24 |
|
| 25 |
+
# Load the deepfake model
|
| 26 |
deepfake_model = tf.keras.models.load_model("model_15_64.h5")
|
| 27 |
|
| 28 |
# Setup SQLite connection
|
|
|
|
| 44 |
''')
|
| 45 |
conn.commit()
|
| 46 |
|
| 47 |
+
# Email validation
|
| 48 |
def is_valid_email(email):
|
| 49 |
return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
| 50 |
|
| 51 |
+
# Phone validation
|
| 52 |
def is_valid_phone(phone):
|
| 53 |
return re.match(r"^[0-9]{10}$", phone)
|
| 54 |
|
|
|
|
| 59 |
image = image.astype(np.float32) / 255.0
|
| 60 |
return np.expand_dims(image, axis=0)
|
| 61 |
|
| 62 |
+
# Predict deepfake
|
| 63 |
def predict_image(image):
|
| 64 |
preprocessed = preprocess_image(image)
|
| 65 |
prediction = deepfake_model.predict(preprocessed)[0][0]
|
|
|
|
| 83 |
print(f"β
Registered new user: {email}")
|
| 84 |
return "β
Registration successful! Please log in.", True
|
| 85 |
|
| 86 |
+
# Login existing user
|
| 87 |
def login_user(email, password):
|
| 88 |
cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
|
| 89 |
result = cursor.fetchone()
|
|
|
|
| 91 |
return "β
Login successful!", True
|
| 92 |
return "β Invalid credentials", False
|
| 93 |
|
| 94 |
+
# Gradio interface
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
session = gr.State({})
|
|
|
|
| 97 |
|
| 98 |
+
with gr.Tabs():
|
| 99 |
+
with gr.Tab("Home"):
|
| 100 |
+
home.layout()
|
| 101 |
|
| 102 |
+
with gr.Tab("Login"):
|
| 103 |
+
status = gr.Textbox(label="", interactive=False)
|
| 104 |
+
name = gr.Textbox(label="Name (Sign Up Only)")
|
| 105 |
+
phone = gr.Textbox(label="Phone (Sign Up Only)")
|
| 106 |
+
email = gr.Textbox(label="Email")
|
| 107 |
+
password = gr.Textbox(label="Password", type="password")
|
| 108 |
+
login_btn = gr.Button("Login")
|
| 109 |
+
signup_btn = gr.Button("Sign Up")
|
| 110 |
|
| 111 |
+
def handle_login(e, p):
|
| 112 |
+
msg, ok = login_user(e, p)
|
| 113 |
+
return msg
|
| 114 |
|
| 115 |
+
def handle_signup(n, ph, e, p):
|
| 116 |
+
msg, ok = register_user(n, ph, e, p)
|
| 117 |
+
return msg
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
login_btn.click(handle_login, [email, password], status)
|
| 120 |
+
signup_btn.click(handle_signup, [name, phone, email, password], status)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
with gr.Tab("Detect Deepfake"):
|
| 123 |
+
gr.Markdown("### Upload an Image to Detect")
|
| 124 |
+
image_input = gr.Image(type="pil")
|
| 125 |
+
result = gr.Textbox(label="Result")
|
| 126 |
+
predict_btn = gr.Button("Predict")
|
| 127 |
+
predict_btn.click(predict_image, inputs=image_input, outputs=result)
|
| 128 |
|
| 129 |
+
with gr.Tab("Examples"):
|
| 130 |
+
examples.layout()
|
| 131 |
|
| 132 |
+
with gr.Tab("About"):
|
| 133 |
+
about.layout()
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
with gr.Tab("Community"):
|
| 136 |
+
community.layout()
|
| 137 |
+
|
| 138 |
+
with gr.Tab("User Guide"):
|
| 139 |
+
user_guide.layout()
|
| 140 |
+
|
| 141 |
+
with gr.Tab("Install"):
|
| 142 |
+
install.layout()
|
| 143 |
+
|
| 144 |
+
# Run the app
|
| 145 |
if __name__ == "__main__":
|
| 146 |
demo.launch()
|