KoreaPeter commited on
Commit
74f0127
·
verified ·
1 Parent(s): ca75b13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -98
app.py CHANGED
@@ -27,17 +27,19 @@ EXPLAINER_REGISTRY = {
27
 
28
  METHODS_BY_BRANCH = {
29
  "low": [
30
- ("HiResCAM", "hirescam"),
31
- ("GradCAMElementWise", "gradcamelementwise"),
32
- ("LayerCAM", "layercam"),
33
  ],
34
  "high": [
35
- ("EigenGradCAM", "eigengradcam"),
36
- ("GradCAM++", "gradcamplusplus"),
37
- ("XGradCAM", "xgradcam"),
38
  ],
39
  }
40
 
 
 
41
  CATEGORY_FAKE = 1 # XAI always visualizes forgery evidence
42
 
43
  _cache: dict = {}
@@ -69,7 +71,7 @@ def explain(
69
  threshold: float,
70
  ):
71
  if image is None:
72
- return None, "⚠️ Please upload an image."
73
 
74
  with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
75
  image.save(f.name)
@@ -99,99 +101,83 @@ def explain(
99
  except Exception as e:
100
  msg = str(e)
101
  if "face" in msg.lower() or "NoneType" in msg or "index" in msg.lower():
102
- return None, "😶 No face detected.\nPlease use an image with a clearly visible face."
103
- return None, f"❌ Explainer error: {e}"
104
  finally:
105
  os.unlink(tmp_path)
106
 
107
- method_label = dict(METHODS_BY_BRANCH[branch]).get(method, method)
108
- status = (
109
- f"✅ {method_label} · {branch.upper()}-level branch · "
110
- f"{MODEL_NAME[variant]} · {dataset}"
111
- )
112
- return result, status
113
 
114
 
115
- # ── Assets (HF CDN absolute URLs — keeps GIF animation, bypasses static serving) ─
116
  _HF_BASE = "https://huggingface.co/spaces/KoreaPeter/DeepFake-Detection-XAI/resolve/main"
117
  DUAL_BRANCH_SRC = f"{_HF_BASE}/dual_branch.gif"
118
- XAI_DEMO_SRC = f"{_HF_BASE}/xai_demo.gif"
119
 
120
 
121
  # ── Hero banner ───────────────────────────────────────────────────────────────
122
  HERO = f"""
123
  <div style="
124
- background: linear-gradient(135deg, #311b92 0%, #5e35b1 45%, #1a237e 100%);
125
- border-radius: 12px; padding: 36px 40px; margin-bottom: 8px;
126
- border: 1px solid #4527a0; box-shadow: 0 4px 24px rgba(94,53,177,0.4);
127
  ">
128
  <div style="display:flex; align-items:center; gap:16px; margin-bottom:12px;">
129
  <span style="font-size:2.4rem;">🔬</span>
130
  <div>
131
- <h1 style="margin:0; color:#ede7f6; font-size:1.9rem; font-weight:700; letter-spacing:-0.5px;">DeepGuard · XAI</h1>
132
- <p style="margin:4px 0 0; color:#b39ddb; font-size:1rem;">Dual-Branch Deepfake Explainability</p>
133
  </div>
134
  </div>
135
- <p style="margin:0 0 16px; color:#d1c4e9; font-size:0.95rem; line-height:1.6;">
136
- Our custom <strong style="color:#ede7f6;">MS-EffGCViT</strong> reasons over two parallel streams — a
137
- <strong style="color:#90caf9;">high-resolution low-level branch</strong> for local forgery artifacts and a
138
- <strong style="color:#ef9a9a;">low-resolution high-level branch</strong> for global semantic structure.
139
  This tool mirrors that dual-branch design to reveal <em>where and why</em> a face is flagged as manipulated.
140
  </p>
141
- <div style="text-align:center; margin:8px 0 16px;"><img src="{DUAL_BRANCH_SRC}" style="max-width:70%; border-radius:8px; border:1px solid #4527a0;"></div>
142
  <div style="display:flex; gap:12px; flex-wrap:wrap;">
143
  <a href="https://github.com/HanMoonSub/DeepGuard" target="_blank"
144
- style="background:#1a237e; color:#b39ddb; padding:7px 16px; border-radius:6px;
145
- text-decoration:none; font-size:0.85rem; border:1px solid #5e35b1;">🔗 GitHub</a>
146
  <a href="https://huggingface.co/KoreaPeter" target="_blank"
147
- style="background:#1a237e; color:#b39ddb; padding:7px 16px; border-radius:6px;
148
- text-decoration:none; font-size:0.85rem; border:1px solid #5e35b1;">🤗 HuggingFace</a>
149
  <a href="https://huggingface.co/spaces/KoreaPeter/DeepFake-Image-Detection" target="_blank"
150
- style="background:#1a237e; color:#b39ddb; padding:7px 16px; border-radius:6px;
151
- text-decoration:none; font-size:0.85rem; border:1px solid #5e35b1;">🕵️ Image Detection</a>
152
  <a href="https://huggingface.co/spaces/KoreaPeter/DeepFake-Vidoe-Detection" target="_blank"
153
- style="background:#1a237e; color:#b39ddb; padding:7px 16px; border-radius:6px;
154
- text-decoration:none; font-size:0.85rem; border:1px solid #5e35b1;">🎬 Video Detection</a>
155
  </div>
156
  </div>
157
  """
158
 
159
- # ── Display guide (xai_demo.gif) ──────────────────────────────────────────────
160
- DISPLAY_GUIDE = f"""
161
- ### 🎨 Display Guide
162
-
163
- <p align="center"><img src="{XAI_DEMO_SRC}" style="max-width:80%; border-radius:8px;"></p>
164
-
165
- | Mode | What it shows | Best for |
166
- |---|---|---|
167
- | **Heatmap** | Continuous activation distribution over the face | Seeing the overall forgery-attention gradient |
168
- | **BBox** | Discrete box(es) around suspicious regions, labeled with local probability | Pinpointing localized artifacts |
169
- | **Heatmap + BBox** | Heatmap overlay *plus* boxes (recommended for reporting) | Full forensic evidence in one view |
170
-
171
- - **Image Weight** blends original vs. heatmap — `1.0` = original only, `0.0` = heatmap only.
172
- - **Threshold** binarizes the CAM — `0.9` keeps only the top-10% hottest regions.
173
- """
174
-
175
  # ── Theme & CSS ───────────────────────────────────────────────────────────────
176
  THEME = gr.themes.Base(
177
- primary_hue="indigo",
178
  neutral_hue="slate",
179
  font=gr.themes.GoogleFont("Inter"),
180
  ).set(
181
- body_background_fill="#0f0a1e",
182
- body_text_color="#ede7f6",
183
- block_background_fill="#1a1033",
184
- block_border_color="#4527a0",
185
- input_background_fill="#0f0a1e",
186
- button_primary_background_fill="#5e35b1",
187
- button_primary_text_color="#ede7f6",
188
- button_primary_border_color="#7e57c2",
189
- button_primary_background_fill_hover="#7e57c2",
190
- button_primary_text_color_hover="#ffffff",
191
  )
192
 
193
  CSS = """
194
- .result-box textarea { font-size:1.05rem !important; font-weight:bold !important; text-align:center; }
 
 
 
 
 
 
195
  footer { display:none !important; }
196
  """
197
 
@@ -228,13 +214,15 @@ with gr.Blocks(title="DeepGuard XAI") as demo:
228
  choices=[("🔵 Low — local artifacts", "low"), ("🔴 High — global semantics", "high")],
229
  value="low",
230
  label=None,
 
231
  )
232
 
233
  gr.Markdown("### 📐 CAM Method")
234
  method_radio = gr.Radio(
235
  choices=METHODS_BY_BRANCH["low"],
236
- value="hirescam",
237
  label=None,
 
238
  )
239
 
240
  gr.Markdown("### 🖼️ Display Mode")
@@ -248,16 +236,23 @@ with gr.Blocks(title="DeepGuard XAI") as demo:
248
  label=None,
249
  )
250
 
 
 
 
 
 
 
 
 
 
 
 
251
  run_btn = gr.Button("🔬 Visualize", variant="primary", size="lg")
252
 
253
  with gr.Column(scale=1, min_width=320):
254
  result_output = gr.Image(label="Forgery Localization", type="numpy")
255
- status_output = gr.Textbox(
256
- label="Status", interactive=False, lines=2, elem_classes=["result-box"],
257
- )
258
 
259
  gr.Markdown("""
260
- ---
261
  **Branch Guide**
262
 
263
  | Branch | Feature Map | Focus | Best For |
@@ -271,51 +266,51 @@ with gr.Blocks(title="DeepGuard XAI") as demo:
271
  |---|---|---|
272
  | 🔵 Low | **HiResCAM** | GradCAM with element-wise activation×gradient; provably faithful |
273
  | 🔵 Low | **GradCAMElementWise** | Element-wise activation×gradient + ReLU before summing |
274
- | 🔵 Low | **LayerCAM** | Positive-gradient spatial weighting; strong in lower layers |
275
- | 🔴 High | **EigenGradCAM** | 1st principal component of activations×grad; cleaner GradCAM |
276
  | 🔴 High | **GradCAM++** | GradCAM using second-order gradients |
277
  | 🔴 High | **XGradCAM** | GradCAM with gradients scaled by normalized activations |
278
  """)
279
 
280
- gr.Markdown(DISPLAY_GUIDE)
281
-
282
- with gr.Accordion("⚙️ Advanced Settings", open=False):
283
- gr.Markdown("Fine-tune the CAM smoothing and overlay. Defaults are recommended for most cases.")
284
  with gr.Row():
285
- with gr.Column():
286
- aug_smooth_chk = gr.Checkbox(
287
- value=False, label="Aug Smooth",
288
- info="TTA (horizontal flips) before averaging → smoother maps. Slower.",
289
- )
290
- eigen_smooth_chk = gr.Checkbox(
291
- value=False, label="Eigen Smooth",
292
- info="PCA noise reduction → keeps dominant forgery pattern only.",
293
- )
294
- with gr.Column():
295
- image_weight_slider = gr.Slider(
296
- minimum=0.0, maximum=1.0, value=0.7, step=0.05,
297
- label="Image Weight",
298
- info="0.0 heatmap only ←→ 1.0 original only (heatmap modes)",
299
- )
300
- threshold_slider = gr.Slider(
301
- minimum=0.5, maximum=1.0, value=0.9, step=0.05,
302
- label="Threshold",
303
- info="CAM binarization cutoff — 0.9 keeps top-10% regions",
304
- )
305
-
306
- # branch → method choices sync
307
  def _sync_methods(branch):
308
  choices = METHODS_BY_BRANCH[branch]
309
- return gr.update(choices=choices, value=choices[0][1])
310
 
311
  branch_radio.change(fn=_sync_methods, inputs=branch_radio, outputs=method_radio)
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
  inputs = [
314
  image_input, variant_radio, dataset_radio, branch_radio, method_radio,
315
  display_radio, aug_smooth_chk, eigen_smooth_chk,
316
  image_weight_slider, threshold_slider,
317
  ]
318
- run_btn.click(fn=explain, inputs=inputs, outputs=[result_output, status_output])
319
 
320
  if __name__ == "__main__":
321
  demo.launch(theme=THEME, css=CSS)
 
27
 
28
  METHODS_BY_BRANCH = {
29
  "low": [
30
+ ("HiResCAM", "hirescam"),
31
+ ("GradCAMElementWise", "gradcamelementwise"),
32
+ ("⭐ LayerCAM (recommended)", "layercam"),
33
  ],
34
  "high": [
35
+ ("⭐ EigenGradCAM (recommended)", "eigengradcam"),
36
+ ("GradCAM++", "gradcamplusplus"),
37
+ ("XGradCAM", "xgradcam"),
38
  ],
39
  }
40
 
41
+ RECOMMENDED = {"low": "layercam", "high": "eigengradcam"}
42
+
43
  CATEGORY_FAKE = 1 # XAI always visualizes forgery evidence
44
 
45
  _cache: dict = {}
 
71
  threshold: float,
72
  ):
73
  if image is None:
74
+ raise gr.Error("Please upload an image first.")
75
 
76
  with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
77
  image.save(f.name)
 
101
  except Exception as e:
102
  msg = str(e)
103
  if "face" in msg.lower() or "NoneType" in msg or "index" in msg.lower():
104
+ raise gr.Error("😶 No face detected. Please use an image with a clearly visible face.")
105
+ raise gr.Error(f"Explainer error: {e}")
106
  finally:
107
  os.unlink(tmp_path)
108
 
109
+ return result
 
 
 
 
 
110
 
111
 
112
+ # ── Assets (HF CDN absolute URL — keeps GIF animation) ─────────────────────────
113
  _HF_BASE = "https://huggingface.co/spaces/KoreaPeter/DeepFake-Detection-XAI/resolve/main"
114
  DUAL_BRANCH_SRC = f"{_HF_BASE}/dual_branch.gif"
 
115
 
116
 
117
  # ── Hero banner ───────────────────────────────────────────────────────────────
118
  HERO = f"""
119
  <div style="
120
+ background: linear-gradient(135deg, #0f172a 0%, #115e59 55%, #064e3b 100%);
121
+ border-radius: 14px; padding: 36px 40px; margin-bottom: 8px;
122
+ border: 1px solid #10b981; box-shadow: 0 4px 28px rgba(16,185,129,0.22);
123
  ">
124
  <div style="display:flex; align-items:center; gap:16px; margin-bottom:12px;">
125
  <span style="font-size:2.4rem;">🔬</span>
126
  <div>
127
+ <h1 style="margin:0; color:#ecfdf5; font-size:1.9rem; font-weight:700; letter-spacing:-0.5px;">DeepGuard · XAI</h1>
128
+ <p style="margin:4px 0 0; color:#6ee7b7; font-size:1rem;">Dual-Branch Deepfake Explainability</p>
129
  </div>
130
  </div>
131
+ <p style="margin:0 0 16px; color:#a7f3d0; font-size:0.95rem; line-height:1.6;">
132
+ Our custom <strong style="color:#ecfdf5;">MS-EffGCViT</strong> reasons over two parallel streams — a
133
+ <strong style="color:#93c5fd;">high-resolution low-level branch</strong> for local forgery artifacts and a
134
+ <strong style="color:#fca5a5;">low-resolution high-level branch</strong> for global semantic structure.
135
  This tool mirrors that dual-branch design to reveal <em>where and why</em> a face is flagged as manipulated.
136
  </p>
137
+ <div style="text-align:center; margin:8px 0 16px;"><img src="{DUAL_BRANCH_SRC}" style="max-width:70%; border-radius:8px; border:1px solid #10b981;"></div>
138
  <div style="display:flex; gap:12px; flex-wrap:wrap;">
139
  <a href="https://github.com/HanMoonSub/DeepGuard" target="_blank"
140
+ style="background:#064e3b; color:#6ee7b7; padding:7px 16px; border-radius:6px;
141
+ text-decoration:none; font-size:0.85rem; border:1px solid #10b981;">🔗 GitHub</a>
142
  <a href="https://huggingface.co/KoreaPeter" target="_blank"
143
+ style="background:#064e3b; color:#6ee7b7; padding:7px 16px; border-radius:6px;
144
+ text-decoration:none; font-size:0.85rem; border:1px solid #10b981;">🤗 HuggingFace</a>
145
  <a href="https://huggingface.co/spaces/KoreaPeter/DeepFake-Image-Detection" target="_blank"
146
+ style="background:#064e3b; color:#6ee7b7; padding:7px 16px; border-radius:6px;
147
+ text-decoration:none; font-size:0.85rem; border:1px solid #10b981;">🕵️ Image Detection</a>
148
  <a href="https://huggingface.co/spaces/KoreaPeter/DeepFake-Vidoe-Detection" target="_blank"
149
+ style="background:#064e3b; color:#6ee7b7; padding:7px 16px; border-radius:6px;
150
+ text-decoration:none; font-size:0.85rem; border:1px solid #10b981;">🎬 Video Detection</a>
151
  </div>
152
  </div>
153
  """
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  # ── Theme & CSS ───────────────────────────────────────────────────────────────
156
  THEME = gr.themes.Base(
157
+ primary_hue="emerald",
158
  neutral_hue="slate",
159
  font=gr.themes.GoogleFont("Inter"),
160
  ).set(
161
+ body_background_fill="#0b0f17",
162
+ body_text_color="#e5e7eb",
163
+ block_background_fill="#131a24",
164
+ block_border_color="#1f2937",
165
+ input_background_fill="#0b0f17",
166
+ button_primary_background_fill="#10b981",
167
+ button_primary_text_color="#04140c",
168
+ button_primary_border_color="#10b981",
169
+ button_primary_background_fill_hover="#34d399",
170
+ button_primary_text_color_hover="#04140c",
171
  )
172
 
173
  CSS = """
174
+ /* branch/method groups get a colored frame driven by the selected branch */
175
+ #branch-group, #method-group {
176
+ border: 2px solid #3b82f6 !important; /* default = low (blue) */
177
+ border-radius: 10px !important;
178
+ padding: 10px 12px !important;
179
+ transition: border-color 0.2s ease;
180
+ }
181
  footer { display:none !important; }
182
  """
183
 
 
214
  choices=[("🔵 Low — local artifacts", "low"), ("🔴 High — global semantics", "high")],
215
  value="low",
216
  label=None,
217
+ elem_id="branch-group",
218
  )
219
 
220
  gr.Markdown("### 📐 CAM Method")
221
  method_radio = gr.Radio(
222
  choices=METHODS_BY_BRANCH["low"],
223
+ value=RECOMMENDED["low"],
224
  label=None,
225
+ elem_id="method-group",
226
  )
227
 
228
  gr.Markdown("### 🖼️ Display Mode")
 
236
  label=None,
237
  )
238
 
239
+ image_weight_slider = gr.Slider(
240
+ minimum=0.0, maximum=1.0, value=0.7, step=0.05,
241
+ label="Image Weight",
242
+ info="0.0 heatmap only ←→ 1.0 original only (heatmap modes)",
243
+ )
244
+ threshold_slider = gr.Slider(
245
+ minimum=0.5, maximum=1.0, value=0.9, step=0.05,
246
+ label="Threshold",
247
+ info="CAM binarization cutoff — 0.9 keeps top-10% regions",
248
+ )
249
+
250
  run_btn = gr.Button("🔬 Visualize", variant="primary", size="lg")
251
 
252
  with gr.Column(scale=1, min_width=320):
253
  result_output = gr.Image(label="Forgery Localization", type="numpy")
 
 
 
254
 
255
  gr.Markdown("""
 
256
  **Branch Guide**
257
 
258
  | Branch | Feature Map | Focus | Best For |
 
266
  |---|---|---|
267
  | 🔵 Low | **HiResCAM** | GradCAM with element-wise activation×gradient; provably faithful |
268
  | 🔵 Low | **GradCAMElementWise** | Element-wise activation×gradient + ReLU before summing |
269
+ | 🔵 Low | **⭐ LayerCAM** | Positive-gradient spatial weighting; strong in lower layers |
270
+ | 🔴 High | **⭐ EigenGradCAM** | 1st principal component of activations×grad; cleaner GradCAM |
271
  | 🔴 High | **GradCAM++** | GradCAM using second-order gradients |
272
  | 🔴 High | **XGradCAM** | GradCAM with gradients scaled by normalized activations |
273
  """)
274
 
275
+ with gr.Accordion("🌀 CAM Smoothing", open=False):
276
+ gr.Markdown("Optional smoothing passes over the CAM. Off is recommended for most cases.")
 
 
277
  with gr.Row():
278
+ aug_smooth_chk = gr.Checkbox(
279
+ value=False, label="Aug Smooth",
280
+ info="TTA (horizontal flips) before averaging → smoother maps. Slower.",
281
+ )
282
+ eigen_smooth_chk = gr.Checkbox(
283
+ value=False, label="Eigen Smooth",
284
+ info="PCA noise reduction → keeps dominant forgery pattern only.",
285
+ )
286
+
287
+ # branch → method choices sync (recommended method becomes default)
 
 
 
 
 
 
 
 
 
 
 
 
288
  def _sync_methods(branch):
289
  choices = METHODS_BY_BRANCH[branch]
290
+ return gr.update(choices=choices, value=RECOMMENDED[branch])
291
 
292
  branch_radio.change(fn=_sync_methods, inputs=branch_radio, outputs=method_radio)
293
 
294
+ # branch → recolor the branch/method frames (blue=low, red=high)
295
+ branch_radio.change(
296
+ fn=None,
297
+ inputs=branch_radio,
298
+ outputs=None,
299
+ js="""(b) => {
300
+ const c = (b === 'high') ? '#ef4444' : '#3b82f6';
301
+ const g = document.getElementById('branch-group');
302
+ const m = document.getElementById('method-group');
303
+ if (g) g.style.borderColor = c;
304
+ if (m) m.style.borderColor = c;
305
+ }""",
306
+ )
307
+
308
  inputs = [
309
  image_input, variant_radio, dataset_radio, branch_radio, method_radio,
310
  display_radio, aug_smooth_chk, eigen_smooth_chk,
311
  image_weight_slider, threshold_slider,
312
  ]
313
+ run_btn.click(fn=explain, inputs=inputs, outputs=result_output)
314
 
315
  if __name__ == "__main__":
316
  demo.launch(theme=THEME, css=CSS)