parass13 commited on
Commit
dbaa416
Β·
verified Β·
1 Parent(s): 6be0e8c

Update dashboard.py

Browse files
Files changed (1) hide show
  1. dashboard.py +49 -72
dashboard.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import sqlite3
3
  import re
4
  import bcrypt
5
  import numpy as np
@@ -9,8 +8,15 @@ import tensorflow as tf
9
  import os
10
  import warnings
11
 
 
 
12
  from pages import about, community, user_guide
13
 
 
 
 
 
 
14
  # --- Setup ---
15
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
16
  warnings.filterwarnings("ignore")
@@ -32,22 +38,6 @@ with warnings.catch_warnings():
32
  warnings.simplefilter("ignore")
33
  deepfake_model = tf.keras.models.load_model(MODEL_PATH)
34
 
35
- # --- DB ---
36
- DB_PATH = os.path.abspath("users.db")
37
- with sqlite3.connect(DB_PATH) as conn:
38
- cursor = conn.cursor()
39
- cursor.execute('''
40
- CREATE TABLE IF NOT EXISTS user_details (
41
- id INTEGER PRIMARY KEY AUTOINCREMENT,
42
- NAME TEXT NOT NULL,
43
- PHONE TEXT NOT NULL,
44
- EMAIL TEXT UNIQUE NOT NULL,
45
- GENDER TEXT,
46
- PASSWORD BLOB NOT NULL
47
- )
48
- ''')
49
- conn.commit()
50
-
51
  # --- Helpers ---
52
  def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
53
  def is_valid_phone(phone): return re.match(r"^[0-9]{10}$", phone)
@@ -68,43 +58,50 @@ def predict_image(image):
68
  return f"{label} (Confidence: {confidence:.2%})"
69
 
70
  def register_user(name, phone, email, gender, password):
71
- if not all([name, phone, email, gender, password]): return "❌ All fields are required for signup."
 
72
  if not is_valid_email(email): return "❌ Invalid email format."
73
  if not is_valid_phone(phone): return "❌ Phone must be 10 digits."
 
74
  try:
75
- with sqlite3.connect(DB_PATH) as conn:
76
- cursor = conn.cursor()
77
- cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
78
- if cursor.fetchone(): return "⚠️ Email already registered."
79
- hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
80
- cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
81
- (name, phone, email, gender, hashed_pw))
82
- conn.commit()
83
- return "βœ… Registration successful! Please log in."
84
- except sqlite3.Error as e:
85
- print(f"Database error: {e}")
86
- return "❌ A database error occurred."
 
 
 
 
 
 
 
87
 
88
  def login_user(email, password):
89
  if not email or not password: return False
90
  try:
91
- with sqlite3.connect(DB_PATH) as conn:
92
- cursor = conn.cursor()
93
- cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
94
- result = cursor.fetchone()
95
- if result and bcrypt.checkpw(password.encode('utf-8'), result[0]): return True
96
- return False
97
- except sqlite3.Error as e:
98
- print(f"Database error during login: {e}")
99
  return False
100
 
101
  # --- UI ---
102
- with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detection") as demo:
103
  is_logged_in = gr.State(False)
104
 
105
- LOGIN_TAB_NAME = "πŸ” VerifiAI Login"
106
- DETECT_TAB_NAME = "πŸ§ͺ Run Detection"
107
- ABOUT_TAB_NAME = "ℹ️ About VerifiAI"
108
  COMMUNITY_TAB_NAME = "🌐 Community"
109
  GUIDE_TAB_NAME = "πŸ“˜ User Guide"
110
 
@@ -112,10 +109,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detection") as
112
  with gr.Tab(LOGIN_TAB_NAME) as login_tab:
113
  with gr.Row():
114
  with gr.Column(scale=1):
115
- gr.Markdown("## Welcome to VerifiAI!")
116
- gr.Markdown("*AI-powered truth detection for the digital age.*")
117
  with gr.Column(scale=2):
118
- gr.Markdown("### Access VerifiAI")
119
  message_output = gr.Markdown(visible=False)
120
  email_login = gr.Textbox(label="Email", elem_id="login_email")
121
  password_login = gr.Textbox(label="Password", type="password", elem_id="login_pass")
@@ -130,7 +126,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detection") as
130
 
131
  with gr.Tab(DETECT_TAB_NAME, visible=False) as detect_tab:
132
  with gr.Row():
133
- gr.Markdown("## VerifiAI Deepfake Detector")
134
  logout_btn = gr.Button("Logout")
135
  with gr.Row():
136
  image_input = gr.Image(type="pil", label="Upload Image", scale=1)
@@ -143,6 +139,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detection") as
143
  with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
144
 
145
  # --- Events ---
 
146
  def update_ui_on_auth_change(logged_in_status):
147
  if logged_in_status:
148
  return (
@@ -166,7 +163,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detection") as
166
  return False, gr.update(value="❌ Invalid email or password.", visible=True)
167
 
168
  def handle_logout():
169
- return False, "", "" # logged_in=False, clear email/pass fields
170
 
171
  def handle_signup(name, phone, email, gender, password):
172
  msg = register_user(name, phone, email, gender, password)
@@ -175,31 +172,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detection") as
175
  else:
176
  return gr.update(value=msg, visible=True), name, phone, email, gender, password, gr.update(open=True)
177
 
178
- # --- Event Listeners Wiring ---
179
- login_btn.click(
180
- fn=handle_login,
181
- inputs=[email_login, password_login],
182
- outputs=[is_logged_in, message_output]
183
- )
184
-
185
- logout_btn.click(
186
- fn=handle_logout,
187
- inputs=[],
188
- outputs=[is_logged_in, email_login, password_login]
189
- )
190
-
191
- is_logged_in.change(
192
- fn=update_ui_on_auth_change,
193
- inputs=is_logged_in,
194
- outputs=[login_tab, detect_tab, tabs, message_output]
195
- )
196
-
197
- signup_btn.click(
198
- fn=handle_signup,
199
- inputs=[name_signup, phone_signup, email_signup, gender_signup, password_signup],
200
- outputs=[message_output, name_signup, phone_signup, email_signup, gender_signup, password_signup, signup_accordion]
201
- )
202
-
203
  predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
204
 
205
  if __name__ == "__main__":
 
1
  import gradio as gr
 
2
  import re
3
  import bcrypt
4
  import numpy as np
 
8
  import os
9
  import warnings
10
 
11
+ from supabase import create_client, Client
12
+
13
  from pages import about, community, user_guide
14
 
15
+ # --- Supabase Config ---
16
+ SUPABASE_URL = "https://fpbuhzbdtzwomjwytqul.supabase.co"
17
+ SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZwYnVoemJkdHp3b21qd3l0cXVsIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTE5NDk3NzYsImV4cCI6MjA2NzUyNTc3Nn0.oAa2TNNPQMyOGk63AOMZ7XKcwYvy5m-xoSWyvMZd6FY"
18
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
19
+
20
  # --- Setup ---
21
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
22
  warnings.filterwarnings("ignore")
 
38
  warnings.simplefilter("ignore")
39
  deepfake_model = tf.keras.models.load_model(MODEL_PATH)
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # --- Helpers ---
42
  def is_valid_email(email): return re.match(r"[^@]+@[^@]+\.[^@]+", email)
43
  def is_valid_phone(phone): return re.match(r"^[0-9]{10}$", phone)
 
58
  return f"{label} (Confidence: {confidence:.2%})"
59
 
60
  def register_user(name, phone, email, gender, password):
61
+ if not all([name, phone, email, gender, password]):
62
+ return "❌ All fields are required for signup."
63
  if not is_valid_email(email): return "❌ Invalid email format."
64
  if not is_valid_phone(phone): return "❌ Phone must be 10 digits."
65
+
66
  try:
67
+ existing = supabase.table("user_details").select("email").eq("email", email).execute()
68
+ if existing.data:
69
+ return "⚠️ Email already registered."
70
+
71
+ hashed_pw = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
72
+
73
+ supabase.table("user_details").insert({
74
+ "name": name,
75
+ "phone": phone,
76
+ "email": email,
77
+ "gender": gender,
78
+ "password": hashed_pw
79
+ }).execute()
80
+
81
+ return "βœ… Registration successful! Please log in."
82
+
83
+ except Exception as e:
84
+ print(f"Supabase error: {e}")
85
+ return "❌ A Supabase error occurred."
86
 
87
  def login_user(email, password):
88
  if not email or not password: return False
89
  try:
90
+ result = supabase.table("user_details").select("password").eq("email", email).execute()
91
+ if result.data and bcrypt.checkpw(password.encode('utf-8'), result.data[0]["password"].encode("utf-8")):
92
+ return True
93
+ return False
94
+ except Exception as e:
95
+ print(f"Supabase login error: {e}")
 
 
96
  return False
97
 
98
  # --- UI ---
99
+ with gr.Blocks(theme=gr.themes.Soft(), title="VerifiAI - Deepfake Detector") as demo:
100
  is_logged_in = gr.State(False)
101
 
102
+ LOGIN_TAB_NAME = "πŸ” Login"
103
+ DETECT_TAB_NAME = "πŸ§ͺ Detect Deepfake"
104
+ ABOUT_TAB_NAME = "ℹ️ About"
105
  COMMUNITY_TAB_NAME = "🌐 Community"
106
  GUIDE_TAB_NAME = "πŸ“˜ User Guide"
107
 
 
109
  with gr.Tab(LOGIN_TAB_NAME) as login_tab:
110
  with gr.Row():
111
  with gr.Column(scale=1):
112
+ gr.Markdown("## Welcome to VerifiAI!", "Login to access the detector, or sign up for a new account.")
 
113
  with gr.Column(scale=2):
114
+ gr.Markdown("### Login or Sign Up")
115
  message_output = gr.Markdown(visible=False)
116
  email_login = gr.Textbox(label="Email", elem_id="login_email")
117
  password_login = gr.Textbox(label="Password", type="password", elem_id="login_pass")
 
126
 
127
  with gr.Tab(DETECT_TAB_NAME, visible=False) as detect_tab:
128
  with gr.Row():
129
+ gr.Markdown("## Deepfake Detector")
130
  logout_btn = gr.Button("Logout")
131
  with gr.Row():
132
  image_input = gr.Image(type="pil", label="Upload Image", scale=1)
 
139
  with gr.Tab(GUIDE_TAB_NAME): user_guide.layout()
140
 
141
  # --- Events ---
142
+
143
  def update_ui_on_auth_change(logged_in_status):
144
  if logged_in_status:
145
  return (
 
163
  return False, gr.update(value="❌ Invalid email or password.", visible=True)
164
 
165
  def handle_logout():
166
+ return False, "", ""
167
 
168
  def handle_signup(name, phone, email, gender, password):
169
  msg = register_user(name, phone, email, gender, password)
 
172
  else:
173
  return gr.update(value=msg, visible=True), name, phone, email, gender, password, gr.update(open=True)
174
 
175
+ login_btn.click(fn=handle_login, inputs=[email_login, password_login], outputs=[is_logged_in, message_output])
176
+ logout_btn.click(fn=handle_logout, inputs=[], outputs=[is_logged_in, email_login, password_login])
177
+ is_logged_in.change(fn=update_ui_on_auth_change, inputs=is_logged_in, outputs=[login_tab, detect_tab, tabs, message_output])
178
+ signup_btn.click(fn=handle_signup, inputs=[name_signup, phone_signup, email_signup, gender_signup, password_signup],
179
+ outputs=[message_output, name_signup, phone_signup, email_signup, gender_signup, password_signup, signup_accordion])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
181
 
182
  if __name__ == "__main__":