parass13 commited on
Commit
ddc8227
Β·
verified Β·
1 Parent(s): 6678c63

Update dashboard.py

Browse files
Files changed (1) hide show
  1. dashboard.py +43 -29
dashboard.py CHANGED
@@ -9,29 +9,23 @@ import tensorflow as tf
9
  import os
10
  import warnings
11
 
12
- # Import pages (make sure each page has layout() function defined)
13
-
14
- from pages import about
15
- from pages import community
16
-
17
- from pages import user_guide
18
-
19
 
20
  # Suppress logs and warnings
21
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
22
  warnings.filterwarnings("ignore")
23
  np.seterr(all='ignore')
24
 
25
- # Load TensorFlow deepfake model
26
  deepfake_model = tf.keras.models.load_model("model_15_64.h5")
27
 
28
- # Setup SQLite database
29
  db_path = os.path.abspath("users.db")
30
  print(f"βœ… Using database at: {db_path}")
31
  conn = sqlite3.connect(db_path, check_same_thread=False)
32
  cursor = conn.cursor()
33
 
34
- # Create table if it doesn't exist
35
  cursor.execute('''
36
  CREATE TABLE IF NOT EXISTS user_details (
37
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -64,33 +58,34 @@ def predict_image(image):
64
 
65
  def register_user(name, phone, email, password):
66
  if not is_valid_email(email):
67
- return "❌ Invalid email"
68
  if not is_valid_phone(phone):
69
- return "❌ Phone must be 10 digits"
70
 
71
  cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
72
  if cursor.fetchone():
73
- return "⚠️ Email already registered"
74
 
75
  hashed_pw = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
76
  cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
77
  (name, phone, email, "U", hashed_pw))
78
  conn.commit()
79
  print(f"βœ… Registered new user: {email}")
80
- return "βœ… Registration successful! Please log in."
81
 
82
  def login_user(email, password):
83
  cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
84
  result = cursor.fetchone()
85
- if result and bcrypt.checkpw(password.encode(), result[0] if isinstance(result[0], bytes) else result[0].encode()):
86
- return "βœ… Login successful!"
87
- return "❌ Invalid credentials"
 
88
 
89
  # Gradio App
90
  with gr.Blocks() as demo:
91
- with gr.Tabs():
92
-
93
 
 
94
  with gr.Tab("πŸ” Login"):
95
  gr.Markdown("### Login or Sign Up")
96
 
@@ -102,17 +97,37 @@ with gr.Blocks() as demo:
102
  login_btn = gr.Button("Login")
103
  signup_btn = gr.Button("Sign Up")
104
 
105
- login_btn.click(fn=login_user, inputs=[email, password], outputs=status)
106
- signup_btn.click(fn=register_user, inputs=[name, phone, email, password], outputs=status)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- with gr.Tab("πŸ§ͺ Detect Deepfake"):
109
- gr.Markdown("### Upload an Image to Detect Deepfake")
110
- image_input = gr.Image(type="pil")
111
- result = gr.Textbox(label="Prediction Result")
112
- predict_btn = gr.Button("Predict")
113
- predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
114
 
115
-
 
116
 
117
  with gr.Tab("ℹ️ About"):
118
  about.layout()
@@ -123,7 +138,6 @@ with gr.Blocks() as demo:
123
  with gr.Tab("πŸ“˜ User Guide"):
124
  user_guide.layout()
125
 
126
-
127
  # Launch App
128
  if __name__ == "__main__":
129
  demo.launch()
 
9
  import os
10
  import warnings
11
 
12
+ # Import pages
13
+ from pages import about, community, user_guide
 
 
 
 
 
14
 
15
  # Suppress logs and warnings
16
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
17
  warnings.filterwarnings("ignore")
18
  np.seterr(all='ignore')
19
 
20
+ # Load deepfake model
21
  deepfake_model = tf.keras.models.load_model("model_15_64.h5")
22
 
23
+ # SQLite setup
24
  db_path = os.path.abspath("users.db")
25
  print(f"βœ… Using database at: {db_path}")
26
  conn = sqlite3.connect(db_path, check_same_thread=False)
27
  cursor = conn.cursor()
28
 
 
29
  cursor.execute('''
30
  CREATE TABLE IF NOT EXISTS user_details (
31
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
58
 
59
  def register_user(name, phone, email, password):
60
  if not is_valid_email(email):
61
+ return "❌ Invalid email", False
62
  if not is_valid_phone(phone):
63
+ return "❌ Phone must be 10 digits", False
64
 
65
  cursor.execute("SELECT * FROM user_details WHERE EMAIL = ?", (email,))
66
  if cursor.fetchone():
67
+ return "⚠️ Email already registered", False
68
 
69
  hashed_pw = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
70
  cursor.execute("INSERT INTO user_details (NAME, PHONE, EMAIL, GENDER, PASSWORD) VALUES (?, ?, ?, ?, ?)",
71
  (name, phone, email, "U", hashed_pw))
72
  conn.commit()
73
  print(f"βœ… Registered new user: {email}")
74
+ return "βœ… Registration successful! Please log in.", False
75
 
76
  def login_user(email, password):
77
  cursor.execute("SELECT PASSWORD FROM user_details WHERE EMAIL = ?", (email,))
78
  result = cursor.fetchone()
79
+ if result and bcrypt.checkpw(password.encode(), result[0]):
80
+ return "βœ… Login successful!", True
81
+ return "❌ Invalid credentials", False
82
+
83
 
84
  # Gradio App
85
  with gr.Blocks() as demo:
86
+ session = gr.State(value=False) # Stores login state (True/False)
 
87
 
88
+ with gr.Tabs() as tabs:
89
  with gr.Tab("πŸ” Login"):
90
  gr.Markdown("### Login or Sign Up")
91
 
 
97
  login_btn = gr.Button("Login")
98
  signup_btn = gr.Button("Sign Up")
99
 
100
+ def handle_login(e, p):
101
+ msg, ok = login_user(e, p)
102
+ return msg, ok
103
+
104
+ def handle_signup(n, ph, e, p):
105
+ msg, ok = register_user(n, ph, e, p)
106
+ return msg, ok
107
+
108
+ login_btn.click(handle_login, [email, password], [status, session])
109
+ signup_btn.click(handle_signup, [name, phone, email, password], [status, session])
110
+
111
+ with gr.Tab("πŸ§ͺ Detect Deepfake") as detect_tab:
112
+ with gr.Column(visible=False) as detection_content:
113
+ gr.Markdown("### Upload an Image to Detect Deepfake")
114
+ image_input = gr.Image(type="pil")
115
+ result = gr.Textbox(label="Prediction Result")
116
+ predict_btn = gr.Button("Predict")
117
+ predict_btn.click(fn=predict_image, inputs=image_input, outputs=result)
118
+
119
+ # Show warning if not logged in
120
+ with gr.Column(visible=True) as login_prompt:
121
+ warning_text = gr.Markdown("⚠️ Please login or sign up to access deepfake detection.")
122
 
123
+ def toggle_tab(logged_in):
124
+ return (
125
+ gr.update(visible=logged_in), # detection_content
126
+ gr.update(visible=not logged_in), # login_prompt
127
+ )
 
128
 
129
+ # Toggle detection tab visibility based on login state
130
+ session.change(fn=toggle_tab, inputs=session, outputs=[detection_content, login_prompt])
131
 
132
  with gr.Tab("ℹ️ About"):
133
  about.layout()
 
138
  with gr.Tab("πŸ“˜ User Guide"):
139
  user_guide.layout()
140
 
 
141
  # Launch App
142
  if __name__ == "__main__":
143
  demo.launch()