anyantudre
commited on
Commit
·
44efd89
1
Parent(s):
abc6589
update app.py for dowload from HF
Browse files- README.md +12 -0
- app.py +13 -10
- app_local.py +132 -0
- .gitattributes → gitattributes +0 -0
- requirements.txt +4 -21
README.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: DeepFake Videos Detection
|
3 |
+
emoji: 😻
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.9.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -8,6 +8,7 @@ from tqdm import tqdm
|
|
8 |
from training.detectors import DETECTOR
|
9 |
import yaml
|
10 |
import gradio as gr
|
|
|
11 |
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
|
@@ -17,7 +18,7 @@ AVAILABLE_MODELS = [
|
|
17 |
"ucf",
|
18 |
]
|
19 |
|
20 |
-
# load the model
|
21 |
def load_model(model_name, config_path, weights_path):
|
22 |
with open(config_path, 'r') as f:
|
23 |
config = yaml.safe_load(f)
|
@@ -55,9 +56,8 @@ def preprocess_video(video_path, output_dir, frame_num=32):
|
|
55 |
cap.release()
|
56 |
return frames
|
57 |
|
58 |
-
#
|
59 |
def infer_video(video_path, model, device):
|
60 |
-
# Preprocess the video
|
61 |
output_dir = "temp_video_frames"
|
62 |
frames = preprocess_video(video_path, output_dir)
|
63 |
|
@@ -90,18 +90,21 @@ def infer_video(video_path, model, device):
|
|
90 |
prediction = "Fake" if avg_prob > 0.5 else "Real"
|
91 |
return prediction, avg_prob
|
92 |
|
93 |
-
#
|
94 |
def gradio_inference(video, model_name):
|
95 |
-
|
96 |
-
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
return f"Error: Weights file for model '{model_name}' not found at {weights_path}."
|
102 |
|
|
|
103 |
model = load_model(model_name, config_path, weights_path)
|
104 |
|
|
|
105 |
prediction, confidence = infer_video(video, model, device)
|
106 |
return f"Model: {model_name}\nPrediction: {prediction} (Confidence: {confidence:.4f})"
|
107 |
|
|
|
8 |
from training.detectors import DETECTOR
|
9 |
import yaml
|
10 |
import gradio as gr
|
11 |
+
from huggingface_hub import hf_hub_download
|
12 |
|
13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
|
|
|
18 |
"ucf",
|
19 |
]
|
20 |
|
21 |
+
# load the model from HF Model Registry
|
22 |
def load_model(model_name, config_path, weights_path):
|
23 |
with open(config_path, 'r') as f:
|
24 |
config = yaml.safe_load(f)
|
|
|
56 |
cap.release()
|
57 |
return frames
|
58 |
|
59 |
+
# inference on a single video
|
60 |
def infer_video(video_path, model, device):
|
|
|
61 |
output_dir = "temp_video_frames"
|
62 |
frames = preprocess_video(video_path, output_dir)
|
63 |
|
|
|
90 |
prediction = "Fake" if avg_prob > 0.5 else "Real"
|
91 |
return prediction, avg_prob
|
92 |
|
93 |
+
# Gradio inference function
|
94 |
def gradio_inference(video, model_name):
|
95 |
+
# Download config and weights from Hugging Face Model Registry
|
96 |
+
repo_id = "ArissBandoss/deepfake-video-classifier"
|
97 |
+
config_filename = f"{model_name}.yaml"
|
98 |
+
weights_filename = f"{model_name}_best.pth"
|
99 |
|
100 |
+
# Download files
|
101 |
+
config_path = hf_hub_download(repo_id=repo_id, filename=config_filename)
|
102 |
+
weights_path = hf_hub_download(repo_id=repo_id, filename=weights_filename)
|
|
|
103 |
|
104 |
+
# Load the model
|
105 |
model = load_model(model_name, config_path, weights_path)
|
106 |
|
107 |
+
# Run inference
|
108 |
prediction, confidence = infer_video(video, model, device)
|
109 |
return f"Model: {model_name}\nPrediction: {prediction} (Confidence: {confidence:.4f})"
|
110 |
|
app_local.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from torchvision import transforms
|
6 |
+
from PIL import Image
|
7 |
+
from tqdm import tqdm
|
8 |
+
from training.detectors import DETECTOR
|
9 |
+
import yaml
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
|
14 |
+
# available models in the repository
|
15 |
+
AVAILABLE_MODELS = [
|
16 |
+
"xception",
|
17 |
+
"ucf",
|
18 |
+
]
|
19 |
+
|
20 |
+
# load the model
|
21 |
+
def load_model(model_name, config_path, weights_path):
|
22 |
+
with open(config_path, 'r') as f:
|
23 |
+
config = yaml.safe_load(f)
|
24 |
+
|
25 |
+
config['model_name'] = model_name
|
26 |
+
|
27 |
+
model_class = DETECTOR[model_name]
|
28 |
+
model = model_class(config).to(device)
|
29 |
+
|
30 |
+
checkpoint = torch.load(weights_path, map_location=device)
|
31 |
+
model.load_state_dict(checkpoint, strict=True)
|
32 |
+
model.eval()
|
33 |
+
return model
|
34 |
+
|
35 |
+
# preprocess a single video
|
36 |
+
def preprocess_video(video_path, output_dir, frame_num=32):
|
37 |
+
os.makedirs(output_dir, exist_ok=True)
|
38 |
+
frames_dir = os.path.join(output_dir, "frames")
|
39 |
+
os.makedirs(frames_dir, exist_ok=True)
|
40 |
+
|
41 |
+
cap = cv2.VideoCapture(video_path)
|
42 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
43 |
+
frame_indices = np.linspace(0, total_frames - 1, frame_num, dtype=int)
|
44 |
+
|
45 |
+
# extract frames
|
46 |
+
frames = []
|
47 |
+
for idx in frame_indices:
|
48 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
|
49 |
+
ret, frame = cap.read()
|
50 |
+
if ret:
|
51 |
+
frame_path = os.path.join(frames_dir, f"frame_{idx:04d}.png")
|
52 |
+
cv2.imwrite(frame_path, frame)
|
53 |
+
frames.append(frame_path)
|
54 |
+
|
55 |
+
cap.release()
|
56 |
+
return frames
|
57 |
+
|
58 |
+
# inference on a single video
|
59 |
+
def infer_video(video_path, model, device):
|
60 |
+
# Preprocess the video
|
61 |
+
output_dir = "temp_video_frames"
|
62 |
+
frames = preprocess_video(video_path, output_dir)
|
63 |
+
|
64 |
+
transform = transforms.Compose([
|
65 |
+
transforms.Resize((256, 256)),
|
66 |
+
transforms.ToTensor(),
|
67 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
68 |
+
])
|
69 |
+
|
70 |
+
probs = []
|
71 |
+
for frame_path in frames:
|
72 |
+
frame = Image.open(frame_path).convert("RGB")
|
73 |
+
frame = transform(frame).unsqueeze(0).to(device)
|
74 |
+
|
75 |
+
data_dict = {
|
76 |
+
"image": frame,
|
77 |
+
"label": torch.tensor([0]).to(device), # Dummy label
|
78 |
+
"label_spe": torch.tensor([0]).to(device), # Dummy specific label
|
79 |
+
}
|
80 |
+
|
81 |
+
with torch.no_grad():
|
82 |
+
pred_dict = model(data_dict, inference=True)
|
83 |
+
|
84 |
+
logits = pred_dict["cls"] # Shape: [batch_size, num_classes]
|
85 |
+
prob = torch.softmax(logits, dim=1)[:, 1].item() # Probability of being "fake"
|
86 |
+
probs.append(prob)
|
87 |
+
|
88 |
+
# aggregate predictions (e.g., average probability)
|
89 |
+
avg_prob = np.mean(probs)
|
90 |
+
prediction = "Fake" if avg_prob > 0.5 else "Real"
|
91 |
+
return prediction, avg_prob
|
92 |
+
|
93 |
+
# gradio inference function
|
94 |
+
def gradio_inference(video, model_name):
|
95 |
+
config_path = f"/teamspace/studios/this_studio/DeepfakeBench/training/config/detector/{model_name}.yaml"
|
96 |
+
weights_path = f"/teamspace/studios/this_studio/DeepfakeBench/training/weights/{model_name}_best.pth"
|
97 |
+
|
98 |
+
if not os.path.exists(config_path):
|
99 |
+
return f"Error: Config file for model '{model_name}' not found at {config_path}."
|
100 |
+
if not os.path.exists(weights_path):
|
101 |
+
return f"Error: Weights file for model '{model_name}' not found at {weights_path}."
|
102 |
+
|
103 |
+
model = load_model(model_name, config_path, weights_path)
|
104 |
+
|
105 |
+
prediction, confidence = infer_video(video, model, device)
|
106 |
+
return f"Model: {model_name}\nPrediction: {prediction} (Confidence: {confidence:.4f})"
|
107 |
+
|
108 |
+
# Gradio App
|
109 |
+
def create_gradio_app():
|
110 |
+
with gr.Blocks() as demo:
|
111 |
+
gr.Markdown("# Deepfake Detection Demo")
|
112 |
+
gr.Markdown("Upload a video and select a model to detect if it's real or fake.")
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
video_input = gr.Video(label="Upload Video")
|
116 |
+
model_dropdown = gr.Dropdown(choices=AVAILABLE_MODELS, label="Select Model", value="xception")
|
117 |
+
|
118 |
+
output_text = gr.Textbox(label="Prediction Result")
|
119 |
+
|
120 |
+
submit_button = gr.Button("Run Inference")
|
121 |
+
submit_button.click(
|
122 |
+
fn=gradio_inference,
|
123 |
+
inputs=[video_input, model_dropdown],
|
124 |
+
outputs=output_text,
|
125 |
+
)
|
126 |
+
|
127 |
+
return demo
|
128 |
+
|
129 |
+
|
130 |
+
if __name__ == "__main__":
|
131 |
+
demo = create_gradio_app()
|
132 |
+
demo.launch(share=True)
|
.gitattributes → gitattributes
RENAMED
File without changes
|
requirements.txt
CHANGED
@@ -1,33 +1,16 @@
|
|
|
|
1 |
numpy==1.21.5
|
2 |
-
pandas==1.4.2
|
3 |
Pillow==9.0.1
|
4 |
-
|
5 |
-
imageio==2.9.0
|
6 |
-
imgaug==0.4.0
|
7 |
tqdm==4.61.0
|
8 |
-
scipy==1.7.3
|
9 |
-
seaborn==0.11.2
|
10 |
pyyaml==6.0
|
11 |
-
|
12 |
-
opencv-python==4.6.0.66
|
13 |
scikit-image==0.19.2
|
14 |
scikit-learn==1.0.2
|
15 |
albumentations==1.1.0
|
16 |
torch==1.12.0
|
17 |
torchvision==0.13.0
|
18 |
-
torchaudio==0.12.0
|
19 |
efficientnet-pytorch==0.7.1
|
20 |
timm==0.6.12
|
21 |
-
segmentation-models-pytorch==0.3.2
|
22 |
-
torchtoolbox==0.1.8.2
|
23 |
-
tensorboard==2.10.1
|
24 |
-
setuptools==59.5.0
|
25 |
-
loralib
|
26 |
-
einops
|
27 |
transformers
|
28 |
-
|
29 |
-
simplejson
|
30 |
-
kornia
|
31 |
-
fvcore
|
32 |
-
imgaug==0.4.0
|
33 |
-
git+https://github.com/openai/CLIP.git
|
|
|
1 |
+
huggingface_hub
|
2 |
numpy==1.21.5
|
|
|
3 |
Pillow==9.0.1
|
4 |
+
opencv-python==4.6.0.66
|
|
|
|
|
5 |
tqdm==4.61.0
|
|
|
|
|
6 |
pyyaml==6.0
|
7 |
+
scipy==1.7.3
|
|
|
8 |
scikit-image==0.19.2
|
9 |
scikit-learn==1.0.2
|
10 |
albumentations==1.1.0
|
11 |
torch==1.12.0
|
12 |
torchvision==0.13.0
|
|
|
13 |
efficientnet-pytorch==0.7.1
|
14 |
timm==0.6.12
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
transformers
|
16 |
+
gradio
|
|
|
|
|
|
|
|
|
|