S-Mithilesh commited on
Commit
9b508e8
·
verified ·
1 Parent(s): e65243a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +183 -446
app.py CHANGED
@@ -6,9 +6,9 @@ import cv2
6
 
7
  from PIL import Image
8
  from transformers import (
9
- AutoConfig,
10
- AutoModelForImageClassification,
11
- ViTImageProcessor,
12
  )
13
 
14
  MODEL_REPO = "SARVM/Refined_ViT"
@@ -16,51 +16,40 @@ MODEL_REPO = "SARVM/Refined_ViT"
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
 
18
  DEVICE = torch.device(
19
- "cuda" if torch.cuda.is_available() else "cpu"
20
  )
21
 
22
  print("=" * 60)
23
  print("FORESIGHT - MODEL INITIALIZATION")
24
  print("=" * 60)
25
  print(f"Model repository : {MODEL_REPO}")
26
- print(f"Device : {DEVICE}")
27
  print(f"HF token present : {HF_TOKEN is not None}")
28
 
29
  processor = ViTImageProcessor(
30
- do_resize=True,
31
- size={
32
- "height": 224,
33
- "width": 224
34
- },
35
- do_rescale=True,
36
- rescale_factor=1 / 255,
37
- do_normalize=True,
38
- image_mean=[
39
- 0.485,
40
- 0.456,
41
- 0.406
42
- ],
43
- image_std=[
44
- 0.229,
45
- 0.224,
46
- 0.225
47
- ]
48
  )
49
 
50
  print("Loading custom model...")
51
 
52
  config = AutoConfig.from_pretrained(
53
- MODEL_REPO,
54
- token=HF_TOKEN,
55
- trust_remote_code=True
56
  )
57
 
58
  model = AutoModelForImageClassification.from_pretrained(
59
- MODEL_REPO,
60
- config=config,
61
- token=HF_TOKEN,
62
- trust_remote_code=True,
63
- output_attentions=True
64
  )
65
 
66
  model.to(DEVICE)
@@ -68,488 +57,236 @@ model.eval()
68
 
69
  print("Model loaded successfully.")
70
 
71
- model.config.id2label = {
72
- 0: "FAKE",
73
- 1: "REAL"
74
- }
75
-
76
- model.config.label2id = {
77
- "FAKE": 0,
78
- "REAL": 1
79
- }
80
 
81
  print("Labels:", model.config.id2label)
82
 
83
- def compute_attention_rollout(attentions):
84
-
85
- if attentions is None:
86
- return None
87
-
88
- # Expected shape:
89
- # [layers, batch, heads, tokens, tokens]
90
-
91
- att_mat = torch.stack(attentions)
92
-
93
- # Average across attention heads
94
- # [layers, batch, tokens, tokens]
95
-
96
- att_mat = att_mat.mean(dim=2)
97
-
98
- # Select first image in batch
99
- # [layers, tokens, tokens]
100
-
101
- att_mat = att_mat[:, 0]
102
-
103
- num_tokens = att_mat.size(-1)
104
-
105
- # Residual connection
106
-
107
- residual_att = torch.eye(
108
- num_tokens,
109
- device=att_mat.device,
110
- dtype=att_mat.dtype
111
- )
112
-
113
- # Add residual attention
114
 
115
- aug_att_mat = att_mat + residual_att
116
-
117
- # Normalize
118
-
119
- aug_att_mat = aug_att_mat / (
120
- aug_att_mat.sum(
121
- dim=-1,
122
- keepdim=True
123
- ) + 1e-8
124
- )
125
 
126
- # Joint attention
 
127
 
128
- joint_attentions = torch.zeros_like(
129
- aug_att_mat
130
- )
131
 
132
- joint_attentions[0] = aug_att_mat[0]
 
133
 
134
- for layer in range(
135
- 1,
136
- aug_att_mat.size(0)
137
- ):
138
 
139
- joint_attentions[layer] = (
140
- aug_att_mat[layer]
141
- @ joint_attentions[layer - 1]
 
 
142
  )
143
 
144
- return joint_attentions[-1]
145
-
146
- def create_attention_overlay(
147
- image,
148
- attentions
149
- ):
150
-
151
- rollout = compute_attention_rollout(
152
- attentions
153
- )
154
 
155
- if rollout is None:
156
- return np.array(image)
157
 
158
- # CLS token attention to patch tokens
159
-
160
- mask = rollout[0, 1:]
161
-
162
- num_patches = mask.shape[0]
163
-
164
- grid_size = int(
165
- np.sqrt(num_patches)
166
- )
167
-
168
- if grid_size * grid_size != num_patches:
169
-
170
- print(
171
- "Warning: patch count is not square."
172
- )
173
 
174
- return np.array(image)
 
175
 
176
- # Reshape into 2D patch grid
177
 
178
- mask = mask.reshape(
179
- grid_size,
180
- grid_size
181
- )
182
 
183
- mask = mask.detach().cpu().numpy()
 
184
 
185
- # Normalize
 
186
 
187
- mask = (
188
- mask - mask.min()
189
- ) / (
190
- mask.max() - mask.min() + 1e-8
191
- )
192
 
193
- # Resize to original image dimensions
 
 
194
 
195
- mask = cv2.resize(
196
- mask,
197
- image.size,
198
- interpolation=cv2.INTER_CUBIC
199
- )
200
 
201
- # Generate heatmap
 
202
 
203
- heatmap = cv2.applyColorMap(
204
- np.uint8(
205
- 255 * mask
206
- ),
207
- cv2.COLORMAP_JET
208
- )
209
 
210
- # Convert BGR to RGB
 
211
 
212
- heatmap = cv2.cvtColor(
213
- heatmap,
214
- cv2.COLOR_BGR2RGB
215
- )
216
 
217
- image_np = np.array(
218
- image
219
- ).astype(
220
- np.uint8
221
- )
222
 
223
- # Blend original image and heatmap
 
224
 
225
- overlay = cv2.addWeighted(
226
- image_np,
227
- 0.60,
228
- heatmap,
229
- 0.40,
230
- 0
231
- )
232
 
233
- return overlay
234
 
235
  def predict(image):
 
 
236
 
237
- if image is None:
238
-
239
- return (
240
- None,
241
- None,
242
- None
243
- )
244
-
245
- # Convert to PIL RGB
246
-
247
- if not isinstance(
248
- image,
249
- Image.Image
250
- ):
251
-
252
- image = Image.fromarray(
253
- image
254
- )
255
-
256
- image = image.convert(
257
- "RGB"
258
- )
259
-
260
- # Process image
261
-
262
- inputs = processor(
263
- images=image,
264
- return_tensors="pt"
265
- )
266
-
267
- # Move inputs to device
268
-
269
- inputs = {
270
- key: value.to(DEVICE)
271
- for key, value in inputs.items()
272
- }
273
-
274
- # Run inference
275
-
276
- with torch.no_grad():
277
-
278
- outputs = model(
279
- **inputs,
280
- output_attentions=True
281
- )
282
-
283
- # Get logits
284
-
285
- logits = outputs.logits
286
-
287
- # Convert to probabilities
288
-
289
- probabilities = torch.softmax(
290
- logits,
291
- dim=-1
292
- )
293
 
294
- # Get prediction
295
 
296
- confidence, predicted_index = torch.max(
297
- probabilities,
298
- dim=-1
299
- )
300
 
301
- predicted_index = (
302
- predicted_index.item()
303
- )
304
 
305
- confidence = (
306
- confidence.item()
307
- )
308
 
309
- # Get label
 
 
310
 
311
- prediction = model.config.id2label.get(
312
- predicted_index,
313
- str(predicted_index)
314
- )
315
 
316
- confidence_text = (
317
- f"{confidence * 100:.2f}%"
318
- )
319
 
320
- # Generate attention visualization
 
321
 
322
- overlay = create_attention_overlay(
323
- image,
324
- outputs.attentions
325
- )
326
 
327
- return (
328
- prediction,
329
- confidence_text,
330
- overlay
331
- )
332
 
333
  custom_css = """
334
-
335
  {
336
- --primary-blue: #2563eb;
337
- --hero-text: #0f172a;
338
  }
339
-
340
  .dark {
341
- --hero-text: #f8fafc;
342
  }
343
-
344
  body {
345
- background-color: var(
346
- --background-fill-primary
347
- );
348
  }
349
-
350
  .hero {
351
- text-align: center;
352
- font-family: 'Inter', sans-serif;
353
- font-size: 48px;
354
- font-weight: 800;
355
- letter-spacing: -0.04em;
356
- margin-top: 50px;
357
- color: var(
358
- --hero-text
359
- ) !important;
360
  }
361
-
362
  .sub {
363
- text-align: center;
364
- opacity: 0.7;
365
- font-size: 14px;
366
- font-weight: 600;
367
- letter-spacing: 0.1em;
368
- text-transform: uppercase;
369
- margin-bottom: 40px;
370
- color: var(
371
- --body-text-color
372
- );
373
  }
374
-
375
  .glass {
376
- background: var(
377
- --block-background-fill
378
- ) !important;
379
-
380
- border: 1px solid var(
381
- --border-color-primary
382
- ) !important;
383
-
384
- border-radius: 12px !important;
385
-
386
- padding: 24px !important;
387
-
388
- box-shadow: var(
389
- --block-shadow
390
- );
391
-
392
- transition: all 0.2s ease;
393
-
394
  }
395
-
396
- .glass {
397
- border-color: var(
398
- --primary-blue
399
- ) !important;
400
-
401
- box-shadow: 0 4px 20px rgba(
402
- 37,
403
- 99,
404
- 235,
405
- 0.1
406
- );
407
-
408
  }
409
-
410
  button.primary {
411
- background: var(
412
- --primary-blue
413
- ) !important;
414
-
415
- color: white !important;
416
-
417
- border: none !important;
418
-
419
- font-weight: 600 !important;
420
-
421
- padding: 12px 24px !important;
422
-
423
- border-radius: 8px !important;
424
-
425
- box-shadow: 0 4px 12px rgba(
426
- 37,
427
- 99,
428
- 235,
429
- 0.2
430
- ) !important;
431
-
432
  }
433
-
434
- button.primary {
435
- background: #1d4ed8 !important;
436
-
437
- transform: translateY(
438
- -1px
439
- );
440
-
441
- box-shadow: 0 6px 16px rgba(
442
- 37,
443
- 99,
444
- 235,
445
- 0.3
446
- ) !important;
447
-
448
  }
449
-
450
  .gr-label {
451
- font-weight: 600 !important;
452
-
453
- font-size: 12px !important;
454
-
455
- text-transform: uppercase;
456
-
457
- color: var(
458
- --primary-blue
459
- ) !important;
460
-
461
  }
462
-
463
  """
464
 
465
  with gr.Blocks(
466
- css=custom_css,
467
- theme=gr.themes.Soft(
468
- primary_hue="blue",
469
- font=[
470
- gr.themes.GoogleFont(
471
- "Inter"
472
- ),
473
- "ui-sans-serif",
474
- "system-ui"
475
- ]
476
- )
477
  ) as demo:
478
 
479
- gr.Markdown(
480
- """
481
- <div class="hero">
482
- FORESIGHT
483
- <span style="color:#3b82f6">
484
- .
485
- </span>
486
- </div>
487
- """
488
- )
489
-
490
- gr.Markdown(
491
- """
492
- <div class="sub">
493
- Deep Intelligence Neural Analysis
494
- </div>
495
- """
496
- )
497
-
498
- with gr.Row():
499
-
500
- with gr.Column():
501
-
502
- with gr.Group(
503
- elem_classes="glass"
504
- ):
505
-
506
- input_image = gr.Image(
507
- type="pil",
508
- label="Source Input"
509
- )
510
-
511
- run_btn = gr.Button(
512
- "RUN DIAGNOSTIC",
513
- variant="primary"
514
- )
515
-
516
- with gr.Column():
517
-
518
- with gr.Group(
519
- elem_classes="glass"
520
- ):
521
-
522
- output_label = gr.Label(
523
- label="Classification Verdict"
524
- )
525
-
526
- output_conf = gr.Textbox(
527
- label="Confidence Rating",
528
- interactive=False
529
- )
530
-
531
- heatmap_output = gr.Image(
532
- label="Vulnerability Visualization"
533
- )
534
-
535
- run_btn.click(
536
- fn=predict,
537
- inputs=input_image,
538
- outputs=[
539
- output_label,
540
- output_conf,
541
- heatmap_output
542
- ]
543
- )
544
 
545
- if name == "main":
 
 
 
 
 
 
546
 
547
- demo.launch(
548
- server_name="0.0.0.0",
549
- server_port=int(
550
- os.getenv(
551
- "PORT",
552
- "7860"
553
- )
 
 
 
 
 
 
 
 
 
554
  )
555
- )
 
 
 
 
 
 
6
 
7
  from PIL import Image
8
  from transformers import (
9
+ AutoConfig,
10
+ AutoModelForImageClassification,
11
+ ViTImageProcessor,
12
  )
13
 
14
  MODEL_REPO = "SARVM/Refined_ViT"
 
16
  HF_TOKEN = os.getenv("HF_TOKEN")
17
 
18
  DEVICE = torch.device(
19
+ "cuda" if torch.cuda.is_available() else "cpu"
20
  )
21
 
22
  print("=" * 60)
23
  print("FORESIGHT - MODEL INITIALIZATION")
24
  print("=" * 60)
25
  print(f"Model repository : {MODEL_REPO}")
26
+ print(f"Device : {DEVICE}")
27
  print(f"HF token present : {HF_TOKEN is not None}")
28
 
29
  processor = ViTImageProcessor(
30
+ do_resize=True,
31
+ size={"height": 224, "width": 224},
32
+ do_rescale=True,
33
+ rescale_factor=1 / 255,
34
+ do_normalize=True,
35
+ image_mean=[0.485, 0.456, 0.406],
36
+ image_std=[0.229, 0.224, 0.225],
 
 
 
 
 
 
 
 
 
 
 
37
  )
38
 
39
  print("Loading custom model...")
40
 
41
  config = AutoConfig.from_pretrained(
42
+ MODEL_REPO,
43
+ token=HF_TOKEN,
44
+ trust_remote_code=True,
45
  )
46
 
47
  model = AutoModelForImageClassification.from_pretrained(
48
+ MODEL_REPO,
49
+ config=config,
50
+ token=HF_TOKEN,
51
+ trust_remote_code=True,
52
+ output_attentions=True,
53
  )
54
 
55
  model.to(DEVICE)
 
57
 
58
  print("Model loaded successfully.")
59
 
60
+ model.config.id2label = {0: "FAKE", 1: "REAL"}
61
+ model.config.label2id = {"FAKE": 0, "REAL": 1}
 
 
 
 
 
 
 
62
 
63
  print("Labels:", model.config.id2label)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ def compute_attention_rollout(attentions):
67
+ if attentions is None:
68
+ return None
 
 
 
 
 
 
 
69
 
70
+ # Expected shape: [layers, batch, heads, tokens, tokens]
71
+ att_mat = torch.stack(attentions)
72
 
73
+ # Average across attention heads -> [layers, batch, tokens, tokens]
74
+ att_mat = att_mat.mean(dim=2)
 
75
 
76
+ # Select first image in batch -> [layers, tokens, tokens]
77
+ att_mat = att_mat[:, 0]
78
 
79
+ num_tokens = att_mat.size(-1)
 
 
 
80
 
81
+ # Residual connection
82
+ residual_att = torch.eye(
83
+ num_tokens,
84
+ device=att_mat.device,
85
+ dtype=att_mat.dtype,
86
  )
87
 
88
+ # Add residual attention
89
+ aug_att_mat = att_mat + residual_att
 
 
 
 
 
 
 
 
90
 
91
+ # Normalize
92
+ aug_att_mat = aug_att_mat / (aug_att_mat.sum(dim=-1, keepdim=True) + 1e-8)
93
 
94
+ # Joint attention
95
+ joint_attentions = torch.zeros_like(aug_att_mat)
96
+ joint_attentions[0] = aug_att_mat[0]
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+ for layer in range(1, aug_att_mat.size(0)):
99
+ joint_attentions[layer] = aug_att_mat[layer] @ joint_attentions[layer - 1]
100
 
101
+ return joint_attentions[-1]
102
 
 
 
 
 
103
 
104
+ def create_attention_overlay(image, attentions):
105
+ rollout = compute_attention_rollout(attentions)
106
 
107
+ if rollout is None:
108
+ return np.array(image)
109
 
110
+ # CLS token attention to patch tokens
111
+ mask = rollout[0, 1:]
112
+ num_patches = mask.shape[0]
113
+ grid_size = int(np.sqrt(num_patches))
 
114
 
115
+ if grid_size * grid_size != num_patches:
116
+ print("Warning: patch count is not square.")
117
+ return np.array(image)
118
 
119
+ # Reshape into 2D patch grid
120
+ mask = mask.reshape(grid_size, grid_size)
121
+ mask = mask.detach().cpu().numpy()
 
 
122
 
123
+ # Normalize
124
+ mask = (mask - mask.min()) / (mask.max() - mask.min() + 1e-8)
125
 
126
+ # Resize to original image dimensions
127
+ mask = cv2.resize(mask, image.size, interpolation=cv2.INTER_CUBIC)
 
 
 
 
128
 
129
+ # Generate heatmap
130
+ heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_JET)
131
 
132
+ # Convert BGR to RGB
133
+ heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)
 
 
134
 
135
+ image_np = np.array(image).astype(np.uint8)
 
 
 
 
136
 
137
+ # Blend original image and heatmap
138
+ overlay = cv2.addWeighted(image_np, 0.60, heatmap, 0.40, 0)
139
 
140
+ return overlay
 
 
 
 
 
 
141
 
 
142
 
143
  def predict(image):
144
+ if image is None:
145
+ return None, None, None
146
 
147
+ # Convert to PIL RGB
148
+ if not isinstance(image, Image.Image):
149
+ image = Image.fromarray(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ image = image.convert("RGB")
152
 
153
+ # Process image
154
+ inputs = processor(images=image, return_tensors="pt")
 
 
155
 
156
+ # Move inputs to device
157
+ inputs = {key: value.to(DEVICE) for key, value in inputs.items()}
 
158
 
159
+ # Run inference
160
+ with torch.no_grad():
161
+ outputs = model(**inputs, output_attentions=True)
162
 
163
+ # Get logits and probabilities
164
+ logits = outputs.logits
165
+ probabilities = torch.softmax(logits, dim=-1)
166
 
167
+ # Get prediction
168
+ confidence, predicted_index = torch.max(probabilities, dim=-1)
169
+ predicted_index = predicted_index.item()
170
+ confidence = confidence.item()
171
 
172
+ # Get label
173
+ prediction = model.config.id2label.get(predicted_index, str(predicted_index))
174
+ confidence_text = f"{confidence * 100:.2f}%"
175
 
176
+ # Generate attention visualization
177
+ overlay = create_attention_overlay(image, outputs.attentions)
178
 
179
+ return prediction, confidence_text, overlay
 
 
 
180
 
 
 
 
 
 
181
 
182
  custom_css = """
 
183
  {
184
+ --primary-blue: #2563eb;
185
+ --hero-text: #0f172a;
186
  }
 
187
  .dark {
188
+ --hero-text: #f8fafc;
189
  }
 
190
  body {
191
+ background-color: var(--background-fill-primary);
 
 
192
  }
 
193
  .hero {
194
+ text-align: center;
195
+ font-family: 'Inter', sans-serif;
196
+ font-size: 48px;
197
+ font-weight: 800;
198
+ letter-spacing: -0.04em;
199
+ margin-top: 50px;
200
+ color: var(--hero-text) !important;
 
 
201
  }
 
202
  .sub {
203
+ text-align: center;
204
+ opacity: 0.7;
205
+ font-size: 14px;
206
+ font-weight: 600;
207
+ letter-spacing: 0.1em;
208
+ text-transform: uppercase;
209
+ margin-bottom: 40px;
210
+ color: var(--body-text-color);
 
 
211
  }
 
212
  .glass {
213
+ background: var(--block-background-fill) !important;
214
+ border: 1px solid var(--border-color-primary) !important;
215
+ border-radius: 12px !important;
216
+ padding: 24px !important;
217
+ box-shadow: var(--block-shadow);
218
+ transition: all 0.2s ease;
 
 
 
 
 
 
 
 
 
 
 
 
219
  }
220
+ .glass:hover {
221
+ border-color: var(--primary-blue) !important;
222
+ box-shadow: 0 4px 20px rgba(37, 99, 235, 0.1);
 
 
 
 
 
 
 
 
 
 
223
  }
 
224
  button.primary {
225
+ background: var(--primary-blue) !important;
226
+ color: white !important;
227
+ border: none !important;
228
+ font-weight: 600 !important;
229
+ padding: 12px 24px !important;
230
+ border-radius: 8px !important;
231
+ box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2) !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  }
233
+ button.primary:hover {
234
+ background: #1d4ed8 !important;
235
+ transform: translateY(-1px);
236
+ box-shadow: 0 6px 16px rgba(37, 99, 235, 0.3) !important;
 
 
 
 
 
 
 
 
 
 
 
237
  }
 
238
  .gr-label {
239
+ font-weight: 600 !important;
240
+ font-size: 12px !important;
241
+ text-transform: uppercase;
242
+ color: var(--primary-blue) !important;
 
 
 
 
 
 
243
  }
 
244
  """
245
 
246
  with gr.Blocks(
247
+ css=custom_css,
248
+ theme=gr.themes.Soft(
249
+ primary_hue="blue",
250
+ font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
251
+ ),
 
 
 
 
 
 
252
  ) as demo:
253
 
254
+ gr.Markdown(
255
+ """
256
+ <div class="hero">
257
+ FORESIGHT<span style="color:#3b82f6">.</span>
258
+ </div>
259
+ """
260
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
+ gr.Markdown(
263
+ """
264
+ <div class="sub">
265
+ Deep Intelligence Neural Analysis
266
+ </div>
267
+ """
268
+ )
269
 
270
+ with gr.Row():
271
+ with gr.Column():
272
+ with gr.Group(elem_classes="glass"):
273
+ input_image = gr.Image(type="pil", label="Source Input")
274
+ run_btn = gr.Button("RUN DIAGNOSTIC", variant="primary")
275
+
276
+ with gr.Column():
277
+ with gr.Group(elem_classes="glass"):
278
+ output_label = gr.Label(label="Classification Verdict")
279
+ output_conf = gr.Textbox(label="Confidence Rating", interactive=False)
280
+ heatmap_output = gr.Image(label="Vulnerability Visualization")
281
+
282
+ run_btn.click(
283
+ fn=predict,
284
+ inputs=input_image,
285
+ outputs=[output_label, output_conf, heatmap_output],
286
  )
287
+
288
+ if __name__ == "__main__":
289
+ demo.launch(
290
+ server_name="0.0.0.0",
291
+ server_port=int(os.getenv("PORT", "7860")),
292
+ )