Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,17 @@ import numpy as np
|
|
| 4 |
import torch
|
| 5 |
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 6 |
from PIL import Image
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
# Load model and processor
|
| 10 |
model_name = "prithivMLmods/deepfake-detector-model-v1"
|
| 11 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 12 |
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
-
# Haar face detector
|
| 16 |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 17 |
|
| 18 |
-
# Inference
|
| 19 |
def detect_deepfake(video):
|
| 20 |
if video is None:
|
| 21 |
return "β Please upload a valid MP4 video."
|
|
@@ -41,7 +40,7 @@ def detect_deepfake(video):
|
|
| 41 |
faces = face_detector.detectMultiScale(gray, 1.1, 4)
|
| 42 |
|
| 43 |
if len(faces) > 0:
|
| 44 |
-
x, y, w, h = faces[0] #
|
| 45 |
face = frame[y:y+h, x:x+w]
|
| 46 |
if face.size == 0:
|
| 47 |
continue
|
|
@@ -55,6 +54,7 @@ def detect_deepfake(video):
|
|
| 55 |
logits = model(**inputs).logits
|
| 56 |
prob = torch.softmax(logits, dim=-1)[0][1].item()
|
| 57 |
frame_preds.append(prob)
|
|
|
|
| 58 |
if prob > 0.6:
|
| 59 |
fake_count += 1
|
| 60 |
else:
|
|
@@ -71,23 +71,23 @@ def detect_deepfake(video):
|
|
| 71 |
verdict = "FAKE" if fake_count > real_count else "REAL"
|
| 72 |
|
| 73 |
return f"""
|
| 74 |
-
β
**Result: {verdict}**
|
| 75 |
π’ Real Frames: {real_count}
|
| 76 |
π΄ Fake Frames: {fake_count}
|
| 77 |
π Avg Confidence: {avg_conf:.2f}
|
| 78 |
"""
|
| 79 |
|
| 80 |
-
# Gradio app using Blocks (
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
gr.Markdown("## π Fast Deepfake Video Detector")
|
| 83 |
-
gr.Markdown("Upload a short
|
| 84 |
-
|
| 85 |
with gr.Row():
|
| 86 |
video_input = gr.Video(label="π€ Upload your video")
|
| 87 |
-
result_output = gr.Markdown(label="π§
|
| 88 |
|
| 89 |
-
analyze_btn = gr.Button("Analyze Video")
|
| 90 |
|
| 91 |
analyze_btn.click(fn=detect_deepfake, inputs=video_input, outputs=result_output)
|
| 92 |
|
| 93 |
-
demo.queue(
|
|
|
|
| 4 |
import torch
|
| 5 |
from transformers import AutoImageProcessor, SiglipForImageClassification
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
+
# β
Load model and processor
|
| 9 |
model_name = "prithivMLmods/deepfake-detector-model-v1"
|
| 10 |
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 11 |
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
+
# β
Haar cascade face detector
|
| 15 |
face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 16 |
|
| 17 |
+
# β
Inference logic
|
| 18 |
def detect_deepfake(video):
|
| 19 |
if video is None:
|
| 20 |
return "β Please upload a valid MP4 video."
|
|
|
|
| 40 |
faces = face_detector.detectMultiScale(gray, 1.1, 4)
|
| 41 |
|
| 42 |
if len(faces) > 0:
|
| 43 |
+
x, y, w, h = faces[0] # Only the first face for speed
|
| 44 |
face = frame[y:y+h, x:x+w]
|
| 45 |
if face.size == 0:
|
| 46 |
continue
|
|
|
|
| 54 |
logits = model(**inputs).logits
|
| 55 |
prob = torch.softmax(logits, dim=-1)[0][1].item()
|
| 56 |
frame_preds.append(prob)
|
| 57 |
+
|
| 58 |
if prob > 0.6:
|
| 59 |
fake_count += 1
|
| 60 |
else:
|
|
|
|
| 71 |
verdict = "FAKE" if fake_count > real_count else "REAL"
|
| 72 |
|
| 73 |
return f"""
|
| 74 |
+
β
**Final Result: {verdict}**
|
| 75 |
π’ Real Frames: {real_count}
|
| 76 |
π΄ Fake Frames: {fake_count}
|
| 77 |
π Avg Confidence: {avg_conf:.2f}
|
| 78 |
"""
|
| 79 |
|
| 80 |
+
# β
Gradio app using Blocks (queue-safe)
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
gr.Markdown("## π Fast Deepfake Video Detector")
|
| 83 |
+
gr.Markdown("Upload a short `.mp4` video (under 50MB). The model analyzes faces and detects if the video is REAL or FAKE.")
|
| 84 |
+
|
| 85 |
with gr.Row():
|
| 86 |
video_input = gr.Video(label="π€ Upload your video")
|
| 87 |
+
result_output = gr.Markdown(label="π§ Detection Result")
|
| 88 |
|
| 89 |
+
analyze_btn = gr.Button("π Analyze Video")
|
| 90 |
|
| 91 |
analyze_btn.click(fn=detect_deepfake, inputs=video_input, outputs=result_output)
|
| 92 |
|
| 93 |
+
demo.queue().launch()
|