yerang commited on
Commit
69f7f0d
·
verified ·
1 Parent(s): 66095e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -128
app.py CHANGED
@@ -51,163 +51,173 @@ print("LD_LIBRARY_PATH:", os.environ['LD_LIBRARY_PATH'])
51
 
52
  from stf_utils import STFPipeline
53
 
 
54
 
55
- def partial_fields(target_class, kwargs):
56
- return target_class(**{k: v for k, v in kwargs.items() if hasattr(target_class, k)})
 
57
 
58
- # set tyro theme
59
- tyro.extras.set_accent_color("bright_cyan")
60
- args = tyro.cli(ArgumentConfig)
61
 
62
- # specify configs for inference
63
- inference_cfg = partial_fields(InferenceConfig, args.__dict__) # use attribute of args to initial InferenceConfig
64
- crop_cfg = partial_fields(CropConfig, args.__dict__) # use attribute of args to initial CropConfig
65
 
66
- # gradio_pipeline = GradioPipeline(
67
- # inference_cfg=inference_cfg,
68
- # crop_cfg=crop_cfg,
69
- # args=args
70
- # )
71
 
72
- @spaces.GPU(duration=120)
73
- def gpu_wrapped_execute_video(*args, **kwargs):
74
- return gradio_pipeline.execute_video(*args, **kwargs)
75
 
76
- @spaces.GPU(duration=120)
77
- def gpu_wrapped_execute_image(*args, **kwargs):
78
- return gradio_pipeline.execute_image(*args, **kwargs)
79
 
80
- @spaces.GPU(duration=120)
81
- def gpu_wrapped_stf_pipeline_execute(audio_path):
82
- return stf_pipeline.execute(audio_path)
83
 
84
- @spaces.GPU(duration=120)
85
- def gpu_wrapped_elevenlabs_pipeline_generate_voice(text, voice):
86
- return elevenlabs_pipeline.generate_voice(text, voice)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
 
89
- def is_square_video(video_path):
90
- video = cv2.VideoCapture(video_path)
91
 
92
- width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
93
- height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
94
 
95
- video.release()
96
- if width != height:
97
- raise gr.Error("Error: the video does not have a square aspect ratio. We currently only support square videos")
98
 
99
- return gr.update(visible=True)
100
 
101
 
102
- # assets
103
- title_md = "assets/gradio_title.md"
104
- example_portrait_dir = "assets/examples/source"
105
- example_video_dir = "assets/examples/driving"
106
- data_examples = [
107
- [osp.join(example_portrait_dir, "s9.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
108
- [osp.join(example_portrait_dir, "s6.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
109
- [osp.join(example_portrait_dir, "s10.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
110
- [osp.join(example_portrait_dir, "s5.jpg"), osp.join(example_video_dir, "d18.mp4"), True, True, True, True],
111
- [osp.join(example_portrait_dir, "s7.jpg"), osp.join(example_video_dir, "d19.mp4"), True, True, True, True],
112
- [osp.join(example_portrait_dir, "s22.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
113
- ]
114
- #################### interface logic ####################
115
-
116
-
117
-
118
- # Define components first
119
- eye_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target eyes-open ratio")
120
- lip_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target lip-open ratio")
121
- retargeting_input_image = gr.Image(type="filepath")
122
- output_image = gr.Image(type="numpy")
123
- output_image_paste_back = gr.Image(type="numpy")
124
- output_video = gr.Video()
125
- output_video_concat = gr.Video()
126
 
127
 
128
 
129
 
130
- def run_end_to_end(image_path, text, voice, input_video, flag_relative, flag_do_crop, flag_remap, flag_crop_driving_video, male): #, animal):
131
 
132
- # # animal 체크 여부에 따라 다른 pipeline 사용
133
- # if animal:
134
- # gradio_pipeline = GradioPipelineAnimal(
135
- # inference_cfg=inference_cfg,
136
- # crop_cfg=crop_cfg,
137
- # args=args
138
- # )
139
- # else:
140
- # gradio_pipeline = GradioPipeline(
141
- # inference_cfg=inference_cfg,
142
- # crop_cfg=crop_cfg,
143
- # args=args
144
- # )
145
-
146
-
147
- if input_video is None:
148
 
149
- if not male:
150
- stf_pipeline = STFPipeline()
151
- else:
152
- stf_pipeline = STFPipeline(template_video_path="/home/user/app/stf/TEMP/Cam2_2309071202_0012_Natural_Looped.mp4",
153
- config_path="/home/user/app/stf/TEMP/front_config_v3.json",
154
- checkpoint_path="/home/user/app/stf/TEMP/0157.pth",
155
- )
156
-
157
- if input_video is None:
158
- #audio_path = elevenlabs_pipeline.generate_voice(text, voice)
159
- audio_path = gpu_wrapped_elevenlabs_pipeline_generate_voice(text, voice)
160
- #driving_video_path = stf_pipeline.execute(audio_path)
161
- driving_video_path = gpu_wrapped_stf_pipeline_execute(audio_path)
162
- else:
163
- driving_video_path = input_video
164
- os.makedirs("animations",exist_ok=True)
165
- audio_path = osp.join("animations", Path(input_video).stem+".wav")
166
- extract_audio(driving_video_path, audio_path)
167
-
168
-
169
- #output_path, crop_output_path = gradio_pipeline.execute_video(
170
- output_path, crop_output_path = gpu_wrapped_execute_video(
171
- input_image_path=image_path,
172
- input_video_path=driving_video_path,
173
- # input_driving_video_pickle_path=None,
174
- flag_do_crop_input=flag_do_crop,
175
- flag_remap_input=flag_remap,
176
- flag_relative_input=flag_relative,
177
- # driving_multiplier=1.0,
178
- # flag_stitching=False,
179
- # flag_crop_driving_video_input=flag_crop_driving_video,
180
- # scale=2.3,
181
- # vx_ratio=0.0,
182
- # vy_ratio=-0.125,
183
- # scale_crop_driving_video=2.2,
184
- # vx_ratio_crop_driving_video=0.0,
185
- # vy_ratio_crop_driving_video=-0.1,
186
- # tab_selection=None,
187
- audio_path=audio_path
188
- )
189
 
190
- return output_path, crop_output_path
191
 
192
 
193
 
194
 
195
- ###### 테스트중 ######
196
 
197
 
198
- stf_pipeline = STFPipeline()
 
 
 
 
 
 
 
 
 
 
199
 
200
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
201
- stf_button = gr.Button("stf test", variant="primary")
202
- stf_button.click(
203
- fn=gpu_wrapped_stf_pipeline_execute,
204
- inputs=[
205
- audio_path
206
- ],
207
- outputs=[driving_video_path]
208
- )
209
-
210
- ###### 테스트중 ######
211
 
212
 
213
  # with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
51
 
52
  from stf_utils import STFPipeline
53
 
54
+ stf_pipeline = STFPipeline()
55
 
56
+ @spaces.GPU(duration=120)
57
+ def gpu_wrapped_stf_pipeline_execute(audio_path):
58
+ return stf_pipeline.execute(audio_path)
59
 
60
+
 
 
61
 
 
 
 
62
 
 
 
 
 
 
63
 
 
 
 
64
 
65
+ # def partial_fields(target_class, kwargs):
66
+ # return target_class(**{k: v for k, v in kwargs.items() if hasattr(target_class, k)})
 
67
 
68
+ # # set tyro theme
69
+ # tyro.extras.set_accent_color("bright_cyan")
70
+ # args = tyro.cli(ArgumentConfig)
71
 
72
+ # # specify configs for inference
73
+ # inference_cfg = partial_fields(InferenceConfig, args.__dict__) # use attribute of args to initial InferenceConfig
74
+ # crop_cfg = partial_fields(CropConfig, args.__dict__) # use attribute of args to initial CropConfig
75
+
76
+ # # gradio_pipeline = GradioPipeline(
77
+ # # inference_cfg=inference_cfg,
78
+ # # crop_cfg=crop_cfg,
79
+ # # args=args
80
+ # # )
81
+
82
+ # @spaces.GPU(duration=120)
83
+ # def gpu_wrapped_execute_video(*args, **kwargs):
84
+ # return gradio_pipeline.execute_video(*args, **kwargs)
85
+
86
+ # @spaces.GPU(duration=120)
87
+ # def gpu_wrapped_execute_image(*args, **kwargs):
88
+ # return gradio_pipeline.execute_image(*args, **kwargs)
89
+
90
+ # @spaces.GPU(duration=120)
91
+ # def gpu_wrapped_stf_pipeline_execute(audio_path):
92
+ # return stf_pipeline.execute(audio_path)
93
+
94
+ # @spaces.GPU(duration=120)
95
+ # def gpu_wrapped_elevenlabs_pipeline_generate_voice(text, voice):
96
+ # return elevenlabs_pipeline.generate_voice(text, voice)
97
 
98
 
99
+ # def is_square_video(video_path):
100
+ # video = cv2.VideoCapture(video_path)
101
 
102
+ # width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
103
+ # height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
104
 
105
+ # video.release()
106
+ # if width != height:
107
+ # raise gr.Error("Error: the video does not have a square aspect ratio. We currently only support square videos")
108
 
109
+ # return gr.update(visible=True)
110
 
111
 
112
+ # # assets
113
+ # title_md = "assets/gradio_title.md"
114
+ # example_portrait_dir = "assets/examples/source"
115
+ # example_video_dir = "assets/examples/driving"
116
+ # data_examples = [
117
+ # [osp.join(example_portrait_dir, "s9.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
118
+ # [osp.join(example_portrait_dir, "s6.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
119
+ # [osp.join(example_portrait_dir, "s10.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
120
+ # [osp.join(example_portrait_dir, "s5.jpg"), osp.join(example_video_dir, "d18.mp4"), True, True, True, True],
121
+ # [osp.join(example_portrait_dir, "s7.jpg"), osp.join(example_video_dir, "d19.mp4"), True, True, True, True],
122
+ # [osp.join(example_portrait_dir, "s22.jpg"), osp.join(example_video_dir, "d0.mp4"), True, True, True, True],
123
+ # ]
124
+ # #################### interface logic ####################
125
+
126
+
127
+
128
+ # # Define components first
129
+ # eye_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target eyes-open ratio")
130
+ # lip_retargeting_slider = gr.Slider(minimum=0, maximum=0.8, step=0.01, label="target lip-open ratio")
131
+ # retargeting_input_image = gr.Image(type="filepath")
132
+ # output_image = gr.Image(type="numpy")
133
+ # output_image_paste_back = gr.Image(type="numpy")
134
+ # output_video = gr.Video()
135
+ # output_video_concat = gr.Video()
136
 
137
 
138
 
139
 
140
+ # def run_end_to_end(image_path, text, voice, input_video, flag_relative, flag_do_crop, flag_remap, flag_crop_driving_video, male): #, animal):
141
 
142
+ # # # animal 체크 여부에 따라 다른 pipeline 사용
143
+ # # if animal:
144
+ # # gradio_pipeline = GradioPipelineAnimal(
145
+ # # inference_cfg=inference_cfg,
146
+ # # crop_cfg=crop_cfg,
147
+ # # args=args
148
+ # # )
149
+ # # else:
150
+ # # gradio_pipeline = GradioPipeline(
151
+ # # inference_cfg=inference_cfg,
152
+ # # crop_cfg=crop_cfg,
153
+ # # args=args
154
+ # # )
155
+
156
+
157
+ # if input_video is None:
158
 
159
+ # if not male:
160
+ # stf_pipeline = STFPipeline()
161
+ # else:
162
+ # stf_pipeline = STFPipeline(template_video_path="/home/user/app/stf/TEMP/Cam2_2309071202_0012_Natural_Looped.mp4",
163
+ # config_path="/home/user/app/stf/TEMP/front_config_v3.json",
164
+ # checkpoint_path="/home/user/app/stf/TEMP/0157.pth",
165
+ # )
166
+
167
+ # if input_video is None:
168
+ # #audio_path = elevenlabs_pipeline.generate_voice(text, voice)
169
+ # audio_path = gpu_wrapped_elevenlabs_pipeline_generate_voice(text, voice)
170
+ # #driving_video_path = stf_pipeline.execute(audio_path)
171
+ # driving_video_path = gpu_wrapped_stf_pipeline_execute(audio_path)
172
+ # else:
173
+ # driving_video_path = input_video
174
+ # os.makedirs("animations",exist_ok=True)
175
+ # audio_path = osp.join("animations", Path(input_video).stem+".wav")
176
+ # extract_audio(driving_video_path, audio_path)
177
+
178
+
179
+ # #output_path, crop_output_path = gradio_pipeline.execute_video(
180
+ # output_path, crop_output_path = gpu_wrapped_execute_video(
181
+ # input_image_path=image_path,
182
+ # input_video_path=driving_video_path,
183
+ # # input_driving_video_pickle_path=None,
184
+ # flag_do_crop_input=flag_do_crop,
185
+ # flag_remap_input=flag_remap,
186
+ # flag_relative_input=flag_relative,
187
+ # # driving_multiplier=1.0,
188
+ # # flag_stitching=False,
189
+ # # flag_crop_driving_video_input=flag_crop_driving_video,
190
+ # # scale=2.3,
191
+ # # vx_ratio=0.0,
192
+ # # vy_ratio=-0.125,
193
+ # # scale_crop_driving_video=2.2,
194
+ # # vx_ratio_crop_driving_video=0.0,
195
+ # # vy_ratio_crop_driving_video=-0.1,
196
+ # # tab_selection=None,
197
+ # audio_path=audio_path
198
+ # )
199
 
200
+ # return output_path, crop_output_path
201
 
202
 
203
 
204
 
205
+ # ###### 테스트중 ######
206
 
207
 
208
+ # stf_pipeline = STFPipeline()
209
+
210
+ # with gr.Blocks(theme=gr.themes.Soft()) as demo:
211
+ # stf_button = gr.Button("stf test", variant="primary")
212
+ # stf_button.click(
213
+ # fn=gpu_wrapped_stf_pipeline_execute,
214
+ # inputs=[
215
+ # audio_path
216
+ # ],
217
+ # outputs=[driving_video_path]
218
+ # )
219
 
220
+ # ###### 테스트중 ######
 
 
 
 
 
 
 
 
 
 
221
 
222
 
223
  # with gr.Blocks(theme=gr.themes.Soft()) as demo: