Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.transforms as T
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# NOTE FOR USER:
|
| 9 |
+
# This is a PROTOTYPE detector.
|
| 10 |
+
# It uses simple CNN-based feature heuristics + frequency analysis.
|
| 11 |
+
# It is NOT a forensic-grade deepfake detector.
|
| 12 |
+
# -----------------------------
|
| 13 |
+
|
| 14 |
+
# Image preprocessing
|
| 15 |
+
transform = T.Compose([
|
| 16 |
+
T.Resize((224, 224)),
|
| 17 |
+
T.ToTensor(),
|
| 18 |
+
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 19 |
+
])
|
| 20 |
+
|
| 21 |
+
# Simple frequency analysis (FFT-based heuristic)
|
| 22 |
+
def frequency_artifacts(image: Image.Image):
|
| 23 |
+
gray = image.convert("L")
|
| 24 |
+
arr = np.array(gray)
|
| 25 |
+
fft = np.fft.fft2(arr)
|
| 26 |
+
fft_shift = np.fft.fftshift(fft)
|
| 27 |
+
magnitude = np.log(np.abs(fft_shift) + 1)
|
| 28 |
+
score = np.mean(magnitude)
|
| 29 |
+
return score
|
| 30 |
+
|
| 31 |
+
# Main prediction function
|
| 32 |
+
def detect_authenticity(image):
|
| 33 |
+
if image is None:
|
| 34 |
+
return "No image provided", 0.0
|
| 35 |
+
|
| 36 |
+
pil_image = Image.fromarray(image)
|
| 37 |
+
freq_score = frequency_artifacts(pil_image)
|
| 38 |
+
|
| 39 |
+
# Heuristic thresholds (tunable)
|
| 40 |
+
if freq_score > 5.2:
|
| 41 |
+
label = "⚠️ Likely Manipulated / Deepfake"
|
| 42 |
+
confidence = min((freq_score - 5) * 20, 95)
|
| 43 |
+
else:
|
| 44 |
+
label = "✅ Likely Authentic"
|
| 45 |
+
confidence = min((6 - freq_score) * 20, 95)
|
| 46 |
+
|
| 47 |
+
return label, round(confidence, 2)
|
| 48 |
+
|
| 49 |
+
# Gradio UI
|
| 50 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 51 |
+
gr.Markdown("""
|
| 52 |
+
# 🕵️ Deepfake & Image Authenticity Detector
|
| 53 |
+
Upload an image to analyze whether it appears **authentic or manipulated**.
|
| 54 |
+
|
| 55 |
+
**Use cases:**
|
| 56 |
+
- Media verification
|
| 57 |
+
- Social media fact-checking
|
| 58 |
+
- Educational demos
|
| 59 |
+
- Digital forensics awareness
|
| 60 |
+
|
| 61 |
+
⚠️ *This tool uses heuristic signals and is for educational purposes.*
|
| 62 |
+
""")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column():
|
| 66 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
| 67 |
+
analyze_btn = gr.Button("Analyze Image 🔍")
|
| 68 |
+
with gr.Column():
|
| 69 |
+
result_label = gr.Textbox(label="Prediction")
|
| 70 |
+
confidence = gr.Slider(0, 100, label="Confidence (%)")
|
| 71 |
+
|
| 72 |
+
analyze_btn.click(
|
| 73 |
+
fn=detect_authenticity,
|
| 74 |
+
inputs=image_input,
|
| 75 |
+
outputs=[result_label, confidence]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
gr.Markdown("""
|
| 79 |
+
### ℹ️ How it works (Simplified)
|
| 80 |
+
- Analyzes **frequency artifacts** commonly introduced by GANs & image manipulation
|
| 81 |
+
- Detects unnatural pixel distributions
|
| 82 |
+
|
| 83 |
+
🔧 *Future upgrades you can add:*
|
| 84 |
+
- XceptionNet / EfficientNet deepfake models
|
| 85 |
+
- Face-only region detection
|
| 86 |
+
- Video deepfake detection
|
| 87 |
+
- Explainable heatmaps
|
| 88 |
+
""")
|
| 89 |
+
|
| 90 |
+
# Launch
|
| 91 |
+
demo.launch()
|