Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,65 @@
|
|
| 1 |
-
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 2 |
-
from PIL import Image
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
with torch.no_grad():
|
| 15 |
-
outputs = model(**inputs)
|
| 16 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 17 |
-
id2label = { "0": "Fake", "1": "Real" }
|
| 18 |
-
return { id2label[str(i)]: float(probs[i]) for i in range(2) }
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
demo = gr.Interface(
|
| 21 |
-
fn=
|
| 22 |
-
inputs=gr.Image(type="
|
| 23 |
-
outputs=
|
| 24 |
-
title="Deepfake Detector",
|
| 25 |
-
description="
|
| 26 |
)
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
demo.launch()
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load multiple models
|
| 7 |
+
model_names = [
|
| 8 |
+
"dima806/deepfake_vs_real_image_detection",
|
| 9 |
+
"BuzzFeedNews/Deepfake-Detection",
|
| 10 |
+
"umarlai/deepfake-detection-vit"
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
models = []
|
| 14 |
+
processors = []
|
| 15 |
+
|
| 16 |
+
for name in model_names:
|
| 17 |
+
processors.append(AutoImageProcessor.from_pretrained(name))
|
| 18 |
+
models.append(AutoModelForImageClassification.from_pretrained(name))
|
| 19 |
+
|
| 20 |
+
def predict(image):
|
| 21 |
+
votes = {"Real": 0, "Fake": 0}
|
| 22 |
+
probs_list = []
|
| 23 |
+
|
| 24 |
+
for model, processor in zip(models, processors):
|
| 25 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
outputs = model(**inputs)
|
| 28 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 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 |
+
# Normalize labels to Real/Fake
|
| 35 |
+
real_score = result.get("Real", result.get("REAL", result.get("0", 0)))
|
| 36 |
+
fake_score = result.get("Fake", result.get("FAKE", result.get("1", 0)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
probs_list.append({"Real": real_score, "Fake": fake_score})
|
| 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 |
+
|