File size: 1,700 Bytes
086e367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
from transformers import AutoImageProcessor, SiglipForImageClassification
from PIL import Image
import torch
import cv2
import os
import uuid

# Load model
model_name = "prithivMLmods/deepfake-detector-model-v1"
processor = AutoImageProcessor.from_pretrained(model_name)
model = SiglipForImageClassification.from_pretrained(model_name)

def analyze_video(video_path):
    cap = cv2.VideoCapture(video_path)
    result_labels = []
    frame_skip = 10
    count = 0

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        if count % frame_skip == 0:
            try:
                rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                pil = Image.fromarray(rgb)
                inputs = processor(images=pil, return_tensors="pt")
                with torch.no_grad():
                    logits = model(**inputs).logits
                    pred = torch.argmax(logits, dim=1).item()
                    label = model.config.id2label[pred]
                    result_labels.append(label)
            except:
                continue
        count += 1

    cap.release()

    real = result_labels.count("REAL")
    fake = result_labels.count("FAKE")
    final = "REAL" if real > fake else "FAKE"

    return f"🟒 REAL frames: {real} | πŸ”΄ FAKE frames: {fake} β†’ Final verdict: **{final}**"

# Gradio interface
demo = gr.Interface(
    fn=analyze_video,
    inputs=gr.Video(label="Upload a video"),
    outputs=gr.Markdown(label="Result"),
    title="🎭 Deepfake Video Detector",
    description="Upload a video (MP4). The model will analyze it and return whether it's REAL or FAKE based on detected face frames."
)

demo.launch()