Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import cv2
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
# Load model
|
| 10 |
+
model_name = "prithivMLmods/deepfake-detector-model-v1"
|
| 11 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 12 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
def analyze_video(video_path):
|
| 15 |
+
cap = cv2.VideoCapture(video_path)
|
| 16 |
+
result_labels = []
|
| 17 |
+
frame_skip = 10
|
| 18 |
+
count = 0
|
| 19 |
+
|
| 20 |
+
while True:
|
| 21 |
+
ret, frame = cap.read()
|
| 22 |
+
if not ret:
|
| 23 |
+
break
|
| 24 |
+
if count % frame_skip == 0:
|
| 25 |
+
try:
|
| 26 |
+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 27 |
+
pil = Image.fromarray(rgb)
|
| 28 |
+
inputs = processor(images=pil, return_tensors="pt")
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
logits = model(**inputs).logits
|
| 31 |
+
pred = torch.argmax(logits, dim=1).item()
|
| 32 |
+
label = model.config.id2label[pred]
|
| 33 |
+
result_labels.append(label)
|
| 34 |
+
except:
|
| 35 |
+
continue
|
| 36 |
+
count += 1
|
| 37 |
+
|
| 38 |
+
cap.release()
|
| 39 |
+
|
| 40 |
+
real = result_labels.count("REAL")
|
| 41 |
+
fake = result_labels.count("FAKE")
|
| 42 |
+
final = "REAL" if real > fake else "FAKE"
|
| 43 |
+
|
| 44 |
+
return f"π’ REAL frames: {real} | π΄ FAKE frames: {fake} β Final verdict: **{final}**"
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=analyze_video,
|
| 49 |
+
inputs=gr.Video(label="Upload a video"),
|
| 50 |
+
outputs=gr.Markdown(label="Result"),
|
| 51 |
+
title="π Deepfake Video Detector",
|
| 52 |
+
description="Upload a video (MP4). The model will analyze it and return whether it's REAL or FAKE based on detected face frames."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
demo.launch()
|