monster07's picture
Update app.py
f6ee5ca verified
raw
history blame
2.8 kB
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
import tempfile
import os
# βœ… Load model once
model_name = "prithivMLmods/deepfake-detector-model-v1"
processor = AutoImageProcessor.from_pretrained(model_name)
model = SiglipForImageClassification.from_pretrained(model_name)
model.eval()
# βœ… Load OpenCV Haar 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 = []
frame_count = 0
max_frames = 60
while True:
ret, frame = cap.read()
if not ret or frame_count >= max_frames:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
found = False
for (x, y, w, h) in faces:
face = frame[y:y+h, x:x+w]
if face.size == 0:
continue
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
inputs = processor(images=Image.fromarray(face_rgb), return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
fake_prob = torch.softmax(logits, dim=-1)[0][1].item()
frame_preds.append(fake_prob)
found = True
break
if not found:
frame_preds.append(0.5)
frame_count += 1
cap.release()
# Final Result
if frame_preds:
avg = np.mean(frame_preds)
verdict = "FAKE" if avg > 0.5 else "REAL"
result_text = f"βœ… FINAL RESULT: **{verdict}** (confidence: {avg:.2f})"
else:
result_text = "❌ No faces detected. Please try another video."
# Plot histogram
fig, ax = plt.subplots(figsize=(6, 4))
ax.hist(frame_preds, bins=10, color="orange", edgecolor="black")
ax.set_title("Fake Confidence per Frame")
ax.set_xlabel("Confidence (0=Real, 1=Fake)")
ax.set_ylabel("Frame Count")
ax.grid(True)
# Save plot to temp file
plot_path = os.path.join(tempfile.gettempdir(), "plot.png")
plt.savefig(plot_path)
plt.close(fig)
return result_text, plot_path
# βœ… Gradio Interface
demo = gr.Interface(
fn=analyze_deepfake,
inputs=gr.Video(label="πŸ“€ Upload a video (MP4 only)"),
outputs=[gr.Markdown(label="πŸ“Š Result"), gr.Image(type="filepath", label="πŸ“ˆ Confidence Histogram")],
title="🎭 Deepfake Video Detection App",
description="Upload a video. The model will detect faces and determine if it's REAL or FAKE using frame-level deepfake classification.",
)
demo.launch()