Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,49 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Since this is running in Hugging Face Spaces, we'll assume the detection logic
|
| 6 |
-
# needs to be implemented here or use a simpler demo version
|
| 7 |
def detect(image):
|
| 8 |
-
"""Detect deepfake content in an image
|
| 9 |
if image is None:
|
| 10 |
raise gr.Error("Please upload an image to analyze")
|
| 11 |
|
| 12 |
try:
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
return overall, aigen, deepfake
|
| 25 |
|
| 26 |
except Exception as e:
|
| 27 |
raise gr.Error(f"Analysis error: {str(e)}")
|
| 28 |
|
| 29 |
-
# Custom CSS
|
| 30 |
custom_css = """
|
| 31 |
.container {
|
| 32 |
max-width: 1200px;
|
|
@@ -67,10 +84,12 @@ custom_css = """
|
|
| 67 |
MARKDOWN0 = """
|
| 68 |
<div class="header">
|
| 69 |
<h1>DeepFake Detection System</h1>
|
| 70 |
-
<p>Advanced AI-powered analysis for identifying manipulated media
|
|
|
|
| 71 |
</div>
|
| 72 |
"""
|
| 73 |
|
|
|
|
| 74 |
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
| 75 |
gr.Markdown(MARKDOWN0)
|
| 76 |
with gr.Row(elem_classes="container"):
|
|
@@ -88,7 +107,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
| 88 |
outputs=[overall, aigen, deepfake]
|
| 89 |
)
|
| 90 |
|
| 91 |
-
# Launch
|
| 92 |
demo.launch(
|
| 93 |
debug=True
|
| 94 |
-
)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the model and processor from Hugging Face
|
| 7 |
+
model = ViTForImageClassification.from_pretrained("dima806/deepfake_vs_real_image_detection")
|
| 8 |
+
processor = ViTImageProcessor.from_pretrained("dima806/deepfake_vs_real_image_detection")
|
| 9 |
|
|
|
|
|
|
|
| 10 |
def detect(image):
|
| 11 |
+
"""Detect deepfake content in an image using dima806/deepfake_vs_real_image_detection"""
|
| 12 |
if image is None:
|
| 13 |
raise gr.Error("Please upload an image to analyze")
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
# Convert Gradio image (filepath) to PIL Image
|
| 17 |
+
pil_image = Image.open(image).convert("RGB")
|
| 18 |
+
|
| 19 |
+
# Preprocess the image
|
| 20 |
+
inputs = processor(images=pil_image, return_tensors="pt")
|
| 21 |
+
|
| 22 |
+
# Perform inference
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
logits = outputs.logits
|
| 26 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
| 27 |
+
|
| 28 |
+
# Get confidence scores
|
| 29 |
+
probabilities = torch.softmax(logits, dim=1)[0]
|
| 30 |
+
confidence_real = probabilities[0].item() * 100
|
| 31 |
+
confidence_fake = probabilities[1].item() * 100
|
| 32 |
+
|
| 33 |
+
# Map class index to label
|
| 34 |
+
label = model.config.id2label[predicted_class]
|
| 35 |
|
| 36 |
+
# Prepare output
|
| 37 |
+
overall = f"{max(confidence_real, confidence_fake):.1f}% Confidence"
|
| 38 |
+
aigen = f"{confidence_fake:.1f}% (AI-Generated Content Likelihood)"
|
| 39 |
+
deepfake = f"{confidence_fake:.1f}% (Face Manipulation Likelihood)"
|
| 40 |
|
| 41 |
return overall, aigen, deepfake
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
raise gr.Error(f"Analysis error: {str(e)}")
|
| 45 |
|
| 46 |
+
# Custom CSS (unchanged from your original)
|
| 47 |
custom_css = """
|
| 48 |
.container {
|
| 49 |
max-width: 1200px;
|
|
|
|
| 84 |
MARKDOWN0 = """
|
| 85 |
<div class="header">
|
| 86 |
<h1>DeepFake Detection System</h1>
|
| 87 |
+
<p>Advanced AI-powered analysis for identifying manipulated media<br>
|
| 88 |
+
Powered by dima806/deepfake_vs_real_image_detection model</p>
|
| 89 |
</div>
|
| 90 |
"""
|
| 91 |
|
| 92 |
+
# Create Gradio interface
|
| 93 |
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
| 94 |
gr.Markdown(MARKDOWN0)
|
| 95 |
with gr.Row(elem_classes="container"):
|
|
|
|
| 107 |
outputs=[overall, aigen, deepfake]
|
| 108 |
)
|
| 109 |
|
| 110 |
+
# Launch the application
|
| 111 |
demo.launch(
|
| 112 |
debug=True
|
| 113 |
+
)
|