Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Load model and processor
|
| 9 |
+
model_name = "prithivMLmods/deepfake-detector-model-v1"
|
| 10 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
# Updated label mapping
|
| 14 |
+
id2label = {
|
| 15 |
+
"0": "fake",
|
| 16 |
+
"1": "real"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def classify_image(image):
|
| 20 |
+
image = Image.fromarray(image).convert("RGB")
|
| 21 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
logits = outputs.logits
|
| 26 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
| 27 |
+
|
| 28 |
+
prediction = {
|
| 29 |
+
id2label[str(i)]: round(probs[i], 3) for i in range(len(probs))
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return prediction
|
| 33 |
+
|
| 34 |
+
def sliceFrames(cap):
|
| 35 |
+
frame_count = 0
|
| 36 |
+
frames = []
|
| 37 |
+
|
| 38 |
+
while True:
|
| 39 |
+
ret, frame = cap.read()
|
| 40 |
+
if not ret:
|
| 41 |
+
break
|
| 42 |
+
# Save every 10th frame
|
| 43 |
+
if frame_count % 10 == 0:
|
| 44 |
+
frames.append(frame)
|
| 45 |
+
frame_count += 1
|
| 46 |
+
|
| 47 |
+
cap.release()
|
| 48 |
+
return frames
|
| 49 |
+
|
| 50 |
+
def classify_video(video_path):
|
| 51 |
+
cap = cv2.VideoCapture(video_path)
|
| 52 |
+
if not cap.isOpened():
|
| 53 |
+
return {"error": "Could not open video."}
|
| 54 |
+
|
| 55 |
+
frames = sliceFrames(cap)
|
| 56 |
+
totalfake = 0
|
| 57 |
+
totalreal = 0
|
| 58 |
+
|
| 59 |
+
for frame in frames:
|
| 60 |
+
prediction = classify_image(frame)
|
| 61 |
+
totalfake += prediction["fake"]
|
| 62 |
+
totalreal += prediction["real"]
|
| 63 |
+
|
| 64 |
+
avg_fake = totalfake / len(frames) if frames else 0
|
| 65 |
+
avg_real = totalreal / len(frames) if frames else 0
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
"average_fake": round(avg_fake, 3),
|
| 69 |
+
"average_real": round(avg_real, 3),
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
# Gradio Interface
|
| 73 |
+
def gradio_interface(video_file):
|
| 74 |
+
return classify_video(video_file)
|
| 75 |
+
|
| 76 |
+
iface = gr.Interface(
|
| 77 |
+
fn=gradio_interface,
|
| 78 |
+
inputs=gr.Video(label="Upload a video"),
|
| 79 |
+
outputs=gr.JSON(label="Prediction"),
|
| 80 |
+
title="Deepfake Detector",
|
| 81 |
+
description="Upload a video to check if it's real or fake."
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
iface.launch()
|