# 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 ( "
⚠️ No image provided. Please upload an image to analyze.
", "", "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"
❌ Invalid or Corrupted Image: {err_msg}
", "", f"Failed: {err_msg}" ) # 2. Ensure model is loaded model, device, preprocess = load_model() if model is None: return ( f"
❌ Model failed to load: {LOAD_ERROR}
", "", "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"""
PREDICTION: {verdict_text} {confidence_pct:.2f}% Confidence

{summary_desc}

Inference Metrics & Diagnostics
Model Architecture: CLIP:ViT-L/14
Classifier Checkpoint: fc_weights.pth
Preprocessing Resolution: 224 × 224 (Input: {orig_w} × {orig_h})
Predicted Class: {label}
Confidence: {confidence_pct:.2f}%
Raw Model Logit: {raw_logit:+.4f}
Fake Probability: {prob_fake:.4f}
Decision Threshold: {THRESHOLD:.2f} (logit: 0.00)
Processing Device: {str(active_device).upper()}
Inference Latency: {latency_ms:.1f} ms
Process Memory (RSS): {rss_mb:.1f} MB
""" 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"
❌ Error during analysis: {str(e)}
", "", 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( """
⚠️ Limitation Notice: This detector identifies learned patterns associated with synthetic or manipulated images. It does not guarantee image authenticity.
""" ) 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="
Upload an image and click Analyze Image to begin.
") 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 ( "
Upload an image and click Analyze Image to begin.
", "", "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)