File size: 5,991 Bytes
ca71be4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f21b7
 
ca71be4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

import os
import gradio as gr
import torch
import numpy as np
import cv2
from PIL import Image
from transformers import ViTImageProcessor, ViTForImageClassification
# from transformers import AutoModelForImageClassification, AutoImageProcessor

# -----------------------------
# CONFIGURATION
# -----------------------------

MODEL_REPO = "SARVM/ViT_Deepfake"
HF_TOKEN = os.getenv("HF_TOKEN")  # Set in Space secrets or local env
# HF_TOKEN = "hf_xxxxxxxxxxxxxxxxxxxxxxxx"  # 🔐 Replace with your actual Hugging Face token

print(f"Loading model from {MODEL_REPO}...")

processor = ViTImageProcessor.from_pretrained(
    MODEL_REPO,
    token=HF_TOKEN
)

model = ViTForImageClassification.from_pretrained(
    MODEL_REPO,
    token=HF_TOKEN,
    output_attentions=True
)

# processor = AutoImageProcessor.from_pretrained(
#     MODEL_REPO,
#     token=HF_TOKEN
# )

# model = AutoModelForImageClassification.from_pretrained(
#     MODEL_REPO,
#     token=HF_TOKEN
# )

model.eval()

# Override labels to REAL / FAKE
model.config.id2label = {
    1: "REAL",
    0: "FAKE"
}

model.config.label2id = {
    "REAL": 1,
    "FAKE": 0
}

# -----------------------------
# ATTENTION ROLLOUT
# -----------------------------

def compute_attention_rollout(attentions):
    att_mat = torch.stack(attentions).squeeze(1)
    att_mat = att_mat.mean(dim=1)

    residual_att = torch.eye(att_mat.size(-1))
    aug_att_mat = att_mat + residual_att
    aug_att_mat = aug_att_mat / aug_att_mat.sum(dim=-1).unsqueeze(-1)

    joint_attentions = torch.zeros_like(aug_att_mat)
    joint_attentions[0] = aug_att_mat[0]

    for n in range(1, aug_att_mat.size(0)):
        joint_attentions[n] = aug_att_mat[n] @ joint_attentions[n - 1]

    return joint_attentions[-1]


# -----------------------------
# PREDICTION FUNCTION
# -----------------------------

def predict(image):
    if image is None:
        return None, None, None

    inputs = processor(images=image, return_tensors="pt")

    with torch.no_grad():
        outputs = model(**inputs, output_attentions=True)
        logits = outputs.logits
        attentions = outputs.attentions

    probs = torch.nn.functional.softmax(logits, dim=-1)
    confidence, predicted_class_idx = torch.max(probs, dim=-1)

    prediction = model.config.id2label[predicted_class_idx.item()]
    confidence_pct = round(confidence.item() * 100, 2)

    # Attention rollout
    rollout = compute_attention_rollout(attentions)

    mask = rollout[0, 1:]
    size = int(mask.shape[0] ** 0.5)
    mask = mask.reshape(size, size).cpu().numpy()

    mask = cv2.resize(mask, image.size)
    mask = (mask - mask.min()) / (mask.max() - mask.min() + 1e-8)

    heatmap = cv2.applyColorMap(
        np.uint8(255 * mask),
        cv2.COLORMAP_JET
    )

    overlay = cv2.addWeighted(
        np.array(image),
        0.6,
        heatmap,
        0.4,
        0
    )

    return prediction, f"{confidence_pct}%", overlay


# -----------------------------
# UI DESIGN
# -----------------------------

custom_css = """
/* Professional Adaptive Theme */
:root {
    --primary-blue: #2563eb;
    --hero-text: #0f172a; /* Dark slate for light mode */
}

.dark {
    --hero-text: #f8fafc; /* White for dark mode */
}

/* Background refinement */
body {
    background-color: var(--background-fill-primary);
}

/* Adaptive Typography */
.hero {
    text-align: center;
    font-family: 'Inter', sans-serif;
    font-size: 48px;
    font-weight: 800;
    letter-spacing: -0.04em;
    margin-top: 50px;
    /* This variable handles the visibility toggle */
    color: var(--hero-text) !important;
}

.sub {
    text-align: center;
    opacity: 0.7;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    margin-bottom: 40px;
    color: var(--body-text-color);
}

/* Professional Container Styling */
.glass {
    background: var(--block-background-fill) !important;
    border: 1px solid var(--border-color-primary) !important;
    border-radius: 12px !important;
    padding: 24px !important;
    box-shadow: var(--block-shadow);
    transition: all 0.2s ease;
}

.glass:hover {
    border-color: var(--primary-blue) !important;
    box-shadow: 0 4px 20px rgba(37, 99, 235, 0.1);
}

/* Enterprise Button */
button.primary {
    background: var(--primary-blue) !important;
    color: white !important;
    border: none !important;
    font-weight: 600 !important;
    padding: 12px 24px !important;
    border-radius: 8px !important;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2) !important;
}

button.primary:hover {
    background: #1d4ed8 !important;
    transform: translateY(-1px);
    box-shadow: 0 6px 16px rgba(37, 99, 235, 0.3) !important;
}

/* Label & Input tweaks for clarity */
.gr-label {
    font-weight: 600 !important;
    font-size: 12px !important;
    text-transform: uppercase;
    color: var(--primary-blue) !important;
}
"""

with gr.Blocks(
    css=custom_css,
    theme=gr.themes.Soft(
        primary_hue="blue", 
        font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"]
    )
) as demo:

    gr.Markdown(f"<div class='hero'>FORESIGHT<span style='color:#3b82f6'>.</span></div>")
    gr.Markdown("<div class='sub'>Deep Intelligence Neural Analysis</div>")

    with gr.Row():
        with gr.Column():
            with gr.Group(elem_classes="glass"):
                input_image = gr.Image(type="pil", label="Source Input")
                run_btn = gr.Button("RUN DIAGNOSTIC", variant="primary")

        with gr.Column():
            with gr.Group(elem_classes="glass"):
                output_label = gr.Label(label="Classification Verdict")
                output_conf = gr.Textbox(label="Confidence Rating", interactive=False)
                heatmap_output = gr.Image(label="Vulnerability Visualization")

    run_btn.click(
        fn=predict,
        inputs=input_image,
        outputs=[output_label, output_conf, heatmap_output]
    )

if __name__ == "__main__":
    demo.launch()