fcakyon commited on
Commit
133a713
1 Parent(s): b182d92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -31
app.py CHANGED
@@ -1,33 +1,82 @@
1
- import os
2
  import torch
3
  import gradio as gr
4
- from transformers import XCLIPProcessor, XCLIPModel
5
- from utils import convert_frames_to_gif, download_youtube_video, sample_frames_from_video_file
 
 
 
 
 
6
 
7
- model_name = "microsoft/xclip-base-patch16-zero-shot"
8
- processor = XCLIPProcessor.from_pretrained(model_name)
9
- model = XCLIPModel.from_pretrained(model_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  examples = [
12
- ["https://www.youtu.be/l1dBM8ZECao", "sleeping dog,cat fight club,birds of prey"],
13
- ["https://youtu.be/VMj-3S1tku0", "programming course,eating spaghetti,playing football"],
14
- ["https://www.youtu.be/x8UAUAuKNcU", "game of thrones,the lord of the rings,vikings"]
 
 
 
 
 
 
 
 
 
15
  ]
16
 
17
 
18
- def predict(youtube_url, labels_text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  labels = labels_text.split(",")
21
- video_path = download_youtube_video(youtube_url)
22
- frames = sample_frames_from_video_file(video_path, num_frames=32)
23
- os.remove(video_path)
24
- gif_path = convert_frames_to_gif(frames)
 
25
 
26
  inputs = processor(
27
- text=labels,
28
- videos=list(frames),
29
- return_tensors="pt",
30
- padding=True
31
  )
32
  # forward pass
33
  with torch.no_grad():
@@ -37,12 +86,15 @@ def predict(youtube_url, labels_text):
37
  label_to_prob = {}
38
  for ind, label in enumerate(labels):
39
  label_to_prob[label] = float(probs[ind])
40
-
41
  return label_to_prob, gif_path
42
 
 
43
  app = gr.Blocks()
44
  with app:
45
- gr.Markdown("# **<p align='center'>Zero-shot Video Classification with X-CLIP</p>**")
 
 
46
  gr.Markdown(
47
  """
48
  <p style='text-align: center'>
@@ -54,24 +106,63 @@ with app:
54
 
55
  with gr.Row():
56
  with gr.Column():
57
- gr.Markdown("Provide a Youtube video URL and a list of labels separated by commas")
58
- youtube_url = gr.Textbox(label="Youtube URL:", show_label=True)
59
- labels_text = gr.Textbox(label="Labels Text:", show_label=True)
60
- predict_btn = gr.Button(value="Predict")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  with gr.Column():
62
- video_gif = gr.Image(label="Input Clip", show_label=True,)
 
 
 
63
  with gr.Column():
64
- predictions = gr.Label(label='Predictions:', show_label=True)
65
 
66
  gr.Markdown("**Examples:**")
67
- gr.Examples(examples, [youtube_url, labels_text], [predictions, video_gif], fn=predict, cache_examples=True)
 
 
 
 
 
 
68
 
69
- predict_btn.click(predict, inputs=[youtube_url, labels_text], outputs=[predictions, video_gif])
 
 
 
 
 
 
 
 
 
70
  gr.Markdown(
71
  """
72
- \n Demo created by: <a href=\"https://github.com/fcakyon\">fcakyon</a>
73
- <br> Based on this <a href=\"https://huggingface.co/microsoft/xclip-base-patch16-zero-shot\">HuggingFace model</a>
74
  """
75
  )
76
-
77
  app.launch()
 
 
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://www.youtu.be/x8UAUAuKNcU",
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():
 
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'>
 
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=False,
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()