Spaces:
Sleeping
Sleeping
File size: 2,787 Bytes
086e367 06fdfc7 f6ee5ca 42ce81c debb82a 6d0cf13 06fdfc7 6d0cf13 f6ee5ca 086e367 06fdfc7 6d0cf13 f6ee5ca 06fdfc7 6d0cf13 06fdfc7 a540a43 debb82a 06fdfc7 086e367 06fdfc7 086e367 f6ee5ca 6d0cf13 06fdfc7 6d0cf13 06fdfc7 6d0cf13 06fdfc7 6d0cf13 06fdfc7 f6ee5ca 06fdfc7 086e367 06fdfc7 a540a43 06fdfc7 bbe970a 06fdfc7 6d0cf13 06fdfc7 debb82a 61cca59 bbe970a debb82a 6d0cf13 debb82a 06fdfc7 a540a43 06fdfc7 a540a43 06fdfc7 a540a43 61cca59 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import gradio as gr
import cv2
import torch
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from transformers import AutoImageProcessor, SiglipForImageClassification
# β
Load model and processor (no manual files)
model_name = "prithivMLmods/deepfake-detector-model-v1"
processor = AutoImageProcessor.from_pretrained(model_name)
model = SiglipForImageClassification.from_pretrained(model_name)
model.eval()
# β
Face detector
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
# β
Deepfake detection function
def analyze(video_path):
if video_path is None:
return "β Please upload a video", None
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)
pil_image = Image.fromarray(face_rgb)
inputs = processor(images=pil_image, 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) # neutral prediction
frame_count += 1
cap.release()
if not frame_preds:
return "β No faces found. Try a better-quality video.", None
avg = np.mean(frame_preds)
verdict = "FAKE" if avg > 0.5 else "REAL"
result = f"β
FINAL RESULT: **{verdict}**\nπ’ Confidence: {avg:.2f}"
# β
Plot
fig, ax = plt.subplots(figsize=(6, 4))
ax.hist(frame_preds, bins=10, color="red" if avg > 0.5 else "green", 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)
return result, fig
# β
Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## π Deepfake Detector (Colab Version Converted to Gradio)")
gr.Markdown("Upload a short `.mp4` video and get a REAL or FAKE decision with confidence histogram.")
video = gr.Video(label="Upload your video")
result = gr.Markdown()
plot = gr.Plot()
button = gr.Button("π Analyze")
button.click(fn=analyze, inputs=video, outputs=[result, plot])
demo.queue().launch()
|