File size: 15,407 Bytes
02acebb
 
 
 
 
 
 
f455d02
 
 
 
 
 
 
 
ad19c0f
 
 
 
 
 
 
 
 
 
 
 
d57cc01
 
 
 
 
 
 
 
 
 
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9adf1b0
 
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8be8011
f455d02
 
 
f2e86ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f455d02
 
 
 
 
 
8be8011
f455d02
 
 
 
 
 
f43d3cf
 
 
 
 
0174c50
 
f455d02
f43d3cf
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0174c50
 
 
 
 
 
 
 
 
 
 
 
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f43d3cf
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
8be8011
f43d3cf
8be8011
f455d02
 
 
 
 
 
8be8011
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f43d3cf
 
 
 
 
 
 
 
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2e86ba
f455d02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8be8011
f455d02
 
 
 
d57cc01
f455d02
 
 
 
 
 
 
 
 
 
 
ad19c0f
 
f455d02
 
3ecd68e
 
 
8be8011
3ecd68e
 
 
f455d02
3ecd68e
 
ad19c0f
 
f455d02
 
 
f2e86ba
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Hugging Face ZeroGPU requires spaces to be imported before any CUDA/torch modules
try:
    import spaces
    SPACES_AVAILABLE = True
except ImportError:
    SPACES_AVAILABLE = False

import os
import sys
import time
import psutil
from PIL import Image
import torch
import gradio as gr

# Patch gradio_client schema parsing bug where boolean additionalProperties crashes get_api_info
try:
    import gradio_client.utils as gc_utils
    _orig_jstype = gc_utils._json_schema_to_python_type
    def _safe_jstype(schema, defs):
        if isinstance(schema, bool):
            return "Any"
        return _orig_jstype(schema, defs)
    gc_utils._json_schema_to_python_type = _safe_jstype
except Exception as _patch_err:
    print(f"Warning: could not patch gradio_client: {_patch_err}")

# Compatibility shim for Python 3.12+ environments where pkg_resources is deprecated/absent
try:
    import pkg_resources
except ImportError:
    import packaging
    import types
    _pr = types.ModuleType("pkg_resources")
    _pr.packaging = packaging
    sys.modules["pkg_resources"] = _pr

# Ensure UniversalFakeDetect is in Python path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
UFD_DIR = os.path.join(BASE_DIR, "UniversalFakeDetect")
if UFD_DIR not in sys.path:
    sys.path.insert(0, UFD_DIR)

from models import get_model

# Global model cache (loaded once at startup)
MODEL = None
DEVICE = None
PREPROCESS = None
LOAD_ERROR = None
LOAD_TIME_SEC = None


def load_model():
    """Load UniversalFakeDetect model and pretrained linear weights once."""
    global MODEL, DEVICE, PREPROCESS, LOAD_ERROR, LOAD_TIME_SEC
    if MODEL is not None:
        return MODEL, DEVICE, PREPROCESS

    start_t = time.perf_counter()
    try:
        # Check CUDA availability; support seamless CPU fallback
        # Default to CPU for stability and direct CPU verification
        DEVICE = torch.device("cpu")
        print(f"[UniversalFakeDetect] Target device: {DEVICE}")

        # Initialize CLIP ViT-L/14 wrapper
        # The bundled clip.load downloads ViT-L-14.pt (~933MB) to ~/.cache/clip on first run
        print("[UniversalFakeDetect] Initializing CLIP:ViT-L/14 architecture...")
        model = get_model("CLIP:ViT-L/14")

        # Load pretrained linear probe weights (fc_weights.pth, 4KB)
        ckpt_path = os.path.join(UFD_DIR, "pretrained_weights", "fc_weights.pth")
        if not os.path.exists(ckpt_path):
            raise FileNotFoundError(f"Missing pretrained weights file at: {ckpt_path}")

        print(f"[UniversalFakeDetect] Loading weights from {ckpt_path}...")
        state_dict = torch.load(ckpt_path, map_location="cpu")
        model.fc.load_state_dict(state_dict)

        # Move to target device and set to evaluation mode
        model.to(DEVICE)
        model.eval()

        MODEL = model
        PREPROCESS = model.preprocess
        LOAD_TIME_SEC = time.perf_counter() - start_t
        print(f"[UniversalFakeDetect] Successfully loaded model in {LOAD_TIME_SEC:.2f}s on {DEVICE}")
        return MODEL, DEVICE, PREPROCESS
    except Exception as e:
        LOAD_ERROR = str(e)
        print(f"[UniversalFakeDetect] Model loading error: {e}")
        return None, None, None


# Attempt initial loading at startup
load_model()


def run_inference(image):
    """
    Execute single-image inference:
    1. Input validation & RGB conversion
    2. Exact repository preprocessing
    3. Model forward pass under torch.inference_mode()
    4. Metric calculations (raw score, fake probability, confidence, latency)
    """
    # 1. Validation for empty or invalid input
    if image is None:
        return (
            "<div style='background: #fef2f2; border: 1px solid #f87171; color: #991b1b; padding: 12px; border-radius: 8px; font-weight: 500;'>⚠️ No image provided. Please upload an image to analyze.</div>",
            "",
            "Awaiting image upload."
        )

    # Safely load and validate image
    try:
        if isinstance(image, str):
            with Image.open(image) as opened_img:
                img_rgb = opened_img.convert("RGB")
        elif isinstance(image, Image.Image):
            img_rgb = image.convert("RGB")
        else:
            img_rgb = Image.fromarray(image).convert("RGB")
    except Exception as img_err:
        err_msg = f"Invalid or corrupted image file: {str(img_err)}"
        return (
            f"<div style='background: #fef2f2; border: 1.5px solid #f87171; color: #991b1b; padding: 16px; border-radius: 8px;'>❌ <b>Invalid or Corrupted Image:</b> {err_msg}</div>",
            "",
            f"Failed: {err_msg}"
        )

    # 2. Ensure model is loaded
    model, device, preprocess = load_model()
    if model is None:
        return (
            f"<div style='background: #fef2f2; border: 1px solid #f87171; color: #991b1b; padding: 12px; border-radius: 8px;'>❌ Model failed to load: {LOAD_ERROR}</div>",
            "",
            "Model unavailable."
        )

    try:
        start_time = time.perf_counter()

        # Dynamically use CUDA if available (e.g. inside @spaces.GPU) or fallback to CPU
        active_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        if next(model.parameters()).device != active_device:
            model.to(active_device)

        orig_w, orig_h = img_rgb.size

        # Apply exact preprocessing: Resize(224) -> CenterCrop(224) -> ToTensor -> Normalize
        input_tensor = preprocess(img_rgb).unsqueeze(0).to(active_device)

        # Single-image forward pass
        with torch.inference_mode():
            features = model.model.encode_image(input_tensor)
            logit_tensor = model.fc(features.float())
            raw_logit = logit_tensor.item()
            prob_fake = torch.sigmoid(logit_tensor).item()

        latency_ms = (time.perf_counter() - start_time) * 1000

        # Output label and confidence mapping
        # 0 = REAL, 1 = FAKE
        THRESHOLD = 0.50
        is_fake = prob_fake >= THRESHOLD
        label = "FAKE" if is_fake else "REAL"
        confidence = prob_fake if is_fake else (1.0 - prob_fake)
        confidence_pct = confidence * 100

        # Memory usage
        process = psutil.Process()
        rss_mb = process.memory_info().rss / (1024 * 1024)

        # Verdict card styling
        if is_fake:
            badge_color = "#ef4444"
            badge_bg = "#fef2f2"
            badge_border = "#fca5a5"
            verdict_text = "FAKE (AI-Generated / Synthetic)"
            summary_desc = "The model detected statistical patterns and feature signatures characteristic of generative models (e.g., GANs, Diffusion)."
        else:
            badge_color = "#10b981"
            badge_bg = "#ecfdf5"
            badge_border = "#6ee7b7"
            verdict_text = "REAL (Authentic Photograph)"
            summary_desc = "The model detected visual feature distributions characteristic of genuine photographic captures."

        result_html = f"""
        <div style="background: {badge_bg}; border: 1.5px solid {badge_border}; border-radius: 12px; padding: 20px; margin-bottom: 16px;">
            <div style="display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px;">
                <span style="font-size: 1.3rem; font-weight: 700; color: {badge_color};">
                    PREDICTION: {verdict_text}
                </span>
                <span style="background: {badge_color}; color: white; padding: 4px 14px; border-radius: 20px; font-weight: 600; font-size: 0.95rem;">
                    {confidence_pct:.2f}% Confidence
                </span>
            </div>
            <p style="margin-top: 10px; margin-bottom: 0; color: #374151; font-size: 0.95rem; line-height: 1.4;">
                {summary_desc}
            </p>
        </div>

        <div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 10px; padding: 16px;">
            <div style="font-weight: 600; font-size: 0.95rem; margin-bottom: 12px; color: #111827;">Inference Metrics & Diagnostics</div>
            <table style="width: 100%; border-collapse: collapse; font-size: 0.9rem; color: #374151;">
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Model Architecture:</td>
                    <td style="padding: 6px 0; text-align: right; font-family: monospace;">CLIP:ViT-L/14</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Classifier Checkpoint:</td>
                    <td style="padding: 6px 0; text-align: right; font-family: monospace;">fc_weights.pth</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Preprocessing Resolution:</td>
                    <td style="padding: 6px 0; text-align: right; font-family: monospace;">224 × 224 (Input: {orig_w} × {orig_h})</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Predicted Class:</td>
                    <td style="padding: 6px 0; text-align: right; font-weight: 600; color: {badge_color};">{label}</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Confidence:</td>
                    <td style="padding: 6px 0; text-align: right; font-weight: 600;">{confidence_pct:.2f}%</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Raw Model Logit:</td>
                    <td style="padding: 6px 0; text-align: right; font-family: monospace;">{raw_logit:+.4f}</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Fake Probability:</td>
                    <td style="padding: 6px 0; text-align: right; font-family: monospace;">{prob_fake:.4f}</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Decision Threshold:</td>
                    <td style="padding: 6px 0; text-align: right; font-family: monospace;">{THRESHOLD:.2f} (logit: 0.00)</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Processing Device:</td>
                    <td style="padding: 6px 0; text-align: right; font-weight: 500;">{str(active_device).upper()}</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Inference Latency:</td>
                    <td style="padding: 6px 0; text-align: right;">{latency_ms:.1f} ms</td>
                </tr>
                <tr>
                    <td style="padding: 6px 0; font-weight: 500;">Process Memory (RSS):</td>
                    <td style="padding: 6px 0; text-align: right;">{rss_mb:.1f} MB</td>
                </tr>
            </table>
        </div>
        """

        prob_str = f"Real: {(1.0 - prob_fake) * 100:.2f}%  |  Fake: {prob_fake * 100:.2f}%"
        status_str = f"Completed successfully on {str(active_device).upper()} in {latency_ms:.1f} ms."
        return result_html, prob_str, status_str

    except Exception as e:
        err_msg = f"Inference error: {str(e)}"
        print(f"[UniversalFakeDetect] {err_msg}")
        return (
            f"<div style='background: #fef2f2; border: 1px solid #f87171; color: #991b1b; padding: 12px; border-radius: 8px;'>❌ Error during analysis: {str(e)}</div>",
            "",
            err_msg
        )


# Custom CSS styling for modern, clean presentation
custom_css = """
.gradio-container {
    max-width: 960px !important;
    margin: auto !important;
}
.disclaimer-banner {
    background: #fffbeb;
    border: 1px solid #fef3c7;
    border-left: 4px solid #f59e0b;
    color: #92400e;
    padding: 14px 18px;
    border-radius: 8px;
    font-size: 0.92rem;
    margin-bottom: 20px;
    line-height: 1.5;
}
"""

# Apply ZeroGPU decorator if available
if SPACES_AVAILABLE:
    try:
        run_inference = spaces.GPU(duration=60)(run_inference)
    except Exception as dec_err:
        print(f"[UniversalFakeDetect] spaces.GPU decorator error: {dec_err}")

with gr.Blocks(title="UniversalFakeDetect - Single Image Analysis", css=custom_css) as demo:
    gr.Markdown(
        """
        # 🔬 UniversalFakeDetect: AI-Generated Image Detector
        ### Towards Universal Fake Image Detectors that Generalize Across Generative Models (CVPR 2023)
        Upload an individual image to evaluate whether it exhibits patterns characteristic of real photographs or synthetic AI generation.
        """
    )

    # Mandatory limitation disclaimer
    gr.HTML(
        """
        <div class="disclaimer-banner">
            <strong>⚠️ Limitation Notice:</strong>
            This detector identifies learned patterns associated with synthetic or manipulated images. It does not guarantee image authenticity.
        </div>
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            input_image = gr.Image(type="filepath", label="Upload Image for Analysis")
            with gr.Row():
                analyze_btn = gr.Button("🔍 Analyze Image", variant="primary", scale=2)
                clear_btn = gr.ClearButton(components=[input_image], scale=1)

            gr.Markdown(
                """
                **Technical Notes:**
                - Evaluates one image at a time.
                - Converts input to RGB and applies standard CLIP bicubic 224×224 normalization.
                - Features extracted using frozen OpenAI CLIP ViT-L/14 backbone with a trained linear probe.
                """
            )

        with gr.Column(scale=1):
            output_card = gr.HTML(label="Analysis Result", value="<div style='color: #6b7280; padding: 20px; text-align: center;'>Upload an image and click <b>Analyze Image</b> to begin.</div>")
            output_probs = gr.Textbox(label="Class Probability Distribution", interactive=False, max_lines=1)
            status_output = gr.Textbox(label="Processing Status", interactive=False, max_lines=1)

    with gr.Accordion("ℹ️ Model Architecture & Citation Details", open=False):
        gr.Markdown(
            r"""
            - **Model**: `UniversalFakeDetect` (CLIP:ViT-L/14 Backbone + Linear Classifier)
            - **Paper**: *Towards Universal Fake Image Detectors that Generalize Across Generative Models* (CVPR 2023)
            - **Authors**: Utkarsh Ojha*, Yuheng Li*, Yong Jae Lee (Wisconsin AI and Vision Lab)
            - **Checkpoint**: `fc_weights.pth` (trained with fixed CLIP backbone on ProGAN synthetic images)
            - **Decision Rule**: Sigmoid probability $\ge 0.50 \implies$ **FAKE**, $< 0.50 \implies$ **REAL**
            """
        )

    analyze_btn.click(
        fn=run_inference,
        inputs=[input_image],
        outputs=[output_card, output_probs, status_output],
        api_name="predict"
    )

    def clear_fields():
        return (
            "<div style='color: #6b7280; padding: 20px; text-align: center;'>Upload an image and click <b>Analyze Image</b> to begin.</div>",
            "",
            "Ready."
        )

    clear_btn.click(
        fn=clear_fields,
        inputs=[],
        outputs=[output_card, output_probs, status_output],
        api_name="clear"
    )

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