mehal commited on
Commit
0fb1163
1 Parent(s): aaaa0f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +168 -0
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import AutoProcessor, AutoModel
4
+ from utils import (
5
+ convert_frames_to_gif,
6
+ download_youtube_video,
7
+ get_num_total_frames,
8
+ sample_frames_from_video_file,
9
+ )
10
+
11
+ FRAME_SAMPLING_RATE = 4
12
+ DEFAULT_MODEL = "microsoft/xclip-base-patch16-zero-shot"
13
+
14
+ VALID_ZEROSHOT_VIDEOCLASSIFICATION_MODELS = [
15
+ "microsoft/xclip-base-patch32",
16
+ "microsoft/xclip-base-patch16-zero-shot",
17
+ "microsoft/xclip-base-patch16-kinetics-600",
18
+ "microsoft/xclip-large-patch14ft/xclip-base-patch32-16-frames",
19
+ "microsoft/xclip-large-patch14",
20
+ "microsoft/xclip-base-patch16-hmdb-4-shot",
21
+ "microsoft/xclip-base-patch16-16-frames",
22
+ "microsoft/xclip-base-patch16-hmdb-2-shot",
23
+ "microsoft/xclip-base-patch16-ucf-2-shot",
24
+ "microsoft/xclip-base-patch16-ucf-8-shot",
25
+ "microsoft/xclip-base-patch16",
26
+ "microsoft/xclip-base-patch16-hmdb-8-shot",
27
+ "microsoft/xclip-base-patch16-hmdb-16-shot",
28
+ "microsoft/xclip-base-patch16-ucf-16-shot",
29
+ ]
30
+
31
+ processor = AutoProcessor.from_pretrained(DEFAULT_MODEL)
32
+ model = AutoModel.from_pretrained(DEFAULT_MODEL)
33
+
34
+ examples = [
35
+ [
36
+ "https://www.youtu.be/l1dBM8ZECao",
37
+ "sleeping dog,cat fight club,birds of prey",
38
+ ],
39
+ [
40
+ "https://youtu.be/VMj-3S1tku0",
41
+ "programming course,eating spaghetti,playing football",
42
+ ],
43
+ [
44
+ "https://youtu.be/BRw7rvLdGzU",
45
+ "game of thrones,the lord of the rings,vikings",
46
+ ],
47
+ ]
48
+
49
+
50
+ def select_model(model_name):
51
+ global processor, model
52
+ processor = AutoProcessor.from_pretrained(model_name)
53
+ model = AutoModel.from_pretrained(model_name)
54
+
55
+
56
+ def predict(youtube_url_or_file_path, labels_text):
57
+
58
+ if youtube_url_or_file_path.startswith("http"):
59
+ video_path = download_youtube_video(youtube_url_or_file_path)
60
+ else:
61
+ video_path = youtube_url_or_file_path
62
+
63
+ # rearrange sampling rate based on video length and model input length
64
+ num_total_frames = get_num_total_frames(video_path)
65
+ num_model_input_frames = model.config.vision_config.num_frames
66
+ if num_total_frames < FRAME_SAMPLING_RATE * num_model_input_frames:
67
+ frame_sampling_rate = num_total_frames // num_model_input_frames
68
+ else:
69
+ frame_sampling_rate = FRAME_SAMPLING_RATE
70
+
71
+ labels = labels_text.split(",")
72
+
73
+ frames = sample_frames_from_video_file(
74
+ video_path, num_model_input_frames, frame_sampling_rate
75
+ )
76
+ gif_path = convert_frames_to_gif(frames, save_path="video.gif")
77
+
78
+ inputs = processor(
79
+ text=labels, videos=list(frames), return_tensors="pt", padding=True
80
+ )
81
+ # forward pass
82
+ with torch.no_grad():
83
+ outputs = model(**inputs)
84
+
85
+ probs = outputs.logits_per_video[0].softmax(dim=-1).cpu().numpy()
86
+ label_to_prob = {}
87
+ for ind, label in enumerate(labels):
88
+ label_to_prob[label] = float(probs[ind])
89
+
90
+ return label_to_prob, gif_path
91
+
92
+
93
+ app = gr.Blocks()
94
+ with app:
95
+ gr.Markdown(
96
+ "# **<p align='center'>Zero-shot Video Classification with 🤗 Transformers</p>**"
97
+ )
98
+ gr.Markdown(
99
+ """
100
+ <p style='text-align: center'>
101
+ Follow me for more!
102
+ <br> <a href='https://twitter.com/fcakyon' target='_blank'>twitter</a> | <a href='https://github.com/fcakyon' target='_blank'>github</a> | <a href='https://www.linkedin.com/in/fcakyon/' target='_blank'>linkedin</a> | <a href='https://fcakyon.medium.com/' target='_blank'>medium</a>
103
+ </p>
104
+ """
105
+ )
106
+
107
+ with gr.Row():
108
+ with gr.Column():
109
+ model_names_dropdown = gr.Dropdown(
110
+ choices=VALID_ZEROSHOT_VIDEOCLASSIFICATION_MODELS,
111
+ label="Model:",
112
+ show_label=True,
113
+ value=DEFAULT_MODEL,
114
+ )
115
+ model_names_dropdown.change(fn=select_model, inputs=model_names_dropdown)
116
+ with gr.Tab(label="Youtube URL"):
117
+ gr.Markdown(
118
+ "### **Provide a Youtube video URL and a list of labels separated by commas**"
119
+ )
120
+ youtube_url = gr.Textbox(label="Youtube URL:", show_label=True)
121
+ youtube_url_labels_text = gr.Textbox(
122
+ label="Labels Text:", show_label=True
123
+ )
124
+ youtube_url_predict_btn = gr.Button(value="Predict")
125
+ with gr.Tab(label="Local File"):
126
+ gr.Markdown(
127
+ "### **Upload a video file and provide a list of labels separated by commas**"
128
+ )
129
+ video_file = gr.Video(label="Video File:", show_label=True)
130
+ local_video_labels_text = gr.Textbox(
131
+ label="Labels Text:", show_label=True
132
+ )
133
+ local_video_predict_btn = gr.Button(value="Predict")
134
+ with gr.Column():
135
+ video_gif = gr.Image(
136
+ label="Input Clip",
137
+ show_label=True,
138
+ )
139
+ with gr.Column():
140
+ predictions = gr.Label(label="Predictions:", show_label=True)
141
+
142
+ gr.Markdown("**Examples:**")
143
+ gr.Examples(
144
+ examples,
145
+ [youtube_url, youtube_url_labels_text],
146
+ [predictions, video_gif],
147
+ fn=predict,
148
+ cache_examples=True,
149
+ )
150
+
151
+ youtube_url_predict_btn.click(
152
+ predict,
153
+ inputs=[youtube_url, youtube_url_labels_text],
154
+ outputs=[predictions, video_gif],
155
+ )
156
+ local_video_predict_btn.click(
157
+ predict,
158
+ inputs=[video_file, local_video_labels_text],
159
+ outputs=[predictions, video_gif],
160
+ )
161
+ gr.Markdown(
162
+ """
163
+ \n Demo created by: <a href=\"https://github.com/fcakyon\">fcakyon</a>.
164
+ <br> Based on this <a href=\"https://huggingface.co/docs/transformers/main/model_doc/xclip">HuggingFace model</a>.
165
+ """
166
+ )
167
+
168
+ app.launch()