Spaces:
Runtime error
Runtime error
LeoXing1996
commited on
Commit
·
62ab16e
1
Parent(s):
0f3562d
return state for examples
Browse files- app-huggingface.py +126 -29
app-huggingface.py
CHANGED
|
@@ -37,7 +37,8 @@ def add_text(gif_file: str, mp4_file: str, text_index: int):
|
|
| 37 |
except EOFError:
|
| 38 |
pass
|
| 39 |
|
| 40 |
-
text = ['Happy New Year!', 'Happy New Year!',
|
|
|
|
| 41 |
size = [36, 36, 36, 36][text_index]
|
| 42 |
|
| 43 |
for i, frame in enumerate(frames):
|
|
@@ -55,6 +56,7 @@ def add_text(gif_file: str, mp4_file: str, text_index: int):
|
|
| 55 |
mp4_file = convert_gif_to_mp4(gif_file, mp4_file)
|
| 56 |
return gif_file, mp4_file
|
| 57 |
|
|
|
|
| 58 |
css = """
|
| 59 |
.toolbutton {
|
| 60 |
margin-buttom: 0em 0em 0em 0em;
|
|
@@ -255,6 +257,83 @@ class AnimateController:
|
|
| 255 |
return n_prompt, ip_adapter_scale
|
| 256 |
|
| 257 |
def animate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
self,
|
| 259 |
init_img,
|
| 260 |
motion_scale,
|
|
@@ -269,17 +348,11 @@ class AnimateController:
|
|
| 269 |
with_text=False,
|
| 270 |
text_idx=0,
|
| 271 |
progress=gr.Progress(),
|
| 272 |
-
):
|
| 273 |
|
|
|
|
| 274 |
global sample_idx
|
| 275 |
-
print(example_img)
|
| 276 |
-
|
| 277 |
if init_img is None:
|
| 278 |
-
|
| 279 |
-
gr.Warning('Please upload image or use example images.')
|
| 280 |
-
else:
|
| 281 |
-
gr.Info('Use exmaple image for fast try!')
|
| 282 |
-
init_img = np.array(Image.open(example_img))
|
| 283 |
|
| 284 |
if seed_textbox != -1 and seed_textbox != "":
|
| 285 |
torch.manual_seed(int(seed_textbox))
|
|
@@ -339,11 +412,13 @@ class AnimateController:
|
|
| 339 |
add_text(save_sample_path_gif, save_sample_path_mp4, text_idx)
|
| 340 |
|
| 341 |
sample_idx += 1
|
| 342 |
-
# return (save_sample_path_mp4,
|
| 343 |
-
# gr.update(link=f'file/{save_sample_path_mp4}'),
|
| 344 |
-
# gr.update(link=f'file/{save_sample_path_gif}'))
|
| 345 |
return (save_sample_path_mp4,
|
| 346 |
-
[save_sample_path_mp4, save_sample_path_gif]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
|
| 349 |
controller = AnimateController()
|
|
@@ -472,7 +547,8 @@ def ui():
|
|
| 472 |
result_video = gr.Video(
|
| 473 |
label="Generated Animation", interactive=False)
|
| 474 |
with gr.Row():
|
| 475 |
-
download = gr.Files(
|
|
|
|
| 476 |
|
| 477 |
generate_button.click(
|
| 478 |
fn=controller.animate,
|
|
@@ -486,12 +562,11 @@ def ui():
|
|
| 486 |
seed_textbox,
|
| 487 |
ip_adapter_scale,
|
| 488 |
style_dropdown,
|
| 489 |
-
# None,
|
| 490 |
],
|
| 491 |
outputs=[result_video, download])
|
| 492 |
|
| 493 |
default_1.click(
|
| 494 |
-
fn=controller.
|
| 495 |
inputs=[
|
| 496 |
init_img,
|
| 497 |
default_motion,
|
|
@@ -506,9 +581,16 @@ def ui():
|
|
| 506 |
with_wishes,
|
| 507 |
default_1_idx,
|
| 508 |
],
|
| 509 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
default_2.click(
|
| 511 |
-
fn=controller.
|
| 512 |
inputs=[
|
| 513 |
init_img,
|
| 514 |
default_motion,
|
|
@@ -523,9 +605,16 @@ def ui():
|
|
| 523 |
with_wishes,
|
| 524 |
default_2_idx,
|
| 525 |
],
|
| 526 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
default_3.click(
|
| 528 |
-
fn=controller.
|
| 529 |
inputs=[
|
| 530 |
init_img,
|
| 531 |
default_motion,
|
|
@@ -540,9 +629,16 @@ def ui():
|
|
| 540 |
with_wishes,
|
| 541 |
default_3_idx,
|
| 542 |
],
|
| 543 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
default_4.click(
|
| 545 |
-
fn=controller.
|
| 546 |
inputs=[
|
| 547 |
init_img,
|
| 548 |
default_motion,
|
|
@@ -557,13 +653,14 @@ def ui():
|
|
| 557 |
with_wishes,
|
| 558 |
default_4_idx,
|
| 559 |
],
|
| 560 |
-
outputs=[
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
|
|
|
| 567 |
|
| 568 |
def create_example(input_list):
|
| 569 |
return gr.Examples(
|
|
|
|
| 37 |
except EOFError:
|
| 38 |
pass
|
| 39 |
|
| 40 |
+
text = ['Happy New Year!', 'Happy New Year!',
|
| 41 |
+
'Happy New Year!', 'Happy New Year!'][text_index]
|
| 42 |
size = [36, 36, 36, 36][text_index]
|
| 43 |
|
| 44 |
for i, frame in enumerate(frames):
|
|
|
|
| 56 |
mp4_file = convert_gif_to_mp4(gif_file, mp4_file)
|
| 57 |
return gif_file, mp4_file
|
| 58 |
|
| 59 |
+
|
| 60 |
css = """
|
| 61 |
.toolbutton {
|
| 62 |
margin-buttom: 0em 0em 0em 0em;
|
|
|
|
| 257 |
return n_prompt, ip_adapter_scale
|
| 258 |
|
| 259 |
def animate(
|
| 260 |
+
self,
|
| 261 |
+
init_img,
|
| 262 |
+
motion_scale,
|
| 263 |
+
prompt_textbox,
|
| 264 |
+
negative_prompt_textbox,
|
| 265 |
+
sample_step_slider,
|
| 266 |
+
cfg_scale_slider,
|
| 267 |
+
seed_textbox,
|
| 268 |
+
ip_adapter_scale,
|
| 269 |
+
style,
|
| 270 |
+
progress=gr.Progress(),
|
| 271 |
+
):
|
| 272 |
+
|
| 273 |
+
global sample_idx
|
| 274 |
+
|
| 275 |
+
if init_img is None:
|
| 276 |
+
gr.Warning('Please upload image or use example images.')
|
| 277 |
+
|
| 278 |
+
if seed_textbox != -1 and seed_textbox != "":
|
| 279 |
+
torch.manual_seed(int(seed_textbox))
|
| 280 |
+
seed = int(seed_textbox)
|
| 281 |
+
else:
|
| 282 |
+
seed = torch.initial_seed()
|
| 283 |
+
generator = torch.Generator(device='cuda')
|
| 284 |
+
generator.manual_seed(seed)
|
| 285 |
+
seed_everything(seed)
|
| 286 |
+
|
| 287 |
+
print(f'Seed: {seed}')
|
| 288 |
+
|
| 289 |
+
pipeline = self.pipeline_dict[style]
|
| 290 |
+
init_img, h, w = preprocess_img(init_img)
|
| 291 |
+
print(f'img size: {h, w}')
|
| 292 |
+
|
| 293 |
+
sample = pipeline(
|
| 294 |
+
image=init_img,
|
| 295 |
+
prompt=prompt_textbox,
|
| 296 |
+
negative_prompt=negative_prompt_textbox,
|
| 297 |
+
generator=generator,
|
| 298 |
+
num_inference_steps=sample_step_slider,
|
| 299 |
+
guidance_scale=cfg_scale_slider,
|
| 300 |
+
width=w,
|
| 301 |
+
height=h,
|
| 302 |
+
video_length=16,
|
| 303 |
+
mask_sim_template_idx=motion_scale - 1,
|
| 304 |
+
ip_adapter_scale=ip_adapter_scale,
|
| 305 |
+
progress_fn=progress,
|
| 306 |
+
).videos
|
| 307 |
+
|
| 308 |
+
save_sample_path_mp4 = os.path.join(
|
| 309 |
+
self.savedir_sample, f"{sample_idx}.mp4")
|
| 310 |
+
save_sample_path_gif = os.path.join(
|
| 311 |
+
self.savedir_sample, f"{sample_idx}.gif")
|
| 312 |
+
|
| 313 |
+
save_videos_grid(sample, save_sample_path_mp4)
|
| 314 |
+
save_videos_grid(sample, save_sample_path_gif)
|
| 315 |
+
|
| 316 |
+
sample_config = {
|
| 317 |
+
"prompt": prompt_textbox,
|
| 318 |
+
"n_prompt": negative_prompt_textbox,
|
| 319 |
+
"num_inference_steps": sample_step_slider,
|
| 320 |
+
"guidance_scale": cfg_scale_slider,
|
| 321 |
+
"width": w,
|
| 322 |
+
"height": h,
|
| 323 |
+
"seed": seed,
|
| 324 |
+
"motion": motion_scale,
|
| 325 |
+
}
|
| 326 |
+
print(sample_config)
|
| 327 |
+
json_str = json.dumps(sample_config, indent=4)
|
| 328 |
+
with open(os.path.join(self.savedir, "logs.json"), "a") as f:
|
| 329 |
+
f.write(json_str)
|
| 330 |
+
f.write("\n\n")
|
| 331 |
+
|
| 332 |
+
sample_idx += 1
|
| 333 |
+
return (save_sample_path_mp4,
|
| 334 |
+
[save_sample_path_mp4, save_sample_path_gif])
|
| 335 |
+
|
| 336 |
+
def animate_example(
|
| 337 |
self,
|
| 338 |
init_img,
|
| 339 |
motion_scale,
|
|
|
|
| 348 |
with_text=False,
|
| 349 |
text_idx=0,
|
| 350 |
progress=gr.Progress(),
|
|
|
|
| 351 |
|
| 352 |
+
):
|
| 353 |
global sample_idx
|
|
|
|
|
|
|
| 354 |
if init_img is None:
|
| 355 |
+
gr.Warning('Please upload image or use example images.')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
if seed_textbox != -1 and seed_textbox != "":
|
| 358 |
torch.manual_seed(int(seed_textbox))
|
|
|
|
| 412 |
add_text(save_sample_path_gif, save_sample_path_mp4, text_idx)
|
| 413 |
|
| 414 |
sample_idx += 1
|
|
|
|
|
|
|
|
|
|
| 415 |
return (save_sample_path_mp4,
|
| 416 |
+
[save_sample_path_mp4, save_sample_path_gif],
|
| 417 |
+
example_img,
|
| 418 |
+
seed,
|
| 419 |
+
motion_scale,
|
| 420 |
+
cfg_scale_slider,
|
| 421 |
+
)
|
| 422 |
|
| 423 |
|
| 424 |
controller = AnimateController()
|
|
|
|
| 547 |
result_video = gr.Video(
|
| 548 |
label="Generated Animation", interactive=False)
|
| 549 |
with gr.Row():
|
| 550 |
+
download = gr.Files(
|
| 551 |
+
file_types=['gif', 'mp4'], label='Donwload Output')
|
| 552 |
|
| 553 |
generate_button.click(
|
| 554 |
fn=controller.animate,
|
|
|
|
| 562 |
seed_textbox,
|
| 563 |
ip_adapter_scale,
|
| 564 |
style_dropdown,
|
|
|
|
| 565 |
],
|
| 566 |
outputs=[result_video, download])
|
| 567 |
|
| 568 |
default_1.click(
|
| 569 |
+
fn=controller.animate_example,
|
| 570 |
inputs=[
|
| 571 |
init_img,
|
| 572 |
default_motion,
|
|
|
|
| 581 |
with_wishes,
|
| 582 |
default_1_idx,
|
| 583 |
],
|
| 584 |
+
outputs=[
|
| 585 |
+
result_video,
|
| 586 |
+
download,
|
| 587 |
+
example_img,
|
| 588 |
+
default_seed,
|
| 589 |
+
default_motion,
|
| 590 |
+
default_cfg,
|
| 591 |
+
])
|
| 592 |
default_2.click(
|
| 593 |
+
fn=controller.animate_example,
|
| 594 |
inputs=[
|
| 595 |
init_img,
|
| 596 |
default_motion,
|
|
|
|
| 605 |
with_wishes,
|
| 606 |
default_2_idx,
|
| 607 |
],
|
| 608 |
+
outputs=[
|
| 609 |
+
result_video,
|
| 610 |
+
download,
|
| 611 |
+
example_img,
|
| 612 |
+
default_seed,
|
| 613 |
+
default_motion,
|
| 614 |
+
default_cfg,
|
| 615 |
+
])
|
| 616 |
default_3.click(
|
| 617 |
+
fn=controller.animate_example,
|
| 618 |
inputs=[
|
| 619 |
init_img,
|
| 620 |
default_motion,
|
|
|
|
| 629 |
with_wishes,
|
| 630 |
default_3_idx,
|
| 631 |
],
|
| 632 |
+
outputs=[
|
| 633 |
+
result_video,
|
| 634 |
+
download,
|
| 635 |
+
example_img,
|
| 636 |
+
default_seed,
|
| 637 |
+
default_motion,
|
| 638 |
+
default_cfg,
|
| 639 |
+
])
|
| 640 |
default_4.click(
|
| 641 |
+
fn=controller.animate_example,
|
| 642 |
inputs=[
|
| 643 |
init_img,
|
| 644 |
default_motion,
|
|
|
|
| 653 |
with_wishes,
|
| 654 |
default_4_idx,
|
| 655 |
],
|
| 656 |
+
outputs=[
|
| 657 |
+
result_video,
|
| 658 |
+
download,
|
| 659 |
+
example_img,
|
| 660 |
+
default_seed,
|
| 661 |
+
default_motion,
|
| 662 |
+
default_cfg,
|
| 663 |
+
])
|
| 664 |
|
| 665 |
def create_example(input_list):
|
| 666 |
return gr.Examples(
|