Spaces:
Sleeping
Sleeping
import gradio as gr | |
import cv2 | |
import numpy as np | |
import torch | |
from transformers import AutoImageProcessor, SiglipForImageClassification | |
from PIL import Image | |
# β 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() | |
# β Haar cascade face detector | |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
# β Inference logic | |
def detect_deepfake(video): | |
if video is None: | |
return "β Please upload a valid MP4 video." | |
cap = cv2.VideoCapture(video) | |
frame_preds = [] | |
real_count = 0 | |
fake_count = 0 | |
frame_index = 0 | |
max_frames = 20 | |
frame_skip = 10 | |
while True: | |
ret, frame = cap.read() | |
if not ret or frame_index >= max_frames: | |
break | |
if frame_index % frame_skip != 0: | |
frame_index += 1 | |
continue | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
faces = face_detector.detectMultiScale(gray, 1.1, 4) | |
if len(faces) > 0: | |
x, y, w, h = faces[0] # Only the first face for speed | |
face = frame[y:y+h, x:x+w] | |
if face.size == 0: | |
continue | |
face = cv2.resize(face, (224, 224)) | |
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) | |
img = Image.fromarray(face_rgb) | |
inputs = processor(images=img, return_tensors="pt") | |
with torch.no_grad(): | |
logits = model(**inputs).logits | |
prob = torch.softmax(logits, dim=-1)[0][1].item() | |
frame_preds.append(prob) | |
if prob > 0.6: | |
fake_count += 1 | |
else: | |
real_count += 1 | |
frame_index += 1 | |
cap.release() | |
if not frame_preds: | |
return "β No faces detected. Try a different video." | |
avg_conf = np.mean(frame_preds) | |
verdict = "FAKE" if fake_count > real_count else "REAL" | |
return f""" | |
β **Final Result: {verdict}** | |
π’ Real Frames: {real_count} | |
π΄ Fake Frames: {fake_count} | |
π Avg Confidence: {avg_conf:.2f} | |
""" | |
# β Gradio app using Blocks (queue-safe) | |
with gr.Blocks() as demo: | |
gr.Markdown("## π Fast Deepfake Video Detector") | |
gr.Markdown("Upload a short `.mp4` video (under 50MB). The model analyzes faces and detects if the video is REAL or FAKE.") | |
with gr.Row(): | |
video_input = gr.Video(label="π€ Upload your video") | |
result_output = gr.Markdown(label="π§ Detection Result") | |
analyze_btn = gr.Button("π Analyze Video") | |
analyze_btn.click(fn=detect_deepfake, inputs=video_input, outputs=result_output) | |
demo.queue().launch() | |