quantumiracle-git commited on
Commit
7670b16
·
1 Parent(s): c9c5342

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import random
4
+ import numpy as np
5
+
6
+ def video_identity(video):
7
+ return video
8
+
9
+ def nan():
10
+ return None
11
+
12
+ # demo = gr.Interface(video_identity,
13
+ # gr.Video(),
14
+ # "playable_video",
15
+ # examples=[
16
+ # os.path.join(os.path.dirname(__file__),
17
+ # "videos/rl-video-episode-0.mp4")],
18
+ # cache_examples=True)
19
+
20
+ FORMAT = ['mp4', 'gif'][1]
21
+
22
+ def update(left, right, data_folder='videos'):
23
+ # data_folder='videos'
24
+ envs = parse_envs()
25
+ env_name = envs[random.randint(0, len(envs)-1)]
26
+ # choose video
27
+ videos = os.listdir(os.path.join(data_folder, env_name))
28
+ video_files = []
29
+ for f in videos:
30
+ if f.endswith(f'.{FORMAT}'):
31
+ video_files.append(os.path.join(data_folder, env_name, f))
32
+ # choose two videos
33
+ selected_video_ids = np.random.choice(len(video_files), 2, replace=False)
34
+ left = video_files[selected_video_ids[0]]
35
+ right = video_files[selected_video_ids[1]]
36
+ print(env_name, left, right)
37
+ return left, right
38
+
39
+ # def update(left, right):
40
+ # if FORMAT == 'mp4':
41
+ # left = os.path.join(os.path.dirname(__file__),
42
+ # "videos/rl-video-episode-2.mp4")
43
+ # right = os.path.join(os.path.dirname(__file__),
44
+ # "videos/rl-video-episode-3.mp4")
45
+ # else:
46
+ # left = os.path.join(os.path.dirname(__file__),
47
+ # "videos/rl-video-episode-2.gif")
48
+ # right = os.path.join(os.path.dirname(__file__),
49
+ # "videos/rl-video-episode-3.gif")
50
+ # print(left, right)
51
+ # return left, right
52
+
53
+ def replay(left, right):
54
+ return left, right
55
+
56
+ def parse_envs(folder='./videos'):
57
+ envs = []
58
+ for f in os.listdir(folder):
59
+ if os.path.isdir(os.path.join(folder, f)):
60
+ envs.append(f)
61
+ return envs
62
+
63
+ def build_interface(iter=3, data_folder='./videos'):
64
+ # build gradio interface
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("Here is RoboTinder!")
67
+ gr.Markdown("Select the best robot behaviour in your choice!")
68
+ with gr.Row():
69
+ # some initial videos
70
+ if FORMAT == 'mp4':
71
+ left_video_path = os.path.join(os.path.dirname(__file__),
72
+ "videos/rl-video-episode-0.mp4")
73
+ right_video_path = os.path.join(os.path.dirname(__file__),
74
+ "videos/rl-video-episode-1.mp4")
75
+ left = gr.PlayableVideo(left_video_path)
76
+ right = gr.PlayableVideo(right_video_path)
77
+ else:
78
+ left_video_path = os.path.join(os.path.dirname(__file__),
79
+ "videos/rl-video-episode-0.gif")
80
+ right_video_path = os.path.join(os.path.dirname(__file__),
81
+ "videos/rl-video-episode-1.gif")
82
+ left = gr.Image(left_video_path, shape=(1024, 768))
83
+ # right = gr.Image(right_video_path).style(height=768, width=1024)
84
+ right = gr.Image(right_video_path)
85
+
86
+ btn1 = gr.Button("Replay")
87
+ btn1.click(fn=replay, inputs=[left, right], outputs=[left, right])
88
+
89
+ gr.Radio(["Left", "Right", "Not Sure"], label="Which one is your favorite?")
90
+ btn2 = gr.Button("Next")
91
+ # choose env
92
+ btn2.click(fn=update, inputs=[left, right], outputs=[left, right])
93
+
94
+ return demo
95
+
96
+ if __name__ == "__main__":
97
+ demo = build_interface()
98
+ # demo.launch(share=True)
99
+ demo.launch(share=False)