Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from torchvision import models, transforms
|
| 5 |
+
from safetensors.torch import load_file
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# 1. Rebuild the blank ResNet-50 architecture
|
| 9 |
+
model = models.resnet50(weights=None)
|
| 10 |
+
model.fc = nn.Linear(model.fc.in_features, 2) # 2 classes: Fake (0) and Real (1)
|
| 11 |
+
|
| 12 |
+
# 2. Load YOUR trained weights from the safetensors file
|
| 13 |
+
# Using map_location='cpu' ensures it works on Hugging Face's free CPU tier!
|
| 14 |
+
state_dict = load_file("model.safetensors", device="cpu")
|
| 15 |
+
|
| 16 |
+
# (Optional safety check) If you trained with DataParallel, keys might have "module." in front.
|
| 17 |
+
# This removes it so the weights load perfectly no matter what.
|
| 18 |
+
state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}
|
| 19 |
+
|
| 20 |
+
# Inject the weights into the skeleton and set to test mode
|
| 21 |
+
model.load_state_dict(state_dict)
|
| 22 |
+
model.eval()
|
| 23 |
+
|
| 24 |
+
# 3. Define the strict patch transform (No randomness!)
|
| 25 |
+
test_transform = transforms.Compose([
|
| 26 |
+
transforms.Resize(256), # Standard practice to resize slightly before center cropping
|
| 27 |
+
transforms.CenterCrop(224),
|
| 28 |
+
transforms.ToTensor(),
|
| 29 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
classes = ['FAKE', 'REAL']
|
| 33 |
+
|
| 34 |
+
# 4. Create the prediction function that Gradio will call
|
| 35 |
+
def predict_image(img):
|
| 36 |
+
if img is None:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
# Ensure image is strictly RGB (drops alpha channels from PNGs)
|
| 40 |
+
img = img.convert('RGB')
|
| 41 |
+
|
| 42 |
+
# Apply transforms and add the batch dimension
|
| 43 |
+
img_tensor = test_transform(img).unsqueeze(0)
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
# Pass through model
|
| 47 |
+
preds = model(img_tensor)
|
| 48 |
+
# Convert raw numbers to percentages (0.0 to 1.0)
|
| 49 |
+
probs = torch.nn.functional.softmax(preds[0], dim=0)
|
| 50 |
+
|
| 51 |
+
# Gradio expects a dictionary of { "Class Name": probability }
|
| 52 |
+
return {classes[i]: float(probs[i]) for i in range(2)}
|
| 53 |
+
|
| 54 |
+
# 5. Build and launch the Web App!
|
| 55 |
+
interface = gr.Interface(
|
| 56 |
+
fn=predict_image,
|
| 57 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 58 |
+
outputs=gr.Label(num_top_classes=2, label="Prediction"),
|
| 59 |
+
title="PixelSleuth: AI Image Detector 🕵️♂️",
|
| 60 |
+
description="Upload an image to see if it is a Real Photograph or AI-Generated (Fake). The model analyzes microscopic pixel artifacts to make its decision.",
|
| 61 |
+
allow_flagging="never" # Turns off the confusing "Flag" button for users
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
interface.launch()
|