Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,93 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
+
# ----------------------------------------------------------
|
| 9 |
+
# Load Hugging Face GenConViT Model
|
| 10 |
+
# ----------------------------------------------------------
|
| 11 |
+
model = AutoModelForImageClassification.from_pretrained(
|
| 12 |
+
"Thanuja2109/GenConViT"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
processor = AutoImageProcessor.from_pretrained(
|
| 16 |
+
"Thanuja2109/GenConViT"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
# ----------------------------------------------------------
|
| 22 |
+
# Deepfake detection function
|
| 23 |
+
# ----------------------------------------------------------
|
| 24 |
+
def detect_deepfake(video):
|
| 25 |
+
|
| 26 |
+
# Load video
|
| 27 |
+
cap = cv2.VideoCapture(video)
|
| 28 |
+
|
| 29 |
+
if not cap.isOpened():
|
| 30 |
+
return "Error: cannot open video", None
|
| 31 |
+
|
| 32 |
+
scores = []
|
| 33 |
+
frames_collected = 0
|
| 34 |
+
|
| 35 |
+
# Sample 1 frame every 10
|
| 36 |
+
frame_interval = 10
|
| 37 |
+
|
| 38 |
+
frame_img = None
|
| 39 |
+
|
| 40 |
+
i = 0
|
| 41 |
+
while True:
|
| 42 |
+
ret, frame = cap.read()
|
| 43 |
+
if not ret:
|
| 44 |
+
break
|
| 45 |
+
|
| 46 |
+
if i % frame_interval == 0:
|
| 47 |
+
# Convert to RGB
|
| 48 |
+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 49 |
+
pil_img = Image.fromarray(rgb)
|
| 50 |
+
|
| 51 |
+
inputs = processor(images=pil_img, return_tensors="pt")
|
| 52 |
+
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
logits = model(**inputs).logits
|
| 55 |
+
prob_fake = torch.softmax(logits, dim=1)[0][1].item()
|
| 56 |
+
|
| 57 |
+
scores.append(prob_fake)
|
| 58 |
+
frame_img = pil_img # save last sampled frame
|
| 59 |
+
|
| 60 |
+
i += 1
|
| 61 |
+
|
| 62 |
+
cap.release()
|
| 63 |
+
|
| 64 |
+
if len(scores) == 0:
|
| 65 |
+
return "No frames processed", None
|
| 66 |
+
|
| 67 |
+
avg_score = np.mean(scores)
|
| 68 |
+
|
| 69 |
+
label = "🔴 Deepfake" if avg_score > 0.5 else "🟢 Real"
|
| 70 |
+
|
| 71 |
+
result_text = f"""
|
| 72 |
+
### Prediction: **{label}**
|
| 73 |
+
**Confidence (fake probability): {avg_score:.4f}**
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
return result_text, frame_img
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# ----------------------------------------------------------
|
| 80 |
+
# Gradio Interface
|
| 81 |
+
# ----------------------------------------------------------
|
| 82 |
+
app = gr.Interface(
|
| 83 |
+
fn=detect_deepfake,
|
| 84 |
+
inputs=gr.Video(label="Upload a video"),
|
| 85 |
+
outputs=[
|
| 86 |
+
gr.Markdown(label="Prediction"),
|
| 87 |
+
gr.Image(label="Analyzed Frame")
|
| 88 |
+
],
|
| 89 |
+
title="GenConViT Deepfake Video Detector",
|
| 90 |
+
description="Upload a video. The app samples frames and uses GenConViT to detect deepfakes."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
app.launch()
|