M-ManiTeja commited on
Commit
b79abd1
·
verified ·
1 Parent(s): eec85c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -50
app.py CHANGED
@@ -4,33 +4,24 @@ import torch.nn as nn
4
  from torchvision import transforms, models
5
  from PIL import Image
6
 
7
- # ==========================================
8
- # 1. SETUP & MODEL LOADING (Required for deployment)
9
- # ==========================================
10
- DEVICE = torch.device("cpu") # Spaces use CPU by default
11
  IMG_SIZE = 224
12
  classes = ['fake', 'real']
13
 
14
- # Rebuild the model architecture
15
  model = models.efficientnet_b0(weights=None)
16
  in_f = model.classifier[1].in_features
17
  model.classifier = nn.Linear(in_f, 2)
18
 
19
- # Load the trained weights
20
  model.load_state_dict(torch.load("deepfake_detector.pth", map_location=DEVICE))
21
  model.to(DEVICE)
22
  model.eval()
23
 
24
- # Define Image Transformations
25
  clean_tf = transforms.Compose([
26
  transforms.Resize((IMG_SIZE, IMG_SIZE)),
27
  transforms.ToTensor(),
28
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
29
  ])
30
 
31
- # ==========================================
32
- # 2. PREDICTION LOGIC
33
- # ==========================================
34
  def enhanced_predict(img):
35
  if img is None:
36
  return None, "<div class='warning-box'>SYSTEM ALERT: Please upload or capture an image for analysis.</div>"
@@ -48,7 +39,6 @@ def enhanced_predict(img):
48
 
49
  confidences = {classes[0]: fake_prob, classes[1]: real_prob}
50
 
51
- # Dynamic styling for the output message
52
  max_conf = max(fake_prob, real_prob) * 100
53
  if max_conf < 60.0:
54
  warning_msg = f"<div class='warning-box'>[WARNING] Low Confidence ({max_conf:.1f}%). Network anomaly detected. Manual review advised.</div>"
@@ -57,14 +47,7 @@ def enhanced_predict(img):
57
 
58
  return confidences, warning_msg
59
 
60
- # ==========================================
61
- # 3. PROFESSIONAL UI
62
- # ==========================================
63
  custom_css = """
64
- /* -------------------------------------
65
- FORCE GLOBAL DARK THEME VARIABLES
66
- (This kills the bright white boxes!)
67
- -------------------------------------- */
68
  :root, .gradio-container, .dark {
69
  --background-fill-primary: #0b0f19 !important;
70
  --background-fill-secondary: #0f172a !important;
@@ -81,9 +64,6 @@ body {
81
  font-family: 'Inter', sans-serif !important;
82
  }
83
 
84
- /* -------------------------------------
85
- HEADER STYLING
86
- -------------------------------------- */
87
  .header-box {
88
  background: rgba(15, 23, 42, 0.7);
89
  backdrop-filter: blur(12px);
@@ -95,6 +75,7 @@ body {
95
  border-top: 3px solid #38bdf8;
96
  box-shadow: 0 10px 30px -10px rgba(56, 189, 248, 0.3);
97
  }
 
98
  .header-box h1 {
99
  margin-bottom: 0.5rem;
100
  color: #f8fafc !important;
@@ -104,15 +85,13 @@ body {
104
  text-transform: uppercase;
105
  text-shadow: 0 0 15px rgba(56,189,248,0.5);
106
  }
 
107
  .header-box p {
108
  font-size: 1.1rem;
109
  color: #94a3b8 !important;
110
  font-weight: 400;
111
  }
112
 
113
- /* -------------------------------------
114
- HIGH-VISIBILITY NAVIGATION TABS (PILL BUTTONS)
115
- -------------------------------------- */
116
  .tabs > .tab-nav {
117
  border: none !important;
118
  display: flex !important;
@@ -121,6 +100,7 @@ body {
121
  margin-bottom: 2.5rem !important;
122
  background: transparent !important;
123
  }
 
124
  .tabs > .tab-nav > button {
125
  background: linear-gradient(145deg, #1e293b, #0f172a) !important;
126
  border: 1px solid rgba(56, 189, 248, 0.4) !important;
@@ -133,12 +113,14 @@ body {
133
  box-shadow: 0 4px 10px rgba(0,0,0,0.5) !important;
134
  transition: all 0.3s ease !important;
135
  }
 
136
  .tabs > .tab-nav > button:hover {
137
  color: #38bdf8 !important;
138
  border-color: #38bdf8 !important;
139
  box-shadow: 0 0 15px rgba(56,189,248,0.3) !important;
140
  transform: translateY(-2px);
141
  }
 
142
  .tabs > .tab-nav > button.selected {
143
  background: linear-gradient(135deg, #0284c7 0%, #38bdf8 100%) !important;
144
  color: #ffffff !important;
@@ -148,9 +130,6 @@ body {
148
  transform: scale(1.05);
149
  }
150
 
151
- /* -------------------------------------
152
- TEXT & HEADER OVERRIDES FOR TAB 2
153
- -------------------------------------- */
154
  .gradio-container h3 {
155
  color: #38bdf8 !important;
156
  font-family: 'Courier New', monospace !important;
@@ -160,13 +139,11 @@ body {
160
  padding-bottom: 8px;
161
  margin-bottom: 15px;
162
  }
 
163
  .gradio-container span.text-gray-500 {
164
  color: #94a3b8 !important;
165
  }
166
 
167
- /* -------------------------------------
168
- INFO CARDS (AWARENESS TAB)
169
- -------------------------------------- */
170
  .info-card {
171
  background: rgba(30, 41, 59, 0.6);
172
  padding: 1.5rem;
@@ -179,20 +156,19 @@ body {
179
  border-bottom: 1px solid rgba(255,255,255,0.05);
180
  transition: transform 0.3s ease, box-shadow 0.3s ease;
181
  }
 
182
  .info-card:hover {
183
  transform: translateY(-5px);
184
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
185
  background: rgba(30, 41, 59, 0.9);
186
  }
 
187
  .info-card p, .info-card li {
188
  color: #f1f5f9 !important;
189
  font-size: 1.05rem;
190
  line-height: 1.6;
191
  }
192
 
193
- /* -------------------------------------
194
- ANIMATED PRIMARY BUTTON
195
- -------------------------------------- */
196
  button.primary {
197
  background: linear-gradient(135deg, #2563eb 0%, #3b82f6 100%) !important;
198
  border: 1px solid #60a5fa !important;
@@ -205,19 +181,16 @@ button.primary {
205
  transition: all 0.3s ease !important;
206
  margin-top: 10px !important;
207
  }
 
208
  button.primary:hover {
209
  transform: translateY(-2px) !important;
210
  box-shadow: 0 8px 25px rgba(37, 99, 235, 0.7) !important;
211
  background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%) !important;
212
  }
213
 
214
- /* Status Boxes */
215
  .warning-box { border-left: 4px solid #f59e0b; background: rgba(245, 158, 11, 0.15); padding: 1rem; border-radius: 6px; color: #fcd34d !important; font-weight: 600; font-size: 1.1em;}
216
  .success-box { border-left: 4px solid #10b981; background: rgba(16, 185, 129, 0.15); padding: 1rem; border-radius: 6px; color: #6ee7b7 !important; font-weight: 600; font-size: 1.1em;}
217
 
218
- /* -------------------------------------
219
- WEBCAM & INTERNAL BUTTON FIX
220
- -------------------------------------- */
221
  .gradio-container button[aria-label="Capture"],
222
  .gradio-container button[aria-label="Clear"],
223
  .gradio-container .icon-button,
@@ -244,9 +217,8 @@ button.primary:hover {
244
  }
245
  """
246
 
247
- with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as demo:
248
 
249
- # --- GLOBAL HEADER ---
250
  gr.HTML("""
251
  <div class="header-box">
252
  <h1>TrueFace AI: Deepfake Detector</h1>
@@ -254,15 +226,10 @@ with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as demo:
254
  </div>
255
  """)
256
 
257
- # ==========================================
258
- # TAB 1: AWARENESS & EDUCATION
259
- # ==========================================
260
  with gr.Tab("Awareness & Education"):
261
  with gr.Row():
262
  with gr.Column(scale=1):
263
- # Path changed from Colab's /content/ to local directory
264
- gr.Image("project.jpeg",
265
- show_label=False, show_download_button=False, interactive=False)
266
  gr.Markdown("<center><i style='color:#94a3b8;'>Conceptual visualization of biometric facial mapping.</i></center>")
267
 
268
  with gr.Column(scale=1):
@@ -287,9 +254,6 @@ with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as demo:
287
  </div>
288
  """)
289
 
290
- # ==========================================
291
- # TAB 2: THE DETECTOR PROJECT
292
- # ==========================================
293
  with gr.Tab("Deepfake Detector"):
294
  with gr.Row():
295
  with gr.Column(scale=1):
@@ -304,5 +268,4 @@ with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as demo:
304
 
305
  analyze_btn.click(fn=enhanced_predict, inputs=input_image, outputs=[output_label, output_warning])
306
 
307
- # Launch modified for production server
308
- demo.launch()
 
4
  from torchvision import transforms, models
5
  from PIL import Image
6
 
7
+ DEVICE = torch.device("cpu")
 
 
 
8
  IMG_SIZE = 224
9
  classes = ['fake', 'real']
10
 
 
11
  model = models.efficientnet_b0(weights=None)
12
  in_f = model.classifier[1].in_features
13
  model.classifier = nn.Linear(in_f, 2)
14
 
 
15
  model.load_state_dict(torch.load("deepfake_detector.pth", map_location=DEVICE))
16
  model.to(DEVICE)
17
  model.eval()
18
 
 
19
  clean_tf = transforms.Compose([
20
  transforms.Resize((IMG_SIZE, IMG_SIZE)),
21
  transforms.ToTensor(),
22
  transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
23
  ])
24
 
 
 
 
25
  def enhanced_predict(img):
26
  if img is None:
27
  return None, "<div class='warning-box'>SYSTEM ALERT: Please upload or capture an image for analysis.</div>"
 
39
 
40
  confidences = {classes[0]: fake_prob, classes[1]: real_prob}
41
 
 
42
  max_conf = max(fake_prob, real_prob) * 100
43
  if max_conf < 60.0:
44
  warning_msg = f"<div class='warning-box'>[WARNING] Low Confidence ({max_conf:.1f}%). Network anomaly detected. Manual review advised.</div>"
 
47
 
48
  return confidences, warning_msg
49
 
 
 
 
50
  custom_css = """
 
 
 
 
51
  :root, .gradio-container, .dark {
52
  --background-fill-primary: #0b0f19 !important;
53
  --background-fill-secondary: #0f172a !important;
 
64
  font-family: 'Inter', sans-serif !important;
65
  }
66
 
 
 
 
67
  .header-box {
68
  background: rgba(15, 23, 42, 0.7);
69
  backdrop-filter: blur(12px);
 
75
  border-top: 3px solid #38bdf8;
76
  box-shadow: 0 10px 30px -10px rgba(56, 189, 248, 0.3);
77
  }
78
+
79
  .header-box h1 {
80
  margin-bottom: 0.5rem;
81
  color: #f8fafc !important;
 
85
  text-transform: uppercase;
86
  text-shadow: 0 0 15px rgba(56,189,248,0.5);
87
  }
88
+
89
  .header-box p {
90
  font-size: 1.1rem;
91
  color: #94a3b8 !important;
92
  font-weight: 400;
93
  }
94
 
 
 
 
95
  .tabs > .tab-nav {
96
  border: none !important;
97
  display: flex !important;
 
100
  margin-bottom: 2.5rem !important;
101
  background: transparent !important;
102
  }
103
+
104
  .tabs > .tab-nav > button {
105
  background: linear-gradient(145deg, #1e293b, #0f172a) !important;
106
  border: 1px solid rgba(56, 189, 248, 0.4) !important;
 
113
  box-shadow: 0 4px 10px rgba(0,0,0,0.5) !important;
114
  transition: all 0.3s ease !important;
115
  }
116
+
117
  .tabs > .tab-nav > button:hover {
118
  color: #38bdf8 !important;
119
  border-color: #38bdf8 !important;
120
  box-shadow: 0 0 15px rgba(56,189,248,0.3) !important;
121
  transform: translateY(-2px);
122
  }
123
+
124
  .tabs > .tab-nav > button.selected {
125
  background: linear-gradient(135deg, #0284c7 0%, #38bdf8 100%) !important;
126
  color: #ffffff !important;
 
130
  transform: scale(1.05);
131
  }
132
 
 
 
 
133
  .gradio-container h3 {
134
  color: #38bdf8 !important;
135
  font-family: 'Courier New', monospace !important;
 
139
  padding-bottom: 8px;
140
  margin-bottom: 15px;
141
  }
142
+
143
  .gradio-container span.text-gray-500 {
144
  color: #94a3b8 !important;
145
  }
146
 
 
 
 
147
  .info-card {
148
  background: rgba(30, 41, 59, 0.6);
149
  padding: 1.5rem;
 
156
  border-bottom: 1px solid rgba(255,255,255,0.05);
157
  transition: transform 0.3s ease, box-shadow 0.3s ease;
158
  }
159
+
160
  .info-card:hover {
161
  transform: translateY(-5px);
162
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
163
  background: rgba(30, 41, 59, 0.9);
164
  }
165
+
166
  .info-card p, .info-card li {
167
  color: #f1f5f9 !important;
168
  font-size: 1.05rem;
169
  line-height: 1.6;
170
  }
171
 
 
 
 
172
  button.primary {
173
  background: linear-gradient(135deg, #2563eb 0%, #3b82f6 100%) !important;
174
  border: 1px solid #60a5fa !important;
 
181
  transition: all 0.3s ease !important;
182
  margin-top: 10px !important;
183
  }
184
+
185
  button.primary:hover {
186
  transform: translateY(-2px) !important;
187
  box-shadow: 0 8px 25px rgba(37, 99, 235, 0.7) !important;
188
  background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%) !important;
189
  }
190
 
 
191
  .warning-box { border-left: 4px solid #f59e0b; background: rgba(245, 158, 11, 0.15); padding: 1rem; border-radius: 6px; color: #fcd34d !important; font-weight: 600; font-size: 1.1em;}
192
  .success-box { border-left: 4px solid #10b981; background: rgba(16, 185, 129, 0.15); padding: 1rem; border-radius: 6px; color: #6ee7b7 !important; font-weight: 600; font-size: 1.1em;}
193
 
 
 
 
194
  .gradio-container button[aria-label="Capture"],
195
  .gradio-container button[aria-label="Clear"],
196
  .gradio-container .icon-button,
 
217
  }
218
  """
219
 
220
+ with gr.Blocks() as demo:
221
 
 
222
  gr.HTML("""
223
  <div class="header-box">
224
  <h1>TrueFace AI: Deepfake Detector</h1>
 
226
  </div>
227
  """)
228
 
 
 
 
229
  with gr.Tab("Awareness & Education"):
230
  with gr.Row():
231
  with gr.Column(scale=1):
232
+ gr.Image("project.jpeg", show_label=False, interactive=False)
 
 
233
  gr.Markdown("<center><i style='color:#94a3b8;'>Conceptual visualization of biometric facial mapping.</i></center>")
234
 
235
  with gr.Column(scale=1):
 
254
  </div>
255
  """)
256
 
 
 
 
257
  with gr.Tab("Deepfake Detector"):
258
  with gr.Row():
259
  with gr.Column(scale=1):
 
268
 
269
  analyze_btn.click(fn=enhanced_predict, inputs=input_image, outputs=[output_label, output_warning])
270
 
271
+ demo.launch(theme=gr.themes.Base(), css=custom_css)