taesiri commited on
Commit
01c97d5
1 Parent(s): 0edcc33
Files changed (1) hide show
  1. app.py +39 -41
app.py CHANGED
@@ -3,18 +3,21 @@ import os
3
  import random
4
  import re
5
  import sys
 
6
  import time
7
  from datetime import datetime
8
  from glob import glob
9
  from pathlib import Path
10
  from typing import List, Optional
11
  from uuid import uuid4
 
12
 
13
  import gradio as gr
14
  import numpy as np
15
  import pandas as pd
16
  import requests
17
  from datasets import load_dataset
 
18
  from huggingface_hub import (
19
  CommitScheduler,
20
  HfApi,
@@ -29,15 +32,6 @@ cached_top_posts = None
29
  last_fetched = None
30
  last_fetched_top = None
31
 
32
- import os
33
- import tempfile
34
- from zipfile import ZipFile
35
-
36
- import numpy as np
37
- from PIL import Image
38
- from decord import VideoReader
39
- from decord import cpu
40
-
41
 
42
  def get_reddit_id(url):
43
  # Regular expression pattern for r/GamePhysics URLs and IDs
@@ -184,23 +178,21 @@ def get_latest_posts():
184
  return examples
185
 
186
 
187
- def row_selected(evt: gr.SelectData):
188
- global cached_latest_posts_df
189
  global cached_top_posts
190
-
191
- # find which dataframe was selected
192
  string_value = evt.value
193
  row = evt.index[0]
194
- target_df = None
195
 
196
- if cached_latest_posts_df.isin([string_value]).any().any():
197
- target_df = cached_latest_posts_df
198
- elif cached_top_posts.isin([string_value]).any().any():
199
- target_df = cached_top_posts
200
- else:
201
- raise gr.Error("Could not find selected post in any dataframe")
202
 
203
- post_id = target_df.iloc[row]["post_id"]
 
 
 
 
 
 
204
  return post_id
205
 
206
 
@@ -230,28 +222,30 @@ with gr.Blocks() as demo:
230
  load_btn = gr.Button("Load")
231
  video_player = gr.Video(interactive=False)
232
 
233
- with gr.Column():
234
- gr.Markdown("## Latest Posts")
235
- latest_post_dataframe = gr.Dataframe()
236
- latest_posts_btn = gr.Button("Refresh Latest Posts")
237
- top_posts_btn = gr.Button("Refresh Top Posts")
238
 
239
- with gr.Column():
240
- gr.Markdown("## Sampled Frames from Video")
241
- with gr.Row():
242
- num_frames = gr.Slider(minimum=1, maximum=60, step=1, value=10)
243
- sample_decord_btn = gr.Button("Sample decord")
244
 
245
- sampled_frames = gr.Gallery()
 
246
 
247
- download_samples_btn = gr.Button("Download Samples")
248
- output_files = gr.File()
 
 
 
249
 
250
- download_samples_btn.click(
251
- download_samples,
252
- inputs=[reddit_id, video_player, num_frames],
253
- outputs=[output_files],
254
- )
 
 
 
255
 
256
  sample_decord_btn.click(
257
  extract_frames_decord,
@@ -262,11 +256,15 @@ with gr.Blocks() as demo:
262
  load_btn.click(load_video, inputs=[reddit_id], outputs=[video_player])
263
 
264
  latest_posts_btn.click(get_latest_posts, outputs=[latest_post_dataframe])
265
- top_posts_btn.click(get_top_posts, outputs=[latest_post_dataframe])
266
 
267
  demo.load(get_latest_posts, outputs=[latest_post_dataframe])
 
268
 
269
- latest_post_dataframe.select(fn=row_selected, outputs=[reddit_id]).then(
 
 
 
270
  load_video, inputs=[reddit_id], outputs=[video_player]
271
  )
272
 
 
3
  import random
4
  import re
5
  import sys
6
+ import tempfile
7
  import time
8
  from datetime import datetime
9
  from glob import glob
10
  from pathlib import Path
11
  from typing import List, Optional
12
  from uuid import uuid4
13
+ from zipfile import ZipFile
14
 
15
  import gradio as gr
16
  import numpy as np
17
  import pandas as pd
18
  import requests
19
  from datasets import load_dataset
20
+ from decord import VideoReader, cpu
21
  from huggingface_hub import (
22
  CommitScheduler,
23
  HfApi,
 
32
  last_fetched = None
33
  last_fetched_top = None
34
 
 
 
 
 
 
 
 
 
 
35
 
36
  def get_reddit_id(url):
37
  # Regular expression pattern for r/GamePhysics URLs and IDs
 
178
  return examples
179
 
180
 
181
+ def row_selected_top(evt: gr.SelectData):
 
182
  global cached_top_posts
 
 
183
  string_value = evt.value
184
  row = evt.index[0]
 
185
 
186
+ post_id = cached_top_posts.iloc[row]["post_id"]
187
+ return post_id
 
 
 
 
188
 
189
+
190
+ def row_selected_latest(evt: gr.SelectData):
191
+ global cached_latest_posts_df
192
+ string_value = evt.value
193
+ row = evt.index[0]
194
+
195
+ post_id = cached_latest_posts_df.iloc[row]["post_id"]
196
  return post_id
197
 
198
 
 
222
  load_btn = gr.Button("Load")
223
  video_player = gr.Video(interactive=False)
224
 
225
+ with gr.Column():
226
+ gr.Markdown("## Sampled Frames from Video")
227
+ num_frames = gr.Slider(minimum=1, maximum=60, step=1, value=10)
228
+ sample_decord_btn = gr.Button("Sample decord")
 
229
 
230
+ sampled_frames = gr.Gallery()
 
 
 
 
231
 
232
+ download_samples_btn = gr.Button("Download Samples")
233
+ output_files = gr.File()
234
 
235
+ download_samples_btn.click(
236
+ download_samples,
237
+ inputs=[reddit_id, video_player, num_frames],
238
+ outputs=[output_files],
239
+ )
240
 
241
+ with gr.Column():
242
+ gr.Markdown("## Latest Posts")
243
+ with gr.Accordion("Latest Posts"):
244
+ latest_post_dataframe = gr.Dataframe()
245
+ latest_posts_btn = gr.Button("Refresh Latest Posts")
246
+ with gr.Accordion("Top Posts"):
247
+ top_posts_dataframe = gr.Dataframe()
248
+ top_posts_btn = gr.Button("Refresh Top Posts")
249
 
250
  sample_decord_btn.click(
251
  extract_frames_decord,
 
256
  load_btn.click(load_video, inputs=[reddit_id], outputs=[video_player])
257
 
258
  latest_posts_btn.click(get_latest_posts, outputs=[latest_post_dataframe])
259
+ top_posts_btn.click(get_top_posts, outputs=[top_posts_dataframe])
260
 
261
  demo.load(get_latest_posts, outputs=[latest_post_dataframe])
262
+ demo.load(get_top_posts, outputs=[top_posts_dataframe])
263
 
264
+ latest_post_dataframe.select(fn=row_selected_latest, outputs=[reddit_id]).then(
265
+ load_video, inputs=[reddit_id], outputs=[video_player]
266
+ )
267
+ top_posts_dataframe.select(fn=row_selected_top, outputs=[reddit_id]).then(
268
  load_video, inputs=[reddit_id], outputs=[video_player]
269
  )
270