Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,899 Bytes
b887ad8 1b501ac b887ad8 1fd6914 b887ad8 d5e8fc2 b887ad8 d88f40d b887ad8 1fd6914 b887ad8 1fd6914 b887ad8 1fd6914 b887ad8 e768692 b887ad8 d88f40d b887ad8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
import spaces
import gradio as gr
import sys
import os
import torch
import numpy as np
from os.path import join as pjoin
import utils.paramUtil as paramUtil
from utils.plot_script import *
from utils.utils import *
from utils.motion_process import recover_from_ric
from accelerate.utils import set_seed
from models.gaussian_diffusion import DiffusePipeline
from options.generate_options import GenerateOptions
from utils.model_load import load_model_weights
from motion_loader import get_dataset_loader
from models import build_models
import yaml
import time
from box import Box
import hashlib
from huggingface_hub import hf_hub_download
ckptdir = './checkpoints/t2m/release'
os.makedirs(ckptdir, exist_ok=True)
os.environ['COMMANDLINE_ARGS'] = '--no-gradio-queue'
mean_path = hf_hub_download(
repo_id="EvanTHU/MotionCLR",
filename="meta/mean.npy",
local_dir=ckptdir,
local_dir_use_symlinks=False
)
std_path = hf_hub_download(
repo_id="EvanTHU/MotionCLR",
filename="meta/std.npy",
local_dir=ckptdir,
local_dir_use_symlinks=False
)
model_path = hf_hub_download(
repo_id="EvanTHU/MotionCLR",
filename="model/latest.tar",
local_dir=ckptdir,
local_dir_use_symlinks=False
)
opt_path = hf_hub_download(
repo_id="EvanTHU/MotionCLR",
filename="opt.txt",
local_dir=ckptdir,
local_dir_use_symlinks=False
)
os.makedirs("tmp", exist_ok=True)
os.environ['GRADIO_TEMP_DIR'] = './tmp'
def generate_md5(input_string):
# Encode the string and compute the MD5 hash
md5_hash = hashlib.md5(input_string.encode())
# Return the hexadecimal representation of the hash
return md5_hash.hexdigest()
def set_all_use_to_false(data):
for key, value in data.items():
if isinstance(value, Box):
set_all_use_to_false(value)
elif key == 'use':
data[key] = False
return data
def yaml_to_box(yaml_file):
with open(yaml_file, 'r') as file:
yaml_data = yaml.safe_load(file)
return Box(yaml_data)
HEAD = """<div class="embed_hidden">
<h1 style='text-align: center'> MotionCLR User Interaction Demo </h1>
"""
edit_config = yaml_to_box('options/edit.yaml')
os.environ['GRADIO_TEMP_DIR'] = './tmp'
CSS = """
.retrieved_video {
position: relative;
margin: 0;
box-shadow: var(--block-shadow);
border-width: var(--block-border-width);
border-color: #000000;
border-radius: var(--block-radius);
background: var(--block-background-fill);
width: 100%;
line-height: var(--line-sm);
}
.contour_video {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: var(--layer-5);
border-radius: var(--block-radius);
background: var(--background-fill-primary);
padding: 0 var(--size-6);
max-height: var(--size-screen-h);
overflow: hidden;
}
"""
@spaces.GPU
def generate_video_from_text(text, opt, pipeline):
width = 500
height = 500
texts = [text]
motion_lens = [opt.motion_length * opt.fps for _ in range(opt.num_samples)]
save_dir = './tmp/gen/'
filename = generate_md5(str(time.time())) + ".mp4"
save_path = pjoin(save_dir, str(filename))
os.makedirs(save_dir, exist_ok=True)
print("xxxxxxx")
print(pipeline.device)
print("xxxxxxx")
start_time = time.perf_counter()
gr.Info("Generating motion...", duration = 3)
pred_motions, _ = pipeline.generate(texts, torch.LongTensor([int(x) for x in motion_lens]))
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Generating time cost: {exc:.2f} s, rendering starts...", duration = 3)
start_time = time.perf_counter()
mean = np.load(pjoin(opt.meta_dir, 'mean.npy'))
std = np.load(pjoin(opt.meta_dir, 'std.npy'))
samples = []
root_list = []
for i, motion in enumerate(pred_motions):
motion = motion.cpu().numpy() * std + mean
# 1. recover 3d joints representation by ik
motion = recover_from_ric(torch.from_numpy(motion).float(), opt.joints_num)
# 2. put on Floor (Y axis)
floor_height = motion.min(dim=0)[0].min(dim=0)[0][1]
motion[:, :, 1] -= floor_height
motion = motion.numpy()
# 3. remove jitter
motion = motion_temporal_filter(motion, sigma=1)
samples.append(motion)
i = 0
title = texts[i]
motion = samples[i]
kinematic_tree = paramUtil.t2m_kinematic_chain if (opt.dataset_name == 't2m') else paramUtil.kit_kinematic_chain
plot_3d_motion(save_path, kinematic_tree, motion, title=title, fps=opt.fps, radius=opt.radius)
gr.Info("Rendered motion...", duration = 3)
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Rendering time cost: {exc:.2f} s", duration = 3)
video_dis = f'<video controls playsinline width="{width}" style="display: block; margin: 0 auto;" src="./file={save_path}"></video>'
style_dis = video_dis + """<br> <p align="center"> Content Reference </p>"""
global edit_config
edit_config = set_all_use_to_false(edit_config)
return video_dis, style_dis, video_dis, gr.update(visible=True)
@spaces.GPU
def reweighting(text, idx, weight, opt, pipeline):
global edit_config
edit_config.reweighting_attn.use = True
edit_config.reweighting_attn.idx = idx
edit_config.reweighting_attn.reweighting_attn_weight = weight
gr.Info("Loading Configurations...", duration = 3)
model = build_models(opt, edit_config=edit_config)
ckpt_path = pjoin(opt.model_dir, opt.which_ckpt + '.tar')
niter = load_model_weights(model, ckpt_path, use_ema=not opt.no_ema)
pipeline = DiffusePipeline(
opt = opt,
model = model,
diffuser_name = opt.diffuser_name,
device=opt.device,
num_inference_steps=opt.num_inference_steps,
torch_dtype=torch.float16,
)
print(edit_config)
width = 500
height = 500
texts = [text, text]
motion_lens = [opt.motion_length * opt.fps for _ in range(opt.num_samples)]
save_dir = './tmp/gen/'
filenames = [generate_md5(str(time.time())) + ".mp4", generate_md5(str(time.time())) + ".mp4"]
save_paths = [pjoin(save_dir, str(filenames[0])), pjoin(save_dir, str(filenames[1]))]
os.makedirs(save_dir, exist_ok=True)
start_time = time.perf_counter()
gr.Info("Generating motion...", duration = 3)
pred_motions, _ = pipeline.generate(texts, torch.LongTensor([int(x) for x in motion_lens]))
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Generating time cost: {exc:.2f} s, rendering starts...", duration = 3)
start_time = time.perf_counter()
mean = np.load(pjoin(opt.meta_dir, 'mean.npy'))
std = np.load(pjoin(opt.meta_dir, 'std.npy'))
samples = []
root_list = []
for i, motion in enumerate(pred_motions):
motion = motion.cpu().numpy() * std + mean
# 1. recover 3d joints representation by ik
motion = recover_from_ric(torch.from_numpy(motion).float(), opt.joints_num)
# 2. put on Floor (Y axis)
floor_height = motion.min(dim=0)[0].min(dim=0)[0][1]
motion[:, :, 1] -= floor_height
motion = motion.numpy()
# 3. remove jitter
motion = motion_temporal_filter(motion, sigma=1)
samples.append(motion)
i = 1
title = texts[i]
motion = samples[i]
kinematic_tree = paramUtil.t2m_kinematic_chain if (opt.dataset_name == 't2m') else paramUtil.kit_kinematic_chain
plot_3d_motion(save_paths[1], kinematic_tree, motion, title=title, fps=opt.fps, radius=opt.radius)
gr.Info("Rendered motion...", duration = 3)
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Rendering time cost: {exc:.2f} s", duration = 3)
video_dis = f'<video controls playsinline width="{width}" style="display: block; margin: 0 auto;" src="./file={save_paths[1]}"></video>'
edit_config = set_all_use_to_false(edit_config)
return video_dis
@spaces.GPU
def generate_example_based_motion(text, chunk_size, example_based_steps_end, temp_seed, temp_seed_bar, num_motion, opt, pipeline):
global edit_config
edit_config.example_based.use = True
edit_config.example_based.chunk_size = chunk_size
edit_config.example_based.example_based_steps_end = example_based_steps_end
edit_config.example_based.temp_seed = temp_seed
edit_config.example_based.temp_seed_bar = temp_seed_bar
gr.Info("Loading Configurations...", duration = 3)
model = build_models(opt, edit_config=edit_config)
ckpt_path = pjoin(opt.model_dir, opt.which_ckpt + '.tar')
niter = load_model_weights(model, ckpt_path, use_ema=not opt.no_ema)
pipeline = DiffusePipeline(
opt = opt,
model = model,
diffuser_name = opt.diffuser_name,
device=opt.device,
num_inference_steps=opt.num_inference_steps,
torch_dtype=torch.float16,
)
width = 500
height = 500
texts = [text for _ in range(num_motion)]
motion_lens = [opt.motion_length * opt.fps for _ in range(opt.num_samples)]
save_dir = './tmp/gen/'
filenames = [generate_md5(str(time.time())) + ".mp4" for _ in range(num_motion)]
save_paths = [pjoin(save_dir, str(filenames[i])) for i in range(num_motion)]
os.makedirs(save_dir, exist_ok=True)
start_time = time.perf_counter()
gr.Info("Generating motion...", duration = 3)
pred_motions, _ = pipeline.generate(texts, torch.LongTensor([int(x) for x in motion_lens]))
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Generating time cost: {exc:.2f} s, rendering starts...", duration = 3)
start_time = time.perf_counter()
mean = np.load(pjoin(opt.meta_dir, 'mean.npy'))
std = np.load(pjoin(opt.meta_dir, 'std.npy'))
samples = []
root_list = []
progress=gr.Progress()
progress(0, desc="Starting...")
for i, motion in enumerate(pred_motions):
motion = motion.cpu().numpy() * std + mean
# 1. recover 3d joints representation by ik
motion = recover_from_ric(torch.from_numpy(motion).float(), opt.joints_num)
# 2. put on Floor (Y axis)
floor_height = motion.min(dim=0)[0].min(dim=0)[0][1]
motion[:, :, 1] -= floor_height
motion = motion.numpy()
# 3. remove jitter
motion = motion_temporal_filter(motion, sigma=1)
samples.append(motion)
video_dis = []
i = 0
for title in progress.tqdm(texts):
print(save_paths[i])
title = texts[i]
motion = samples[i]
kinematic_tree = paramUtil.t2m_kinematic_chain if (opt.dataset_name == 't2m') else paramUtil.kit_kinematic_chain
plot_3d_motion(save_paths[i], kinematic_tree, motion, title=title, fps=opt.fps, radius=opt.radius)
video_html = f'''
<video class="retrieved_video" width="{width}" height="{height}" preload="auto" muted playsinline onpause="this.load()" autoplay loop disablepictureinpicture src="./file={save_paths[i]}"> </video>
'''
video_dis.append(video_html)
i += 1
for _ in range(24 - num_motion):
video_dis.append(None)
gr.Info("Rendered motion...", duration = 3)
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Rendering time cost: {exc:.2f} s", duration = 3)
edit_config = set_all_use_to_false(edit_config)
return video_dis
@spaces.GPU
def transfer_style(text, style_text, style_transfer_steps_end, opt, pipeline):
global edit_config
edit_config.style_tranfer.use = True
edit_config.style_tranfer.style_transfer_steps_end = style_transfer_steps_end
gr.Info("Loading Configurations...", duration = 3)
model = build_models(opt, edit_config=edit_config)
ckpt_path = pjoin(opt.model_dir, opt.which_ckpt + '.tar')
niter = load_model_weights(model, ckpt_path, use_ema=not opt.no_ema)
pipeline = DiffusePipeline(
opt = opt,
model = model,
diffuser_name = opt.diffuser_name,
device=opt.device,
num_inference_steps=opt.num_inference_steps,
torch_dtype=torch.float16,
)
print(edit_config)
width = 500
height = 500
texts = [style_text, text, text]
motion_lens = [opt.motion_length * opt.fps for _ in range(opt.num_samples)]
save_dir = './tmp/gen/'
filenames = [generate_md5(str(time.time())) + ".mp4", generate_md5(str(time.time())) + ".mp4", generate_md5(str(time.time())) + ".mp4"]
save_paths = [pjoin(save_dir, str(filenames[0])), pjoin(save_dir, str(filenames[1])), pjoin(save_dir, str(filenames[2]))]
os.makedirs(save_dir, exist_ok=True)
start_time = time.perf_counter()
gr.Info("Generating motion...", duration = 3)
pred_motions, _ = pipeline.generate(texts, torch.LongTensor([int(x) for x in motion_lens]))
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Generating time cost: {exc:.2f} s, rendering starts...", duration = 3)
start_time = time.perf_counter()
mean = np.load(pjoin(opt.meta_dir, 'mean.npy'))
std = np.load(pjoin(opt.meta_dir, 'std.npy'))
samples = []
root_list = []
for i, motion in enumerate(pred_motions):
motion = motion.cpu().numpy() * std + mean
# 1. recover 3d joints representation by ik
motion = recover_from_ric(torch.from_numpy(motion).float(), opt.joints_num)
# 2. put on Floor (Y axis)
floor_height = motion.min(dim=0)[0].min(dim=0)[0][1]
motion[:, :, 1] -= floor_height
motion = motion.numpy()
# 3. remove jitter
motion = motion_temporal_filter(motion, sigma=1)
samples.append(motion)
for i,title in enumerate(texts):
title = texts[i]
motion = samples[i]
kinematic_tree = paramUtil.t2m_kinematic_chain if (opt.dataset_name == 't2m') else paramUtil.kit_kinematic_chain
plot_3d_motion(save_paths[i], kinematic_tree, motion, title=title, fps=opt.fps, radius=opt.radius)
gr.Info("Rendered motion...", duration = 3)
end_time = time.perf_counter()
exc = end_time - start_time
gr.Info(f"Rendering time cost: {exc:.2f} s", duration = 3)
video_dis0 = f"""<video controls playsinline width="{width}" style="display: block; margin: 0 auto;" src="./file={save_paths[0]}"></video> <br> <p align="center"> Style Reference </p>"""
video_dis1 = f"""<video controls playsinline width="{width}" style="display: block; margin: 0 auto;" src="./file={save_paths[2]}"></video> <br> <p align="center"> Content Reference </p>"""
video_dis2 = f"""<video controls playsinline width="{width}" style="display: block; margin: 0 auto;" src="./file={save_paths[1]}"></video> <br> <p align="center"> Transfered Result </p>"""
edit_config = set_all_use_to_false(edit_config)
return video_dis0, video_dis2
def main():
parser = GenerateOptions()
opt = parser.parse_app()
set_seed(opt.seed)
device_id = opt.gpu_id
device = torch.device('cuda:%d' % device_id if torch.cuda.is_available() else 'cpu')
opt.device = device
print(device)
# load model
model = build_models(opt, edit_config=edit_config)
ckpt_path = pjoin(opt.model_dir, opt.which_ckpt + '.tar')
niter = load_model_weights(model, ckpt_path, use_ema=not opt.no_ema)
pipeline = DiffusePipeline(
opt = opt,
model = model,
diffuser_name = opt.diffuser_name,
device=device,
num_inference_steps=opt.num_inference_steps,
torch_dtype=torch.float16,
)
with gr.Blocks() as demo:
gr.Markdown(HEAD)
with gr.Row():
with gr.Column(scale=7):
text_input = gr.Textbox(label="Input the text prompt to generate motion...")
with gr.Column(scale=3):
sequence_length = gr.Slider(minimum=1, maximum=9.6, step=0.1, label="Motion length", value=8)
with gr.Row():
generate_button = gr.Button("Generate motion")
with gr.Row():
video_display = gr.HTML(label="ηζηθ§ι’", visible=True)
tabs = gr.Tabs(visible=True)
with tabs:
with gr.Tab("Motion (de-)emphasizing"):
with gr.Row():
int_input = gr.Number(label="Editing word index", minimum=0, maximum=70)
weight_input = gr.Slider(minimum=-1, maximum=1, step=0.01, label="Input weight for (de-)emphasizing [-1, 1]", value=0)
trim_button = gr.Button("Edit reweighting")
with gr.Row():
original_video1 = gr.HTML(label="before editing", visible=False)
edited_video = gr.HTML(label="after editing")
trim_button.click(
fn=lambda x, int_input, weight_input : reweighting(x, int_input, weight_input, opt, pipeline),
inputs=[text_input, int_input, weight_input],
outputs=edited_video,
)
with gr.Tab("Example-based motion genration"):
with gr.Row():
with gr.Column(scale=4):
chunk_size = gr.Number(minimum=10, maximum=20, step=10,label="Chunk size (#frames)", value=20)
example_based_steps_end = gr.Number(minimum=0, maximum=9,label="Ending step of manipulation", value=6)
with gr.Column(scale=3):
temp_seed = gr.Number(label="Seed for random", value=200, minimum=0)
temp_seed_bar = gr.Slider(minimum=0, maximum=100, step=1, label="Seed for random bar", value=15)
with gr.Column(scale=3):
num_motion = gr.Radio(choices=[4, 8, 12, 16, 24], value=8, label="Select number of motions")
gen_button = gr.Button("Generate example-based motion")
example_video_display = []
for _ in range(6):
with gr.Row():
for _ in range(4):
video = gr.HTML(label="Example-based motion", visible=True)
example_video_display.append(video)
gen_button.click(
fn=lambda text, chunk_size, example_based_steps_end, temp_seed, temp_seed_bar, num_motion: generate_example_based_motion(text, chunk_size, example_based_steps_end, temp_seed, temp_seed_bar, num_motion, opt, pipeline),
inputs=[text_input, chunk_size, example_based_steps_end, temp_seed, temp_seed_bar, num_motion],
outputs=example_video_display
)
with gr.Tab("Style transfer"):
with gr.Row():
style_text = gr.Textbox(label="Reference prompt (e.g. 'a man walks.')", value="a man walks.")
style_transfer_steps_end = gr.Number(label="The end step of diffusion (0~9)", minimum=0, maximum=9, value=5)
style_transfer_button = gr.Button("Transfer style")
with gr.Row():
style_reference = gr.HTML(label="style reference")
original_video4 = gr.HTML(label="before style transfer", visible=False)
styled_video = gr.HTML(label="after style transfer")
style_transfer_button.click(
fn=lambda text, style_text, style_transfer_steps_end: transfer_style(text, style_text, style_transfer_steps_end, opt, pipeline),
inputs=[text_input, style_text, style_transfer_steps_end],
outputs=[style_reference, styled_video],
)
def update_motion_length(sequence_length):
opt.motion_length = sequence_length
def on_generate(text, length, pipeline):
update_motion_length(length)
return generate_video_from_text(text, opt, pipeline)
generate_button.click(
fn=lambda text, length: on_generate(text, length, pipeline),
inputs=[text_input, sequence_length],
outputs=[
video_display,
original_video1,
original_video4,
tabs,
],
show_progress=True
)
generate_button.click(
fn=lambda: [gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)],
inputs=None,
outputs=[video_display, original_video1, original_video4]
)
demo.launch()
if __name__ == '__main__':
main() |