EvanTHU commited on
Commit
af645b7
·
1 Parent(s): 81db318
Files changed (1) hide show
  1. app.py +89 -2
app.py CHANGED
@@ -1,5 +1,8 @@
 
1
  import gradio as gr
2
  import os
 
 
3
  from huggingface_hub import hf_hub_download
4
 
5
 
@@ -21,9 +24,93 @@ filelist = ["animation", "dance", "haa500", "humman", "idea400", "kungfu", "musi
21
  for file_ in filelist:
22
  filename = f"{file_}-video.zip"
23
  file = hf_hub_download(repo_id=REPO_ID, filename=filename, repo_type="dataset", local_dir="./Movid")
 
24
  unzip_file(filename)
25
 
26
  hf_hub_download(repo_id=REPO_ID, filename="video-QA.json", repo_type="dataset", local_dir="./Movid")
27
 
28
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
29
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
  import gradio as gr
3
  import os
4
+ from fastapi import FastAPI
5
+ import uvicorn
6
  from huggingface_hub import hf_hub_download
7
 
8
 
 
24
  for file_ in filelist:
25
  filename = f"{file_}-video.zip"
26
  file = hf_hub_download(repo_id=REPO_ID, filename=filename, repo_type="dataset", local_dir="./Movid")
27
+ print(file)
28
  unzip_file(filename)
29
 
30
  hf_hub_download(repo_id=REPO_ID, filename="video-QA.json", repo_type="dataset", local_dir="./Movid")
31
 
32
+
33
+ # 读取 JSON 数据
34
+ with open('data.json', 'r') as f:
35
+ data = json.load(f)
36
+
37
+ # 提取需要的信息
38
+ instructions = [item.get('instruction', 'N/A') for item in data]
39
+ input_videos = [item.get('input', 'N/A') for item in data]
40
+ output_captions = [item.get('output', 'N/A') for item in data]
41
+
42
+ title_markdown = ("""<div class="embed_hidden" style="text-align: center;">
43
+ <h1>MoVid Video QA Dataset Visualization</h1>
44
+ <h3>
45
+ Dataset contact:
46
+ <a href="https://lhchen.top" target="_blank" rel="noopener noreferrer">Ling-Hao Chen</a> (THU, IDEA),
47
+ <a href="https://shunlinlu.github.io" target="_blank" rel="noopener noreferrer">Shunlin Lu</a> (CUHK-SZ, IDEA),
48
+ Other contributors: Yuhong Zhang (THU, IDEA).
49
+ </h3>
50
+ </div>
51
+ """)
52
+
53
+ # 获取根目录下的所有一级子目录
54
+ def get_subdirs(directory):
55
+ list_ = [d for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d))]
56
+ for item in list_:
57
+ if "idea400-release" in item:
58
+ list_.remove(item)
59
+ return list_
60
+
61
+ # 定义一个函数来获取下一级子目录或视频文件
62
+ def get_next_level(directory):
63
+ items = os.listdir(directory)
64
+ subdirs = [d for d in items if os.path.isdir(os.path.join(directory, d))]
65
+ videos = [f for f in items if f.endswith('.mp4')]
66
+ return subdirs, videos
67
+
68
+ # 定义一个函数来展示选定的视频及其相关信息
69
+ def display_video(subset1, subset2, video):
70
+ index = input_videos.index(f"./videos/{subset1}/{subset2}/{video}")
71
+ instruction = instructions[index]
72
+ input_video = f"./videos/{subset1}/{subset2}/{video}"
73
+ output_caption = output_captions[index]
74
+ return instruction, input_video, output_caption
75
+
76
+ # 创建 Gradio 界面
77
+ def create_demo():
78
+ with gr.Blocks() as demo:
79
+ css = """.large-font{ font-size: 40px; }
80
+ .gr-video{ width: 70%; max-width: 640px; height: auto; }
81
+ """
82
+ # gr.Markdown("# MoVid Video QA Dataset Visualization")
83
+ gr.Markdown(title_markdown)
84
+
85
+ root_dir = "./videos"
86
+ subset1_dirs = get_subdirs(root_dir)
87
+
88
+ with gr.Row():
89
+ subset1 = gr.Dropdown(choices=subset1_dirs, label="Select MoVid Subset")
90
+ subset2 = gr.Dropdown(choices=[], label="Select Split")
91
+ video = gr.Dropdown(choices=[], label="Select Video File")
92
+
93
+ instruction_output = gr.Textbox(label="Question", elem_classes=["large-font"])
94
+ caption_output = gr.Textbox(label="Answer", elem_classes=["large-font"])
95
+ video_output = gr.Video(label="Input Video", elem_classes=["large-font"])
96
+
97
+ def update_subset2(subset1):
98
+ subset2_dirs, _ = get_next_level(os.path.join(root_dir, subset1))
99
+ return gr.Dropdown.update(choices=subset2_dirs), gr.Dropdown.update(choices=[])
100
+
101
+ def update_videos(subset1, subset2):
102
+ _, videos = get_next_level(os.path.join(root_dir, subset1, subset2))
103
+ return gr.Dropdown.update(choices=videos)
104
+
105
+ subset1.change(update_subset2, inputs=subset1, outputs=[subset2, video])
106
+ subset2.change(update_videos, inputs=[subset1, subset2], outputs=video)
107
+ video.change(display_video, inputs=[subset1, subset2, video], outputs=[instruction_output, video_output, caption_output])
108
+
109
+ return demo
110
+
111
+ # 启动 Gradio Demo
112
+ demo = create_demo()
113
+ # define port
114
+ app = FastAPI()
115
+ app = gr.mount_gradio_app(app, demo, path="/")
116
+ uvicorn.run(app, host="0.0.0.0", port=8080)