monster07's picture
Update app.py
6d0cf13 verified
raw
history blame
2.8 kB
import gradio as gr
import torch
import numpy as np
import cv2
import matplotlib.pyplot as plt
from PIL import Image
from transformers import AutoImageProcessor, SiglipForImageClassification
# βœ… Load model from Hugging Face (no manual files)
model_name = "prithivMLmods/deepfake-detector-model-v1"
processor = AutoImageProcessor.from_pretrained(model_name)
model = SiglipForImageClassification.from_pretrained(model_name)
model.eval()
# βœ… Haar cascade for face detection
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# βœ… Deepfake analysis logic
def detect_deepfake(video_path):
if video_path is None:
return "❌ Please upload a valid .mp4 video", None
cap = cv2.VideoCapture(video_path)
preds = []
count = 0
max_frames = 20
while True:
ret, frame = cap.read()
if not ret or count >= max_frames:
break
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] # Take first detected face
face = frame[y:y+h, x:x+w]
if face.size == 0:
continue
face_rgb = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
pil_img = Image.fromarray(face_rgb)
inputs = processor(images=pil_img, return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
prob = torch.softmax(logits, dim=-1)[0][1].item()
preds.append(prob)
count += 1
cap.release()
if not preds:
return "❌ No faces detected. Try a clearer video.", None
avg_conf = np.mean(preds)
label = "**FAKE**" if avg_conf > 0.5 else "**REAL**"
result = f"""
🎯 **Result:** {label}
πŸ”’ Avg Confidence: {avg_conf:.2f}
πŸ“Š Frames Analyzed: {len(preds)}
"""
# βœ… Create histogram
fig, ax = plt.subplots()
ax.hist(preds, bins=10, color="red" if avg_conf > 0.5 else "green", edgecolor="black")
ax.set_title("Fake Confidence per Frame")
ax.set_xlabel("Fake Probability (0 = Real, 1 = Fake)")
ax.set_ylabel("Frames")
ax.grid(True)
return result, fig
# βœ… Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## 🎭 Deepfake Detector (Transformer-based)")
gr.Markdown("Upload a short `.mp4` video. The app will detect faces and classify the video as **REAL** or **FAKE**.")
video_input = gr.Video(label="πŸ“€ Upload your video")
result_output = gr.Markdown()
graph_output = gr.Plot()
analyze_button = gr.Button("πŸ” Analyze")
analyze_button.click(fn=detect_deepfake, inputs=video_input, outputs=[result_output, graph_output])
demo.queue().launch()