mehal commited on
Commit
0832bd7
1 Parent(s): d7fa53c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -3
app.py CHANGED
@@ -31,6 +31,22 @@ VALID_ZEROSHOT_VIDEOCLASSIFICATION_MODELS = [
31
  processor = AutoProcessor.from_pretrained(DEFAULT_MODEL)
32
  model = AutoModel.from_pretrained(DEFAULT_MODEL)
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def select_model(model_name):
35
  global processor, model
36
  processor = AutoProcessor.from_pretrained(model_name)
@@ -43,6 +59,8 @@ def predict(youtube_url_or_file_path, labels_text):
43
  video_path = download_youtube_video(youtube_url_or_file_path)
44
  else:
45
  video_path = youtube_url_or_file_path
 
 
46
  num_total_frames = get_num_total_frames(video_path)
47
  num_model_input_frames = model.config.vision_config.num_frames
48
  if num_total_frames < FRAME_SAMPLING_RATE * num_model_input_frames:
@@ -60,6 +78,7 @@ def predict(youtube_url_or_file_path, labels_text):
60
  inputs = processor(
61
  text=labels, videos=list(frames), return_tensors="pt", padding=True
62
  )
 
63
  with torch.no_grad():
64
  outputs = model(**inputs)
65
 
@@ -74,7 +93,7 @@ def predict(youtube_url_or_file_path, labels_text):
74
  app = gr.Blocks()
75
  with app:
76
  gr.Markdown(
77
- "# **<p align='center'> PROTOG - VIOLENCE DETECTION MODULE</p>**"
78
  )
79
 
80
  with gr.Row():
@@ -88,7 +107,7 @@ with app:
88
  model_names_dropdown.change(fn=select_model, inputs=model_names_dropdown)
89
  with gr.Tab(label="Youtube URL"):
90
  gr.Markdown(
91
- "### **Enter Youtube URL**"
92
  )
93
  youtube_url = gr.Textbox(label="Youtube URL:", show_label=True)
94
  youtube_url_labels_text = gr.Textbox(
@@ -97,7 +116,7 @@ with app:
97
  youtube_url_predict_btn = gr.Button(value="Predict")
98
  with gr.Tab(label="Local File"):
99
  gr.Markdown(
100
- "### **Video Upload**"
101
  )
102
  video_file = gr.Video(label="Video File:", show_label=True)
103
  local_video_labels_text = gr.Textbox(
@@ -112,6 +131,15 @@ with app:
112
  with gr.Column():
113
  predictions = gr.Label(label="Predictions:", show_label=True)
114
 
 
 
 
 
 
 
 
 
 
115
  youtube_url_predict_btn.click(
116
  predict,
117
  inputs=[youtube_url, youtube_url_labels_text],
@@ -122,5 +150,11 @@ with app:
122
  inputs=[video_file, local_video_labels_text],
123
  outputs=[predictions, video_gif],
124
  )
 
 
 
 
 
 
125
 
126
  app.launch()
 
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)
 
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:
 
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
 
 
93
  app = gr.Blocks()
94
  with app:
95
  gr.Markdown(
96
+ "# **<p align='center'>PROGTOG VIOLENCE DETECTION</p>**"
97
  )
98
 
99
  with gr.Row():
 
107
  model_names_dropdown.change(fn=select_model, inputs=model_names_dropdown)
108
  with gr.Tab(label="Youtube URL"):
109
  gr.Markdown(
110
+ "### **Youtube URL**"
111
  )
112
  youtube_url = gr.Textbox(label="Youtube URL:", show_label=True)
113
  youtube_url_labels_text = gr.Textbox(
 
116
  youtube_url_predict_btn = gr.Button(value="Predict")
117
  with gr.Tab(label="Local File"):
118
  gr.Markdown(
119
+ "### **Tags**"
120
  )
121
  video_file = gr.Video(label="Video File:", show_label=True)
122
  local_video_labels_text = gr.Textbox(
 
131
  with gr.Column():
132
  predictions = gr.Label(label="Predictions:", show_label=True)
133
 
134
+ # gr.Markdown("**Examples:**")
135
+ # gr.Examples(
136
+ # examples,
137
+ # [youtube_url, youtube_url_labels_text],
138
+ # [predictions, video_gif],
139
+ # fn=predict,
140
+ # cache_examples=True,
141
+ # )
142
+
143
  youtube_url_predict_btn.click(
144
  predict,
145
  inputs=[youtube_url, youtube_url_labels_text],
 
150
  inputs=[video_file, local_video_labels_text],
151
  outputs=[predictions, video_gif],
152
  )
153
+ # gr.Markdown(
154
+ # """
155
+ # \n Demo created by: <a href=\"https://github.com/fcakyon\">fcakyon</a>.
156
+ # <br> Based on this <a href=\"https://huggingface.co/docs/transformers/main/model_doc/xclip">HuggingFace model</a>.
157
+ # """
158
+ # )
159
 
160
  app.launch()