Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,31 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
"dima806/deepfake_vs_real_image_detection",
|
| 9 |
-
"BuzzFeedNews/Deepfake-Detection",
|
| 10 |
-
"umarlai/deepfake-detection-vit"
|
| 11 |
-
]
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
probs_list = []
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Map outputs depending on model’s labels
|
| 31 |
-
labels = model.config.id2label
|
| 32 |
-
result = {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
if fake_score > real_score:
|
| 41 |
-
votes["Fake"] += 1
|
| 42 |
-
else:
|
| 43 |
-
votes["Real"] += 1
|
| 44 |
-
|
| 45 |
-
# Majority voting
|
| 46 |
-
final_label = "Fake" if votes["Fake"] > votes["Real"] else "Real"
|
| 47 |
-
|
| 48 |
-
# Average probability
|
| 49 |
-
avg_real = sum([p["Real"] for p in probs_list]) / len(probs_list)
|
| 50 |
-
avg_fake = sum([p["Fake"] for p in probs_list]) / len(probs_list)
|
| 51 |
-
|
| 52 |
-
return {final_label: max(avg_real, avg_fake), "Real": avg_real, "Fake": avg_fake}
|
| 53 |
-
|
| 54 |
-
# UI
|
| 55 |
-
demo = gr.Interface(
|
| 56 |
-
fn=predict,
|
| 57 |
-
inputs=gr.Image(type="pil"),
|
| 58 |
-
outputs="label",
|
| 59 |
-
title="🕵️ Deepfake Detector (Ensemble)",
|
| 60 |
-
description="Uploads an image and checks if it's REAL or FAKE using 3 different models combined."
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
| 65 |
-
|
|
|
|
| 1 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# ✅ Use a valid Hugging Face model repo
|
| 6 |
+
MODEL_NAME = "selimsef/dfdc_deepfake_challenge"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load processor & model
|
| 9 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
|
| 11 |
|
| 12 |
+
# Load your image
|
| 13 |
+
image_path = "your_image.jpg" # replace with your raw image path
|
| 14 |
+
image = Image.open(image_path).convert("RGB")
|
| 15 |
|
| 16 |
+
# Preprocess
|
| 17 |
+
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
| 18 |
|
| 19 |
+
# Inference
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
logits = outputs.logits
|
| 23 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Get labels
|
| 26 |
+
labels = model.config.id2label
|
| 27 |
+
real_prob = probs[labels["0"]] if "0" in labels else probs[0]
|
| 28 |
+
fake_prob = probs[labels["1"]] if "1" in labels else probs[1]
|
| 29 |
|
| 30 |
+
print(f"Prediction: Real={real_prob:.4f}, Fake={fake_prob:.4f}")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|