Spaces:
Sleeping
Sleeping
import gradio as gr | |
import cv2 | |
import numpy as np | |
import torch | |
from transformers import AutoImageProcessor, SiglipForImageClassification | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
# Load model and processor | |
model_name = "prithivMLmods/deepfake-detector-model-v1" | |
processor = AutoImageProcessor.from_pretrained(model_name) | |
model = SiglipForImageClassification.from_pretrained(model_name) | |
model.eval() | |
# Load face detector | |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
def analyze_deepfake(video_path): | |
cap = cv2.VideoCapture(video_path) | |
frame_preds = [] | |
real_count = 0 | |
fake_count = 0 | |
frame_count = 0 | |
max_frames = 40 | |
frame_skip = 5 | |
while True: | |
ret, frame = cap.read() | |
if not ret or frame_count >= max_frames: | |
break | |
if frame_count % frame_skip != 0: | |
frame_count += 1 | |
continue | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
faces = face_detector.detectMultiScale(gray, 1.1, 4) | |
for (x, y, w, h) in faces: | |
face = frame[y:y+h, x:x+w] | |
if face.size == 0: | |
continue | |
rgb_face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) | |
image = Image.fromarray(rgb_face) | |
inputs = processor(images=image, return_tensors="pt") | |
with torch.no_grad(): | |
logits = model(**inputs).logits | |
probs = torch.softmax(logits, dim=-1)[0] | |
fake_prob = probs[1].item() | |
frame_preds.append(fake_prob) | |
if fake_prob > 0.6: | |
fake_count += 1 | |
else: | |
real_count += 1 | |
frame_count += 1 | |
cap.release() | |
# Decision logic | |
if real_count + fake_count == 0: | |
result = "β No faces detected. Try a clearer video." | |
else: | |
final_verdict = "FAKE" if fake_count > real_count else "REAL" | |
confidence = np.mean(frame_preds) | |
result = f"π― Result: **{final_verdict}** (Avg Confidence = {confidence:.2f}, Real = {real_count}, Fake = {fake_count})" | |
# Graph | |
fig, ax = plt.subplots(figsize=(6, 4)) | |
ax.hist(frame_preds, bins=10, color="green" if real_count > fake_count else "red", edgecolor="black") | |
ax.set_title("Fake Confidence per Face") | |
ax.set_xlabel("Confidence (0 = Real, 1 = Fake)") | |
ax.set_ylabel("Count") | |
ax.grid(True) | |
return result, fig | |
# Gradio UI | |
demo = gr.Interface( | |
fn=analyze_deepfake, | |
inputs=gr.Video(label="π€ Upload your .mp4 video (under 100MB)"), | |
outputs=[ | |
gr.Markdown(label="π Deepfake Detection Result"), | |
gr.Plot(label="π Confidence Distribution") | |
], | |
title="π Deepfake Video Detector (Accurate & Fast)", | |
description="This model detects faces in video frames and classifies each as REAL or FAKE using a fine-tuned vision transformer." | |
) | |
demo.launch() | |