Spaces:
Sleeping
Sleeping
Update dashboard.py
Browse files- dashboard.py +76 -71
dashboard.py
CHANGED
|
@@ -30,14 +30,19 @@ np.seterr(all='ignore')
|
|
| 30 |
|
| 31 |
MODEL_PATH = "model_15_64.h5"
|
| 32 |
if not os.path.exists(MODEL_PATH):
|
|
|
|
| 33 |
dummy_model = tf.keras.Sequential([
|
| 34 |
tf.keras.layers.Input(shape=(128, 128, 3)),
|
| 35 |
tf.keras.layers.Flatten(),
|
| 36 |
tf.keras.layers.Dense(1, activation='sigmoid')
|
| 37 |
])
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# --- Helpers ---
|
| 43 |
def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
|
@@ -58,35 +63,17 @@ def predict_image(image):
|
|
| 58 |
label = "β
Real Image" if prediction >= 0.5 else "β οΈ Fake Image"
|
| 59 |
return f"{label} (Confidence: {confidence:.2%})"
|
| 60 |
|
| 61 |
-
def fetch_dashboard_stats():
|
| 62 |
-
url = f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}?select=gender"
|
| 63 |
-
res = requests.get(url, headers=headers)
|
| 64 |
-
if res.status_code != 200:
|
| 65 |
-
return "β Unable to fetch stats."
|
| 66 |
-
users = res.json()
|
| 67 |
-
total = len(users)
|
| 68 |
-
gender_counts = {"M": 0, "F": 0, "O": 0, "U": 0}
|
| 69 |
-
for user in users:
|
| 70 |
-
gender = user.get("gender", "U")
|
| 71 |
-
gender_counts[gender] += 1
|
| 72 |
-
return (
|
| 73 |
-
f"π₯ **Total Users:** {total}\n\n"
|
| 74 |
-
f"π **Gender Breakdown:**\n"
|
| 75 |
-
f"- Male: {gender_counts['M']}\n"
|
| 76 |
-
f"- Female: {gender_counts['F']}\n"
|
| 77 |
-
f"- Other: {gender_counts['O']}\n"
|
| 78 |
-
f"- Unknown: {gender_counts['U']}"
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
def register_user(name, phone, email, gender, password):
|
| 82 |
if not all([name, phone, email, gender, password]):
|
| 83 |
return "β All fields are required for signup."
|
| 84 |
if not is_valid_email(email): return "β Invalid email format."
|
| 85 |
if not is_valid_phone(phone): return "β Phone must be 10 digits."
|
|
|
|
| 86 |
query_url = f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}?email=eq.{email}"
|
| 87 |
r = requests.get(query_url, headers=headers)
|
| 88 |
if r.status_code == 200 and len(r.json()) > 0:
|
| 89 |
return "β οΈ Email already registered."
|
|
|
|
| 90 |
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode()
|
| 91 |
data = {
|
| 92 |
"name": name,
|
|
@@ -107,71 +94,89 @@ def login_user(email, password):
|
|
| 107 |
return False
|
| 108 |
|
| 109 |
# --- UI ---
|
| 110 |
-
with gr.Blocks(title="VerifiAI - Deepfake Detector") as demo:
|
| 111 |
is_logged_in = gr.State(False)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
with gr.
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
with gr.Tab(
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
def handle_login(email, password):
|
| 142 |
if login_user(email, password):
|
| 143 |
-
|
| 144 |
-
return True, stats, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 145 |
else:
|
| 146 |
-
return False,
|
| 147 |
|
| 148 |
def handle_logout():
|
| 149 |
-
return False, "", ""
|
| 150 |
|
| 151 |
def handle_signup(name, phone, email, gender, password):
|
| 152 |
msg = register_user(name, phone, email, gender, password)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
inputs=[email_login, password_login],
|
| 158 |
-
outputs=[is_logged_in, stats_output, stats_output, detect_tab, message_output]
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
logout_btn.click(
|
| 162 |
-
fn=handle_logout,
|
| 163 |
-
inputs=[],
|
| 164 |
-
outputs=[is_logged_in, email_login, password_login, detect_tab, stats_output]
|
| 165 |
-
)
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
signup_btn.click(
|
| 168 |
fn=handle_signup,
|
| 169 |
inputs=[name_signup, phone_signup, email_signup, gender_signup, password_signup],
|
| 170 |
-
outputs=[message_output]
|
| 171 |
)
|
| 172 |
-
|
| 173 |
predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
demo.launch()
|
| 177 |
-
|
|
|
|
| 30 |
|
| 31 |
MODEL_PATH = "model_15_64.h5"
|
| 32 |
if not os.path.exists(MODEL_PATH):
|
| 33 |
+
print(f"Model file '{MODEL_PATH}' not found. Creating a dummy model for testing.")
|
| 34 |
dummy_model = tf.keras.Sequential([
|
| 35 |
tf.keras.layers.Input(shape=(128, 128, 3)),
|
| 36 |
tf.keras.layers.Flatten(),
|
| 37 |
tf.keras.layers.Dense(1, activation='sigmoid')
|
| 38 |
])
|
| 39 |
+
with warnings.catch_warnings():
|
| 40 |
+
warnings.simplefilter("ignore")
|
| 41 |
+
dummy_model.save(MODEL_PATH)
|
| 42 |
|
| 43 |
+
with warnings.catch_warnings():
|
| 44 |
+
warnings.simplefilter("ignore")
|
| 45 |
+
deepfake_model = tf.keras.models.load_model(MODEL_PATH)
|
| 46 |
|
| 47 |
# --- Helpers ---
|
| 48 |
def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
|
|
|
|
| 63 |
label = "β
Real Image" if prediction >= 0.5 else "β οΈ Fake Image"
|
| 64 |
return f"{label} (Confidence: {confidence:.2%})"
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def register_user(name, phone, email, gender, password):
|
| 67 |
if not all([name, phone, email, gender, password]):
|
| 68 |
return "β All fields are required for signup."
|
| 69 |
if not is_valid_email(email): return "β Invalid email format."
|
| 70 |
if not is_valid_phone(phone): return "β Phone must be 10 digits."
|
| 71 |
+
|
| 72 |
query_url = f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}?email=eq.{email}"
|
| 73 |
r = requests.get(query_url, headers=headers)
|
| 74 |
if r.status_code == 200 and len(r.json()) > 0:
|
| 75 |
return "β οΈ Email already registered."
|
| 76 |
+
|
| 77 |
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode()
|
| 78 |
data = {
|
| 79 |
"name": name,
|
|
|
|
| 94 |
return False
|
| 95 |
|
| 96 |
# --- UI ---
|
| 97 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detector") as demo:
|
| 98 |
is_logged_in = gr.State(False)
|
| 99 |
|
| 100 |
+
LOGIN_TAB_NAME = "π Login"
|
| 101 |
+
DETECT_TAB_NAME = "π§ͺ Detect Deepfake"
|
| 102 |
+
ABOUT_TAB_NAME = "βΉοΈ About"
|
| 103 |
+
COMMUNITY_TAB_NAME = "π Community"
|
| 104 |
+
GUIDE_TAB_NAME = "π User Guide"
|
| 105 |
+
|
| 106 |
+
with gr.Tabs(selected=LOGIN_TAB_NAME) as tabs:
|
| 107 |
+
with gr.Tab(LOGIN_TAB_NAME) as login_tab:
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column(scale=1):
|
| 110 |
+
gr.Markdown("## Welcome!", "Login to access the detector, or sign up for a new account.")
|
| 111 |
+
with gr.Column(scale=2):
|
| 112 |
+
gr.Markdown("### Login or Sign Up")
|
| 113 |
+
message_output = gr.Markdown(visible=False)
|
| 114 |
+
email_login = gr.Textbox(label="Email")
|
| 115 |
+
password_login = gr.Textbox(label="Password", type="password")
|
| 116 |
+
login_btn = gr.Button("Login", variant="primary")
|
| 117 |
+
with gr.Accordion("New User? Click here to Sign Up", open=False) as signup_accordion:
|
| 118 |
+
name_signup = gr.Textbox(label="Name")
|
| 119 |
+
phone_signup = gr.Textbox(label="Phone (10 digits)")
|
| 120 |
+
email_signup = gr.Textbox(label="Email")
|
| 121 |
+
gender_signup = gr.Dropdown(label="Gender", choices=["Male", "Female", "Other"])
|
| 122 |
+
password_signup = gr.Textbox(label="Create Password", type="password")
|
| 123 |
+
signup_btn = gr.Button("Sign Up")
|
| 124 |
+
|
| 125 |
+
with gr.Tab(DETECT_TAB_NAME, visible=False) as detect_tab:
|
| 126 |
+
with gr.Row():
|
| 127 |
+
gr.Markdown("## Deepfake Detector")
|
| 128 |
+
logout_btn = gr.Button("Logout")
|
| 129 |
+
with gr.Row():
|
| 130 |
+
image_input = gr.Image(type="pil", label="Upload Image", scale=1)
|
| 131 |
+
with gr.Column(scale=1):
|
| 132 |
+
result = gr.Textbox(label="Prediction Result", interactive=False)
|
| 133 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
| 134 |
+
|
| 135 |
+
with gr.Tab(ABOUT_TAB_NAME): about.layout()
|
| 136 |
+
with gr.Tab(COMMUNITY_TAB_NAME): community.layout()
|
| 137 |
+
with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
|
| 138 |
+
|
| 139 |
+
def update_ui_on_auth_change(logged_in_status):
|
| 140 |
+
if logged_in_status:
|
| 141 |
+
return (
|
| 142 |
+
gr.update(visible=False),
|
| 143 |
+
gr.update(visible=True),
|
| 144 |
+
gr.update(selected=DETECT_TAB_NAME),
|
| 145 |
+
gr.update(value="β
Login successful!", visible=True)
|
| 146 |
+
)
|
| 147 |
+
else:
|
| 148 |
+
return (
|
| 149 |
+
gr.update(visible=True),
|
| 150 |
+
gr.update(visible=False),
|
| 151 |
+
gr.update(selected=LOGIN_TAB_NAME),
|
| 152 |
+
gr.update(value="", visible=False)
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
def handle_login(email, password):
|
| 156 |
if login_user(email, password):
|
| 157 |
+
return True, gr.update(value="β
Login successful!", visible=True)
|
|
|
|
| 158 |
else:
|
| 159 |
+
return False, gr.update(value="β Invalid email or password.", visible=True)
|
| 160 |
|
| 161 |
def handle_logout():
|
| 162 |
+
return False, "", ""
|
| 163 |
|
| 164 |
def handle_signup(name, phone, email, gender, password):
|
| 165 |
msg = register_user(name, phone, email, gender, password)
|
| 166 |
+
if msg.startswith("β
"):
|
| 167 |
+
return gr.update(value=msg, visible=True), "", "", "", "", "", gr.update(open=False)
|
| 168 |
+
else:
|
| 169 |
+
return gr.update(value=msg, visible=True), name, phone, email, gender, password, gr.update(open=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
login_btn.click(fn=handle_login, inputs=[email_login, password_login], outputs=[is_logged_in, message_output])
|
| 172 |
+
logout_btn.click(fn=handle_logout, inputs=[], outputs=[is_logged_in, email_login, password_login])
|
| 173 |
+
is_logged_in.change(fn=update_ui_on_auth_change, inputs=is_logged_in, outputs=[login_tab, detect_tab, tabs, message_output])
|
| 174 |
signup_btn.click(
|
| 175 |
fn=handle_signup,
|
| 176 |
inputs=[name_signup, phone_signup, email_signup, gender_signup, password_signup],
|
| 177 |
+
outputs=[message_output, name_signup, phone_signup, email_signup, gender_signup, password_signup, signup_accordion]
|
| 178 |
)
|
|
|
|
| 179 |
predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
demo.launch()
|
|
|