-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo.py
More file actions
103 lines (79 loc) · 2.73 KB
/
Copy pathdemo.py
File metadata and controls
103 lines (79 loc) · 2.73 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
import gradio as gr
import torch
import torch.nn as nn
import numpy as np
import yt_dlp
import tempfile
import os
import dinov2_features as d2
#Load model
class MLP(nn.Module):
def __init__(self, in_dim=21, h1=64, h2=32):
super().__init__()
self.net = nn.Sequential(
nn.Linear(in_dim, h1),
nn.ReLU(),
nn.Linear(h1, h2),
nn.ReLU(),
nn.Linear(h2, 1)
)
def forward(self, x):
return self.net(x)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MLP()
model.load_state_dict(torch.load("model.pt", weights_only=True))
model.to(device)
model.eval()
mean = np.load("mean.npy")
std = np.load("std.npy")
best_tau = float(np.load("best_tau.npy"))
#Download video using yt-dlp and return path
def download_video(url):
try:
temp_dir = tempfile.mkdtemp()
outfile = os.path.join(temp_dir, "video.mp4")
ydl_opts = {
"outtmpl": outfile,
"format": "mp4/bestvideo+bestaudio/best",
"quiet": True,
"no_warnings": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
return outfile
except Exception as e:
return gr.Error(f"Download failed: {e}")
#Perform classification
def classify(video_file):
if video_file is None:
return {"No video": 1.0}
Z = d2.extract_dinov2_embeddings([video_file], device=device)
features = d2.features_from_Z(Z)
if isinstance(features, torch.Tensor):
features = features.cpu().numpy()
x = (features - mean.squeeze()) / std.squeeze()
x = x.astype(np.float32)
x_tensor = torch.from_numpy(x).unsqueeze(0).to(device)
with torch.no_grad():
logits = model(x_tensor)
prob = torch.sigmoid(logits).cpu().numpy().item()
return {"REAL": prob, "FAKE": 1 - prob}
with gr.Blocks(title="ReStrav Classifier") as demo:
gr.Markdown("### Upload a video OR paste a URL. If using a URL, the video will be downloaded and displayed below.")
with gr.Row():
with gr.Column():
video_widget = gr.Video(label="Video")
url_input = gr.Textbox(label="Paste video URL here")
download_btn = gr.Button("Load Video from URL")
with gr.Column():
output = gr.Label(label="Prediction")
classify_btn = gr.Button("Classify")
def handle_download(url):
if not url:
return gr.Error("No URL provided"), None
path = download_video(url)
return path
download_btn.click(handle_download, inputs=url_input, outputs=video_widget)
classify_btn.click(classify, inputs=video_widget, outputs=output)
if __name__ == "__main__":
demo.launch(share=True)