File size: 41,603 Bytes
39049ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
from diffusers_helper.hf_login import login

import os
import random

hf_home = os.environ.get('HF_HOME')
if hf_home is None:
    os.environ['HF_HOME'] = os.path.abspath(os.path.realpath(os.path.join(os.path.dirname(__file__), './hf_download')))

import gradio as gr
import torch
import traceback
import einops
import safetensors.torch as sf
import numpy as np
import argparse
import math
import re

from PIL import Image
from diffusers import AutoencoderKLHunyuanVideo
from transformers import LlamaModel, CLIPTextModel, LlamaTokenizerFast, CLIPTokenizer
from diffusers_helper.hunyuan import encode_prompt_conds, vae_decode, vae_encode, vae_decode_fake
from diffusers_helper.utils import save_bcthw_as_mp4, crop_or_pad_yield_mask, soft_append_bcthw, resize_and_center_crop, state_dict_weighted_merge, state_dict_offset_merge, generate_timestamp
from diffusers_helper.models.hunyuan_video_packed import HunyuanVideoTransformer3DModelPacked
from diffusers_helper.pipelines.k_diffusion_hunyuan import sample_hunyuan
from diffusers_helper.memory import cpu, gpu, get_cuda_free_memory_gb, move_model_to_device_with_memory_preservation, offload_model_from_device_for_memory_preservation, fake_diffusers_current_device, DynamicSwapInstaller, unload_complete_models, load_model_as_complete
from diffusers_helper.thread_utils import AsyncStream, async_run
from diffusers_helper.gradio.progress_bar import make_progress_bar_css, make_progress_bar_html
from transformers import SiglipImageProcessor, SiglipVisionModel
from diffusers_helper.clip_vision import hf_clip_vision_encode
from diffusers_helper.bucket_tools import find_nearest_bucket

import torchvision

def save_bcthw_as_png(x, output_filename):
    # UIと合わせる
    os.makedirs(os.path.dirname(os.path.abspath(os.path.realpath(output_filename))), exist_ok=True)
    x = torch.clamp(x.float(), 0, 1) * 255
    x = x.detach().cpu().to(torch.uint8)
    x = einops.rearrange(x, 'b c t h w -> c (b h) (t w)')
    torchvision.io.write_png(x, output_filename)
    return output_filename

parser = argparse.ArgumentParser()
parser.add_argument('--share', action='store_true')
parser.add_argument("--server", type=str, default='0.0.0.0')
parser.add_argument("--port", type=int, required=False)
parser.add_argument("--inbrowser", action='store_true')
args = parser.parse_args()

# for win desktop probably use --server 127.0.0.1 --inbrowser
# For linux server probably use --server 127.0.0.1 or do not use any cmd flags

print(args)

free_mem_gb = get_cuda_free_memory_gb(gpu)
high_vram = free_mem_gb > 60

print(f'Free VRAM {free_mem_gb} GB')
print(f'High-VRAM Mode: {high_vram}')

text_encoder = LlamaModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder', torch_dtype=torch.float16).cpu()
text_encoder_2 = CLIPTextModel.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='text_encoder_2', torch_dtype=torch.float16).cpu()
tokenizer = LlamaTokenizerFast.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer')
tokenizer_2 = CLIPTokenizer.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='tokenizer_2')
vae = AutoencoderKLHunyuanVideo.from_pretrained("hunyuanvideo-community/HunyuanVideo", subfolder='vae', torch_dtype=torch.float16).cpu()

feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=torch.float16).cpu()

transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained('lllyasviel/FramePackI2V_HY', torch_dtype=torch.bfloat16).cpu()

vae.eval()
text_encoder.eval()
text_encoder_2.eval()
image_encoder.eval()
transformer.eval()

if not high_vram:
    vae.enable_slicing()
    vae.enable_tiling()

transformer.high_quality_fp32_output_for_inference = True
print('transformer.high_quality_fp32_output_for_inference = True')

transformer.to(dtype=torch.bfloat16)
vae.to(dtype=torch.float16)
image_encoder.to(dtype=torch.float16)
text_encoder.to(dtype=torch.float16)
text_encoder_2.to(dtype=torch.float16)

vae.requires_grad_(False)
text_encoder.requires_grad_(False)
text_encoder_2.requires_grad_(False)
image_encoder.requires_grad_(False)
transformer.requires_grad_(False)

if not high_vram:
    # DynamicSwapInstaller is same as huggingface's enable_sequential_offload but 3x faster
    DynamicSwapInstaller.install_model(transformer, device=gpu)
    DynamicSwapInstaller.install_model(text_encoder, device=gpu)
else:
    text_encoder.to(gpu)
    text_encoder_2.to(gpu)
    image_encoder.to(gpu)
    vae.to(gpu)
    transformer.to(gpu)

stream = AsyncStream()

outputs_folder = './outputs/'
os.makedirs(outputs_folder, exist_ok=True)

def loop_worker(input_image, prompt, n_prompt, generation_count, seed, total_second_length, connection_second_length,padding_second_length, loop_num, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, mp4_crf, reduce_file_output, without_preview, output_latent_image, latent_input_file):
    for generation_count_index in range(generation_count):
        if stream.input_queue.top() == 'end':
            stream.output_queue.push(('end', None))
            break
        if generation_count != 1:
            seed = random.randint(0, 2**32 - 1)
        stream.output_queue.push(('generation count', f"Generation index:{generation_count_index + 1}"))
        worker(input_image, prompt, n_prompt, seed, total_second_length, connection_second_length,padding_second_length, loop_num, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, mp4_crf, reduce_file_output, without_preview, output_latent_image, latent_input_file)
    stream.output_queue.push(('end', None))
    
@torch.no_grad()
def worker(input_image, prompt, n_prompt, seed, total_second_length, connection_second_length, padding_second_length, loop_num, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, mp4_crf, reduce_file_output, without_preview, output_latent_image, latent_input_file):
    if stream.input_queue.top() == 'end':
        stream.output_queue.push(('end', None))
        return
    
    if reduce_file_output:
        tmp_filename = "system_preview.mp4"

    total_latent_sections = (total_second_length * 30) / (latent_window_size * 4)
    total_latent_sections = int(max(round(total_latent_sections), 1))
    total_latent_sections = total_second_length
    
    connection_latent_sections = (connection_second_length * 30) / (latent_window_size * 4)
    connection_latent_sections = int(max(round(connection_second_length), 1))
    connection_latent_sections = connection_second_length

    all_latent_section = total_latent_sections + connection_latent_sections
    # print(total_latent_sections)
    # print(connection_latent_sections)
    
    job_id = generate_timestamp()

    stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Starting ...'))))

    try:
        if latent_input_file is None:
            # Clean GPU
            if not high_vram:
                unload_complete_models(
                    text_encoder, text_encoder_2, image_encoder, vae, transformer
                )

            # Text encoding

            stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Text encoding ...'))))

            if not high_vram:
                fake_diffusers_current_device(text_encoder, gpu)  # since we only encode one text - that is one model move and one encode, offload is same time consumption since it is also one load and one encode.
                load_model_as_complete(text_encoder_2, target_device=gpu)

            llama_vec, clip_l_pooler = encode_prompt_conds(prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2)

            if cfg == 1:
                llama_vec_n, clip_l_pooler_n = torch.zeros_like(llama_vec), torch.zeros_like(clip_l_pooler)
            else:
                llama_vec_n, clip_l_pooler_n = encode_prompt_conds(n_prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2)

            llama_vec, llama_attention_mask = crop_or_pad_yield_mask(llama_vec, length=512)
            llama_vec_n, llama_attention_mask_n = crop_or_pad_yield_mask(llama_vec_n, length=512)

            # Processing input image

            stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Image processing ...'))))

            H, W, C = input_image.shape
            height, width = find_nearest_bucket(H, W, resolution=640)
            input_image_np = resize_and_center_crop(input_image, target_width=width, target_height=height)

            if not without_preview:
                Image.fromarray(input_image_np).save(os.path.join(outputs_folder, f'{job_id}_{seed}.png'))

            input_image_pt = torch.from_numpy(input_image_np).float() / 127.5 - 1
            input_image_pt = input_image_pt.permute(2, 0, 1)[None, :, None]
            

            # VAE encoding

            stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'VAE encoding ...'))))

            if not high_vram:
                load_model_as_complete(vae, target_device=gpu)

            start_latent = vae_encode(input_image_pt, vae)

            # CLIP Vision

            stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'CLIP Vision encoding ...'))))

            if not high_vram:
                load_model_as_complete(image_encoder, target_device=gpu)

            image_encoder_output = hf_clip_vision_encode(input_image_np, feature_extractor, image_encoder)
            image_encoder_last_hidden_state = image_encoder_output.last_hidden_state

            # Dtype

            llama_vec = llama_vec.to(transformer.dtype)
            llama_vec_n = llama_vec_n.to(transformer.dtype)
            clip_l_pooler = clip_l_pooler.to(transformer.dtype)
            clip_l_pooler_n = clip_l_pooler_n.to(transformer.dtype)
            image_encoder_last_hidden_state = image_encoder_last_hidden_state.to(transformer.dtype)

            # Sampling

            stream.output_queue.push(('progress', (None, '', make_progress_bar_html(0, 'Start sampling ...'))))

            rnd = torch.Generator("cpu").manual_seed(seed)
            num_frames = latent_window_size * 4 - 3

            ##メイン作成

            history_latents = torch.zeros(size=(1, 16, 1 + 2 + 16, height // 8, width // 8), dtype=torch.float32).cpu()
            history_pixels = None
            total_generated_latent_frames = 0

            
            latent_paddings = [i for i in reversed(range(total_latent_sections))]

            

            if total_latent_sections > 4:
                # In theory the latent_paddings should follow the above sequence, but it seems that duplicating some
                # items looks better than expanding it when total_latent_sections > 4
                # One can try to remove below trick and just
                # use `latent_paddings = list(reversed(range(total_latent_sections)))` to compare
                latent_paddings = [3] + [2] * (total_latent_sections - 3) + [1, 0]

            last_frame = total_second_length * 30
            for latent_padding in latent_paddings:
                is_last_section = latent_padding == 0
                latent_padding_init_size = int(padding_second_length * latent_window_size)

                latent_padding_size = (latent_padding * latent_window_size) + latent_padding_init_size
                print(latent_padding_size)
                if stream.input_queue.top() == 'end':
                    stream.output_queue.push(('end', None))
                    return

                print(f'latent_padding_size = {latent_padding_size}, is_last_section = {is_last_section}')

                indices = torch.arange(0, sum([1, latent_padding_size, latent_window_size, 1, 2, 16])).unsqueeze(0)
                clean_latent_indices_pre, blank_indices, latent_indices, clean_latent_indices_post, clean_latent_2x_indices, clean_latent_4x_indices = indices.split([1, latent_padding_size, latent_window_size, 1, 2, 16], dim=1)
                #print(clean_latent_indices_pre)
                clean_latent_indices = torch.cat([clean_latent_indices_pre, clean_latent_indices_post], dim=1)

                clean_latents_pre = start_latent.to(history_latents)
                clean_latents_post, clean_latents_2x, clean_latents_4x = history_latents[:, :, :1 + 2 + 16, :, :].split([1, 2, 16], dim=2)

                clean_latents = torch.cat([clean_latents_pre, clean_latents_post], dim=2)
                

                if not high_vram:
                    unload_complete_models()
                    move_model_to_device_with_memory_preservation(transformer, target_device=gpu, preserved_memory_gb=gpu_memory_preservation)

                if use_teacache:
                    transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
                else:
                    transformer.initialize_teacache(enable_teacache=False)

                def callback(d):
                    preview = d['denoised']
                    preview = vae_decode_fake(preview)

                    preview = (preview * 255.0).detach().cpu().numpy().clip(0, 255).astype(np.uint8)
                    preview = einops.rearrange(preview, 'b c t h w -> (b h) (t w) c')

                    if stream.input_queue.top() == 'end':
                        stream.output_queue.push(('end', None))
                        raise KeyboardInterrupt('User ends the task.')

                    current_step = d['i'] + 1
                    percentage = int(100.0 * current_step / steps)
                    hint = f'Sampling {current_step}/{steps}'
                    desc = f'Total generated frames: {int(max(0, total_generated_latent_frames * 4 - 3))}, Video length: {max(0, (total_generated_latent_frames * 4 - 3) / 30) :.2f} seconds (FPS-30). The video is being extended now ...'
                    
                    stream.output_queue.push(('progress', (preview, desc, make_progress_bar_html(percentage, hint))))
                    return

                generated_latents = sample_hunyuan(
                    transformer=transformer,
                    sampler='unipc',
                    width=width,
                    height=height,
                    frames=num_frames,
                    real_guidance_scale=cfg,
                    distilled_guidance_scale=gs,
                    guidance_rescale=rs,
                    # shift=3.0,
                    num_inference_steps=steps,
                    generator=rnd,
                    prompt_embeds=llama_vec,
                    prompt_embeds_mask=llama_attention_mask,
                    prompt_poolers=clip_l_pooler,
                    negative_prompt_embeds=llama_vec_n,
                    negative_prompt_embeds_mask=llama_attention_mask_n,
                    negative_prompt_poolers=clip_l_pooler_n,
                    device=gpu,
                    dtype=torch.bfloat16,
                    image_embeddings=image_encoder_last_hidden_state,
                    latent_indices=latent_indices,
                    clean_latents=clean_latents,
                    clean_latent_indices=clean_latent_indices,
                    clean_latents_2x=clean_latents_2x,
                    clean_latent_2x_indices=clean_latent_2x_indices,
                    clean_latents_4x=clean_latents_4x,
                    clean_latent_4x_indices=clean_latent_4x_indices,
                    callback=callback,
                )

                # if is_last_section:
                #     generated_latents = torch.cat([start_latent.to(generated_latents), generated_latents], dim=2)


                total_generated_latent_frames += int(generated_latents.shape[2])
                history_latents = torch.cat([generated_latents.to(history_latents), history_latents], dim=2)

                real_history_latents = history_latents[:, :, :total_generated_latent_frames, :, :]
                
                if not without_preview:
                    if not high_vram:
                        offload_model_from_device_for_memory_preservation(transformer, target_device=gpu, preserved_memory_gb=8)
                        load_model_as_complete(vae, target_device=gpu)

                    if history_pixels is None:
                        history_pixels = vae_decode(real_history_latents, vae).cpu()
                    else:
                        section_latent_frames = (latent_window_size * 2)
                        overlapped_frames = latent_window_size * 4 - 3

                        current_pixels = vae_decode(real_history_latents[:, :, :section_latent_frames], vae).cpu()
                        history_pixels = soft_append_bcthw(current_pixels, history_pixels, overlapped_frames)
                        
                    if not high_vram:
                        unload_complete_models()

                    if reduce_file_output:
                        output_filename = os.path.join(outputs_folder, tmp_filename)
                    else:    
                        output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}_{seed}.mp4')

                    save_bcthw_as_mp4(history_pixels, output_filename, fps=30, crf=mp4_crf)

                    print(f'Decoded. Current latent shape {real_history_latents.shape}; pixel shape {history_pixels.shape}')

                    stream.output_queue.push(('file', output_filename))

                if is_last_section:
                    break
            


            ##コネクション作成


            #post_history_latents = torch.zeros(size=(1, 16, 2 + 16, height // 8, width // 8), dtype=torch.float32).cpu()
            post_history_latents = history_latents[:, :, :total_generated_latent_frames, :, :]
            post_history_pixels = history_pixels
            print(post_history_latents.shape)
            post_total_generated_latent_frames = total_generated_latent_frames
            print("generate post history")
            
            latent_paddings = [i for i in reversed(range(connection_latent_sections))]
            print(latent_paddings)
            if connection_latent_sections > 4:
                # In theory the latent_paddings should follow the above sequence, but it seems that duplicating some
                # items looks better than expanding it when total_latent_sections > 4
                # One can try to remove below trick and just
                # use `latent_paddings = list(reversed(range(total_latent_sections)))` to compare
                latent_paddings = [3] + [2] * (connection_latent_sections - 3) + [1, 0]
            
            if total_latent_sections > 2:
                N = 16
            elif total_latent_sections == 2:
                N= 15
            else:
                N=6
            for latent_padding in latent_paddings:
                print("latent_padding")
                
                is_last_section = latent_padding == 0
                latent_padding_size = latent_padding * latent_window_size

                if stream.input_queue.top() == 'end':
                    stream.output_queue.push(('end', None))
                    return

                print(f'latent_padding_size = {latent_padding_size}, is_last_section = {is_last_section}')

                indices = torch.arange(0, sum([1,latent_padding_size, latent_window_size, 1, 2, N])).unsqueeze(0)
                clean_latent_indices_pre,blank_indices, latent_indices, clean_latent_indices_post, clean_latent_2x_indices, clean_latent_4x_indices = \
                    indices.split([1,latent_padding_size, latent_window_size, 1, 2, N], dim=1)
                
                clean_latent_indices = torch.cat([clean_latent_indices_pre, clean_latent_indices_post], dim=1)
                clean_latent_2x_indices = torch.cat([clean_latent_2x_indices], dim=1)
                clean_latent_4x_indices = torch.cat([clean_latent_4x_indices], dim=1)


                clean_latents_pre  = post_history_latents[:, :, -1:, :, :]
                clean_latents_post, clean_latents_2x, clean_latents_4x = post_history_latents[:, :, :1 + 2 + N, :, :].split([1, 2, N], dim=2)

                clean_latents = torch.cat([clean_latents_pre, clean_latents_post], dim=2)
                clean_latents_2x = torch.cat([clean_latents_2x], dim=2)
                clean_latents_4x = torch.cat([clean_latents_4x], dim=2)

            
                if not high_vram:
                    unload_complete_models()
                    move_model_to_device_with_memory_preservation(transformer, target_device=gpu, preserved_memory_gb=gpu_memory_preservation)

                if use_teacache:
                    transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
                else:
                    transformer.initialize_teacache(enable_teacache=False)

                def callback(d):
                    preview = d['denoised']
                    preview = vae_decode_fake(preview)

                    preview = (preview * 255.0).detach().cpu().numpy().clip(0, 255).astype(np.uint8)
                    preview = einops.rearrange(preview, 'b c t h w -> (b h) (t w) c')

                    if stream.input_queue.top() == 'end':
                        stream.output_queue.push(('end', None))
                        raise KeyboardInterrupt('User ends the task.')

                    current_step = d['i'] + 1
                    percentage = int(100.0 * current_step / steps)
                    hint = f'Sampling {current_step}/{steps}'
                    desc = f'Now making connection video.'
                    stream.output_queue.push(('progress', (preview, desc, make_progress_bar_html(percentage, hint))))
                    return

                generated_latents = sample_hunyuan(
                    transformer=transformer,
                    sampler='unipc',
                    width=width,
                    height=height,
                    frames=num_frames,
                    real_guidance_scale=cfg,
                    distilled_guidance_scale=gs,
                    guidance_rescale=rs,
                    # shift=3.0,
                    num_inference_steps=steps,
                    generator=rnd,
                    prompt_embeds=llama_vec,
                    prompt_embeds_mask=llama_attention_mask,
                    prompt_poolers=clip_l_pooler,
                    negative_prompt_embeds=llama_vec_n,
                    negative_prompt_embeds_mask=llama_attention_mask_n,
                    negative_prompt_poolers=clip_l_pooler_n,
                    device=gpu,
                    dtype=torch.bfloat16,
                    image_embeddings=image_encoder_last_hidden_state,
                    latent_indices=latent_indices,
                    clean_latents=clean_latents,
                    clean_latent_indices=clean_latent_indices,
                    clean_latents_2x=clean_latents_2x,
                    clean_latent_2x_indices=clean_latent_2x_indices,
                    clean_latents_4x=clean_latents_4x,
                    clean_latent_4x_indices=clean_latent_4x_indices,
                    callback=callback,
                )

                # if is_last_section:
                #     generated_latents = torch.cat([start_latent.to(generated_latents), generated_latents], dim=2)


                post_total_generated_latent_frames += int(generated_latents.shape[2])
                post_history_latents = torch.cat([generated_latents.to(post_history_latents), post_history_latents], dim=2)
                
                post_real_history_latents = post_history_latents[:, :, :post_total_generated_latent_frames, :, :]


                if not without_preview:
                    if not high_vram:
                        offload_model_from_device_for_memory_preservation(transformer, target_device=gpu, preserved_memory_gb=8)
                        load_model_as_complete(vae, target_device=gpu)

    
                    if post_history_pixels is None:
                        post_history_pixels = vae_decode(post_real_history_latents, vae).cpu()
                    else:
                        section_latent_frames = (latent_window_size * 2) if is_last_section else (latent_window_size * 2)
                        overlapped_frames = latent_window_size * 4 - 3

                        current_pixels = vae_decode(post_real_history_latents[:, :, :section_latent_frames], vae).cpu()
                        #print(current_pixels.shape)
                        #print(post_history_pixels.shape)
                        post_history_pixels = soft_append_bcthw(current_pixels, post_history_pixels, overlapped_frames)
                        
                    if not high_vram:
                        unload_complete_models()

                    if reduce_file_output:
                        output_filename = os.path.join(outputs_folder, tmp_filename)
                    else:    
                        output_filename = os.path.join(outputs_folder, f'{job_id}_{post_total_generated_latent_frames}_{seed}_post.mp4')

                    save_bcthw_as_mp4(post_history_pixels, output_filename, fps=30, crf=mp4_crf)

                    print(f'Decoded. Current latent shape {post_real_history_latents.shape}; pixel shape {post_history_pixels.shape}')

                    stream.output_queue.push(('file', output_filename))

                if is_last_section:
                    break

    

            #1ループ作成
            
            #print(all_latent_section)

            #print(history_latents.shape)
            connection_hisotry_latents = post_real_history_latents[:,:,:latent_window_size*connection_latent_sections,:,:]
            main_history_latents = real_history_latents[:,:,:latent_window_size*total_latent_sections,:,:]
            


            final_latents = torch.cat([connection_hisotry_latents[:,:,-latent_window_size:,:,:],
                                    main_history_latents,
                                    connection_hisotry_latents,
                                    main_history_latents[:,:,-latent_window_size:,:,:]],dim=2)
            
            if output_latent_image:
                to_pixcel_latents = torch.cat([main_history_latents,
                                connection_hisotry_latents],dim=2)
                
                to_pixcel_latents_png = vae_decode_fake(to_pixcel_latents)
                output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}_{seed}_latent.png')
                save_bcthw_as_png(to_pixcel_latents_png, output_filename)

                output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}_{seed}_latent.pt')
                torch.save(final_latents,output_filename)
                return
        
        else:
            final_latents = torch.load(latent_input_file)   
            filename = os.path.basename(latent_input_file)    
            match = re.match(r"(\d+_\d+_\d+_\d+)_([0-9]+)_([0-9]+)_latent\.pt", filename)

            job_id = match.group(1)  # '250502_204151_409_9042'
            total_generated_latent_frames = match.group(2)  # '9'
            seed = match.group(3)  # '31337'
        
        if not high_vram:
            offload_model_from_device_for_memory_preservation(transformer, target_device=gpu, preserved_memory_gb=8)
            load_model_as_complete(vae, target_device=gpu)
        #final_latents = final_latents.repeat(1,1,loop_num,1,1)
        #print(final_latents.shape)
        final_history_pixels = None
        MAX = all_latent_section + 2
        pixel_map = dict()

        for i in range(MAX):
            if stream.input_queue.top() == 'end':
                stream.output_queue.push(('end', None))
                return

            pixel_map_key = i % all_latent_section
            #print(i)
            latent_index = (all_latent_section-1 - pixel_map_key)
            latent_offset = latent_index*9
            #print(latent_index)
            #print(pixel_map_key)

            percentage = int(100.0 * i / MAX)
            hint = f'Make 1 loop {i}/{MAX}'
            desc = f'Now making 1 loop decoding'
            stream.output_queue.push(('progress', (None, desc, make_progress_bar_html(percentage, hint))))

            section_latent_frames = (latent_window_size * 2) if i == (MAX-1) else (latent_window_size * 2)
            if final_history_pixels is None:
                #if pixel_map.get(latent_index) is None:
                final_history_pixels = vae_decode(final_latents[:,:,latent_offset:latent_offset + latent_window_size,:,:], vae).cpu()
                #pixel_map[latent_index] = final_history_pixels
            else:
                    
                current_pixels = pixel_map.get(latent_index)
                if current_pixels is None:
                    current_pixels = vae_decode(final_latents[:, :, latent_offset:latent_offset + section_latent_frames], vae).cpu()
                    pixel_map[latent_index] = current_pixels

                overlapped_frames = latent_window_size * 4 - 3
                #print(section_latent_frames)
                #print(current_pixels.shape)
                #print(final_history_pixels.shape)
                final_history_pixels = soft_append_bcthw(current_pixels, final_history_pixels, overlapped_frames)
        
        # ループ1素材だけを取るために前1セクションフレームと、後ろ1セクションフレーム - 3を削除
        final_history_pixels = final_history_pixels[:,:,latent_window_size * 4:,:,:]
        final_history_pixels = final_history_pixels[:,:,:-(latent_window_size* 4 - 3),:,:]
        print(final_history_pixels.shape)
        output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}_{seed}_1loop_{loop_num}.mp4')
        save_bcthw_as_mp4(final_history_pixels, output_filename, fps=30, crf=mp4_crf)
        
        final_history_pixels = final_history_pixels.repeat(1,1,loop_num,1,1)
        output_filename = os.path.join(outputs_folder, f'{job_id}_{total_generated_latent_frames}_{seed}_loop_{loop_num}.mp4')

        save_bcthw_as_mp4(final_history_pixels, output_filename, fps=30, crf=mp4_crf)
        stream.output_queue.push(('file', output_filename))
    except:
        traceback.print_exc()

        if not high_vram:
            unload_complete_models(
                text_encoder, text_encoder_2, image_encoder, vae, transformer
            )
    return


def process(input_image, prompt, n_prompt, generation_count, seed, total_second_length, connection_second_length,padding_second_length, loop_num, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, mp4_crf,progress_preview_option, latent_input_file):
    global stream
    # 表示名と値のマッピング
    options = {
        "All Progress File Output": 1,
        "Reduce Progress File Output": 2,
        "Without Preview": 3,
        "Without VAE Decode": 4,
        "Decode Latent File": 5
    }

    if options[progress_preview_option] == 1:
        reduce_file_output = False
        without_preview = False
        output_latent_image = False
        latent_input_file = None
    elif options[progress_preview_option] == 2:
        reduce_file_output = True
        without_preview = False
        output_latent_image = False
        latent_input_file = None
    elif options[progress_preview_option] == 3:
        reduce_file_output = True
        without_preview = True
        output_latent_image = False
        latent_input_file = None
    elif options[progress_preview_option] == 4:
        reduce_file_output = True
        without_preview = True
        output_latent_image = True
        latent_input_file = None
    elif options[progress_preview_option] == 5:
        reduce_file_output = True
        without_preview = True
        output_latent_image = False

    if options[progress_preview_option] != 5:
        assert input_image is not None, 'No input image!'
    else:
        assert latent_input_file is not None, 'No input Lantet file!'




    yield None, None, '', '', gr.update(interactive=False), gr.update(interactive=True), ''

    stream = AsyncStream()

    async_run(loop_worker, input_image, prompt, n_prompt, generation_count, seed, total_second_length,connection_second_length,padding_second_length, loop_num, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, mp4_crf, reduce_file_output, without_preview, output_latent_image, latent_input_file)

    output_filename = None

    while True:
        flag, data = stream.output_queue.next()

        if flag == 'file':
            output_filename = data
            yield output_filename, gr.update(), gr.update(), gr.update(), gr.update(interactive=False), gr.update(interactive=True), gr.update()

        if flag == 'progress':
            preview, desc, html = data
            yield gr.update(), gr.update(visible=True, value=preview), desc, html, gr.update(interactive=False), gr.update(interactive=True), gr.update()
        
        if flag == 'generation count':
            generation_count_index = data
            yield gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), generation_count_index

        if flag == 'end':
            yield output_filename, gr.update(visible=False), '', '', gr.update(interactive=True), gr.update(interactive=False), ''
            break


def end_process():
    stream.input_queue.push('end')


quick_prompts = [
    'The girl dances gracefully, with clear movements, full of charm.',
    'A character doing some simple body movements.',
]
quick_prompts = [[x] for x in quick_prompts]


css = make_progress_bar_css()
block = gr.Blocks(css=css).queue()
with block:
    gr.Markdown('# FramePackLoop')
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(sources='upload', type="numpy", label="Image", height=320)
            def process_file(file):
                return file
            file_input = gr.File(label="Latentファイルをアップロード",file_types=[".pt"],height=30)
            prompt = gr.Textbox(label="Prompt", value='')
            example_quick_prompts = gr.Dataset(samples=quick_prompts, label='Quick List', samples_per_page=1000, components=[prompt])
            example_quick_prompts.click(lambda x: x[0], inputs=[example_quick_prompts], outputs=prompt, show_progress=False, queue=False)
           
            with gr.Row():
                start_button = gr.Button(value="Start Generation")
                end_button = gr.Button(value="End Generation", interactive=False)
                

            with gr.Group():
                use_teacache = gr.Checkbox(label='Use TeaCache', value=True, info='Faster speed, but often makes hands and fingers slightly worse.')

                n_prompt = gr.Textbox(label="Negative Prompt", value="", visible=False)  # Not used
                
                generation_count = gr.Slider(label="Generation Count", minimum=1, maximum=500, value=1, step=1,
                                                  info='生成回数です。この値が2以上の場合、Seedはランダムな値が使用されます。')
        
                progress_preview_option = gr.Radio(
                    choices=["All Progress File Output",
                            "Reduce Progress File Output",
                            "Without Preview",
                            "Without VAE Decode",
                            "Decode Latent File"],        
                    label="Progress Option",
                    info = "経過動画のプレビューとファイルの保存方式を設定します。説明は下記。",
                    value="Reduce Progress File Output"                        # ←ここでデフォルト選択!
                )
                gr.Markdown("""          
                    - **All Progress File Output**: すべての経過ファイルを出力します。  
                    - **Reduce Progress File Output**: 途中経過のファイルを同じ名前で上書き保存し、出力ファイル数を減らします。  
                            outputフォルダに system_preview.mp4 というファイルが生成され、プレビュー用に使用されます。  
                            ※動画生成中はこのファイルを開かないでください。  
                    - **Without Preview**: 途中経過のプレビューは出力されません。入力画像や経過ファイルも保存されず、最終的な出力のみが行われます。そのため、最終アウトプットの生成速度がやや向上します。  
                    - **Without VAE Decode**: (上級者向け)Latentを画像化したImageとLatentファイルを出力します。
                    - **Decode Latent File**: (上級者向け)指定したLatentファイルを動画にします。
                    """,)
                #reduce_file_output = gr.Checkbox(label='Reduce File Output', value=False, info='途中経過のファイルの出力を減らします.ただし、outputフォルダにsystem_progress_preview.mp4というファイルが生成されます。これは、プレビューに使用する動画ファイルです。動画生成中は開かないでください。')
                #without_preview = gr.Checkbox(label='Without Preview', value=False, info='途中経過の動画のプレビューが出力されません。その代わりに、最終アウトプットの出力が早くなります。')

                seed = gr.Number(label="Seed", value=31337, precision=0, info='この値はGeneration Countが1の時のみ有効です。')

                total_second_length = gr.Slider(label="Main Video Length (Section)", minimum=1, maximum=120, value=1, step=1)
                connection_second_length = gr.Slider(label="Connection Video Length (Section)", minimum=1, maximum=120, value=1, step=1)
                latent_window_size = gr.Slider(label="Latent Window Size", minimum=1, maximum=33, value=9, step=1, visible=False)  # Should not change

                def update_text(slider_value,latent_window_size):
                    return str(int(slider_value * latent_window_size))
                with gr.Group():
                    padding_second_length = gr.Slider(label="Padding Video Length (Section)", minimum=0, maximum=10, value=0, step=0.1, info="この値を0にすると元画像を経由します。1以上にすると、元画像から離れます。")
                    text = gr.Textbox(label="Padding Video Length Checker", info="この値が変わらなければ、Padding Video Length (Section)の変更の効果はありません。")
                    padding_second_length.change(fn=update_text, inputs=[padding_second_length,latent_window_size], outputs=text)
                loop_num = gr.Slider(label="Loop Num", minimum=1, maximum=100, value=5, step=1, info='Loop num.')

                steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=25, step=1, info='Changing this value is not recommended.')

                cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=32.0, value=1.0, step=0.01, visible=False)  # Should not change
                gs = gr.Slider(label="Distilled CFG Scale", minimum=1.0, maximum=32.0, value=10.0, step=0.01, info='Changing this value is not recommended.')
                rs = gr.Slider(label="CFG Re-Scale", minimum=0.0, maximum=1.0, value=0.0, step=0.01, visible=False)  # Should not change

                gpu_memory_preservation = gr.Slider(label="GPU Inference Preserved Memory (GB) (larger means slower)", minimum=6, maximum=128, value=6, step=0.1, info="Set this number to a larger value if you encounter OOM. Larger value causes slower speed.")

                mp4_crf = gr.Slider(label="MP4 Compression", minimum=0, maximum=100, value=16, step=1, info="Lower means better quality. 0 is uncompressed. Change to 16 if you get black outputs. ")

        with gr.Column():
            preview_image = gr.Image(label="Next Latents", height=200, visible=False)
            result_video = gr.Video(label="Finished Frames", autoplay=True, show_share_button=False, height=512, loop=True)
            gr.Markdown('Note that the ending actions will be generated before the starting actions due to the inverted sampling. If the starting action is not in the video, you just need to wait, and it will be generated later.')
            progress_desc = gr.Markdown('', elem_classes='no-generating-animation')
            progress_bar = gr.HTML('', elem_classes='no-generating-animation')
            progress_gcounter = gr.Markdown('', elem_classes='no-generating-animation')

    ips = [input_image, prompt, n_prompt, generation_count, seed, total_second_length, connection_second_length,padding_second_length, loop_num, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, mp4_crf, progress_preview_option, file_input]
    start_button.click(fn=process, inputs=ips, outputs=[result_video, preview_image, progress_desc, progress_bar, start_button, end_button, progress_gcounter])
    end_button.click(fn=end_process)


block.launch(
    server_name=args.server,
    server_port=args.port,
    share=args.share,
    inbrowser=args.inbrowser,
)