Spaces:
Runtime error
Runtime error
Update t2v_video_app.py
Browse files- t2v_video_app.py +49 -5
t2v_video_app.py
CHANGED
@@ -63,7 +63,8 @@ config = {
|
|
63 |
"model_config": model_config,
|
64 |
"W": 512,
|
65 |
"H": 512,
|
66 |
-
"L": 16
|
|
|
67 |
}
|
68 |
|
69 |
# 写死 pretrained_model_path
|
@@ -127,7 +128,6 @@ def initialize_models():
|
|
127 |
# 准备 UNet 的 attention 和 conv
|
128 |
pipeline.unet = prep_unet_attention(pipeline.unet, config["motion_guidance_blocks"])
|
129 |
pipeline.unet = prep_unet_conv(pipeline.unet)
|
130 |
-
pipeline.scheduler.customized_set_timesteps(config["inference_steps"], config["guidance_steps"], config["guidance_scale"], device=device, timestep_spacing_type="uneven")
|
131 |
|
132 |
return pipeline
|
133 |
|
@@ -146,12 +146,14 @@ def generate_video(uploaded_video, motion_representation_save_dir, generated_vid
|
|
146 |
"warm_up_steps": warm_up_steps,
|
147 |
"cool_up_steps": cool_up_steps,
|
148 |
"motion_guidance_weight": motion_guidance_weight,
|
149 |
-
"motion_guidance_blocks": motion_guidance_blocks,
|
150 |
"add_noise_step": add_noise_step
|
151 |
})
|
152 |
|
153 |
# 设置环境变量
|
154 |
os.environ["CUDA_VISIBLE_DEVICES"] = visible_gpu or str(os.getenv('CUDA_VISIBLE_DEVICES', 0))
|
|
|
|
|
155 |
|
156 |
# 创建保存目录
|
157 |
if not os.path.exists(generated_videos_save_dir):
|
@@ -159,14 +161,25 @@ def generate_video(uploaded_video, motion_representation_save_dir, generated_vid
|
|
159 |
|
160 |
# 处理上传的视频
|
161 |
if uploaded_video is not None:
|
|
|
|
|
162 |
# 将上传的视频保存到指定路径
|
163 |
video_path = os.path.join(generated_videos_save_dir, os.path.basename(uploaded_video))
|
164 |
shutil.move(uploaded_video, video_path)
|
|
|
|
|
|
|
165 |
|
166 |
# 更新配置
|
167 |
config["video_path"] = video_path
|
168 |
config["new_prompt"] = new_prompt + config.get("positive_prompt", "")
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
# 提取运动表示
|
172 |
seed_motion = seed if seed is not None else default_seed
|
@@ -184,6 +197,10 @@ def generate_video(uploaded_video, motion_representation_save_dir, generated_vid
|
|
184 |
pipeline.input_config.seed = seed
|
185 |
|
186 |
videos = pipeline.sample_video(generator=generator)
|
|
|
|
|
|
|
|
|
187 |
videos = rearrange(videos, "b c f h w -> b f h w c")
|
188 |
save_path = os.path.join(generated_videos_save_dir, os.path.splitext(os.path.basename(config["video_path"]))[0] + "_" + config["new_prompt"].strip().replace(' ', '_') + str(seed_motion) + "_" + str(seed) + '.mp4')
|
189 |
videos_uint8 = (videos[0] * 255).astype(np.uint8)
|
@@ -204,7 +221,7 @@ with gr.Blocks() as demo:
|
|
204 |
with gr.Row():
|
205 |
with gr.Column():
|
206 |
# 视频上传
|
207 |
-
uploaded_video = gr.Video(label="Upload Video"
|
208 |
# 新提示词
|
209 |
new_prompt = gr.Textbox(label="New Prompt", value="A beautiful scene", lines=2)
|
210 |
# 种子
|
@@ -247,5 +264,32 @@ with gr.Blocks() as demo:
|
|
247 |
outputs=output_video
|
248 |
)
|
249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
# 启动应用
|
251 |
demo.launch(share = True)
|
|
|
63 |
"model_config": model_config,
|
64 |
"W": 512,
|
65 |
"H": 512,
|
66 |
+
"L": 16,
|
67 |
+
"motion_guidance_blocks": ['up_blocks.1',]
|
68 |
}
|
69 |
|
70 |
# 写死 pretrained_model_path
|
|
|
128 |
# 准备 UNet 的 attention 和 conv
|
129 |
pipeline.unet = prep_unet_attention(pipeline.unet, config["motion_guidance_blocks"])
|
130 |
pipeline.unet = prep_unet_conv(pipeline.unet)
|
|
|
131 |
|
132 |
return pipeline
|
133 |
|
|
|
146 |
"warm_up_steps": warm_up_steps,
|
147 |
"cool_up_steps": cool_up_steps,
|
148 |
"motion_guidance_weight": motion_guidance_weight,
|
149 |
+
#"motion_guidance_blocks": motion_guidance_blocks,
|
150 |
"add_noise_step": add_noise_step
|
151 |
})
|
152 |
|
153 |
# 设置环境变量
|
154 |
os.environ["CUDA_VISIBLE_DEVICES"] = visible_gpu or str(os.getenv('CUDA_VISIBLE_DEVICES', 0))
|
155 |
+
|
156 |
+
device = pipeline.device
|
157 |
|
158 |
# 创建保存目录
|
159 |
if not os.path.exists(generated_videos_save_dir):
|
|
|
161 |
|
162 |
# 处理上传的视频
|
163 |
if uploaded_video is not None:
|
164 |
+
pipeline.scheduler.customized_set_timesteps(config["inference_steps"], config["guidance_steps"], config["guidance_scale"], device=device, timestep_spacing_type="uneven")
|
165 |
+
|
166 |
# 将上传的视频保存到指定路径
|
167 |
video_path = os.path.join(generated_videos_save_dir, os.path.basename(uploaded_video))
|
168 |
shutil.move(uploaded_video, video_path)
|
169 |
+
|
170 |
+
print("video_path :")
|
171 |
+
print(video_path)
|
172 |
|
173 |
# 更新配置
|
174 |
config["video_path"] = video_path
|
175 |
config["new_prompt"] = new_prompt + config.get("positive_prompt", "")
|
176 |
+
|
177 |
+
from types import SimpleNamespace
|
178 |
+
|
179 |
+
pipeline.input_config, pipeline.unet.input_config = SimpleNamespace(**config), SimpleNamespace(**config)
|
180 |
+
|
181 |
+
print("pipeline.input_config.video_path :")
|
182 |
+
print(pipeline.input_config.video_path)
|
183 |
|
184 |
# 提取运动表示
|
185 |
seed_motion = seed if seed is not None else default_seed
|
|
|
197 |
pipeline.input_config.seed = seed
|
198 |
|
199 |
videos = pipeline.sample_video(generator=generator)
|
200 |
+
|
201 |
+
#print("videos :")
|
202 |
+
#print(videos)
|
203 |
+
|
204 |
videos = rearrange(videos, "b c f h w -> b f h w c")
|
205 |
save_path = os.path.join(generated_videos_save_dir, os.path.splitext(os.path.basename(config["video_path"]))[0] + "_" + config["new_prompt"].strip().replace(' ', '_') + str(seed_motion) + "_" + str(seed) + '.mp4')
|
206 |
videos_uint8 = (videos[0] * 255).astype(np.uint8)
|
|
|
221 |
with gr.Row():
|
222 |
with gr.Column():
|
223 |
# 视频上传
|
224 |
+
uploaded_video = gr.Video(label="Upload Video")
|
225 |
# 新提示词
|
226 |
new_prompt = gr.Textbox(label="New Prompt", value="A beautiful scene", lines=2)
|
227 |
# 种子
|
|
|
264 |
outputs=output_video
|
265 |
)
|
266 |
|
267 |
+
# 定义示例数据
|
268 |
+
examples = [
|
269 |
+
{"video_path": "reference_videos/camera_zoom_in.mp4", "new_prompt": "Relics on the seabed", "seed": 42},
|
270 |
+
{"video_path": "reference_videos/camera_zoom_in.mp4", "new_prompt": "A road in the mountain", "seed": 42},
|
271 |
+
{"video_path": "reference_videos/camera_zoom_in.mp4", "new_prompt": "Caves, a path for exploration", "seed": 2026},
|
272 |
+
{"video_path": "reference_videos/camera_zoom_in.mp4", "new_prompt": "Railway for train", "seed": 2026},
|
273 |
+
{"video_path": "reference_videos/camera_zoom_out.mp4", "new_prompt": "Tree, in the mountain", "seed": 2026},
|
274 |
+
{"video_path": "reference_videos/camera_zoom_out.mp4", "new_prompt": "Red car on the track", "seed": 2026},
|
275 |
+
{"video_path": "reference_videos/camera_zoom_out.mp4", "new_prompt": "Man, standing in his garden.", "seed": 2026},
|
276 |
+
{"video_path": "reference_videos/camera_1.mp4", "new_prompt": "A island, on the ocean, sunny day", "seed": 42},
|
277 |
+
{"video_path": "reference_videos/camera_1.mp4", "new_prompt": "A tower, with fireworks", "seed": 42},
|
278 |
+
{"video_path": "reference_videos/camera_pan_up.mp4", "new_prompt": "Beautiful house, around with flowers", "seed": 42},
|
279 |
+
{"video_path": "reference_videos/camera_translation_2.mp4", "new_prompt": "Forest, in winter", "seed": 2028},
|
280 |
+
{"video_path": "reference_videos/camera_pan_down.mp4", "new_prompt": "Eagle, standing in the tree", "seed": 2026}
|
281 |
+
]
|
282 |
+
examples = list(map(lambda d: [d["video_path"], d["new_prompt"], d["seed"]], examples))
|
283 |
+
|
284 |
+
# 添加示例
|
285 |
+
gr.Examples(
|
286 |
+
examples=examples,
|
287 |
+
inputs=[uploaded_video, new_prompt, seed],
|
288 |
+
outputs=output_video,
|
289 |
+
fn=generate_video,
|
290 |
+
cache_examples=False
|
291 |
+
)
|
292 |
+
|
293 |
+
|
294 |
# 启动应用
|
295 |
demo.launch(share = True)
|