File size: 26,236 Bytes
0537464
 
 
b720739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b86783
b720739
 
 
 
 
 
 
1b86783
b720739
 
 
 
 
 
 
f29396c
8732b40
 
f29396c
 
 
8732b40
f29396c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
076e3f4
f29396c
076e3f4
 
f29396c
076e3f4
f29396c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8732b40
 
 
f29396c
 
 
 
 
 
 
 
 
 
 
8732b40
f29396c
8732b40
b720739
1b86783
b720739
 
 
 
 
 
1b86783
b720739
 
d50658d
b720739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec96039
b720739
 
d50658d
b720739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481a175
 
 
 
 
c0724a8
481a175
1b86783
b720739
 
dbab9a0
b720739
1b86783
 
b720739
 
 
 
 
1b86783
 
 
 
b720739
 
 
 
 
 
 
481a175
 
b720739
 
 
 
 
481a175
b720739
 
 
 
 
1b86783
b720739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa5e39d
 
 
1b86783
aa5e39d
b720739
 
 
 
 
 
1b86783
b720739
 
 
 
 
aa5e39d
 
 
 
 
1b86783
aa5e39d
 
1b86783
 
 
aa5e39d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b86783
 
aa5e39d
 
 
b720739
 
 
 
 
1b86783
 
 
 
 
aa5e39d
 
 
 
 
 
 
 
 
1b86783
 
aa5e39d
 
 
 
 
 
 
 
 
b720739
64c9783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b720739
89e61fd
 
 
 
 
64c9783
 
89e61fd
5609307
 
64c9783
5609307
 
 
 
 
64c9783
 
 
5fca4b4
1b86783
5fca4b4
b720739
 
 
 
64c9783
 
 
 
 
8732b40
64c9783
 
 
6daf741
 
64c9783
 
 
53506d7
64c9783
 
b720739
 
 
1b86783
 
 
 
 
 
dbab9a0
1b86783
 
481a175
b720739
 
 
1b86783
 
5609307
 
b720739
89e61fd
5609307
 
b720739
1b86783
 
 
 
aa5e39d
b720739
8732b40
 
 
 
 
b720739
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)

from huggingface_hub import snapshot_download, hf_hub_download

snapshot_download(
    repo_id="Wan-AI/Wan2.1-T2V-1.3B",
    local_dir="wan_models/Wan2.1-T2V-1.3B",
    local_dir_use_symlinks=False,
    resume_download=True,
    repo_type="model" 
)

hf_hub_download(
    repo_id="gdhe17/Self-Forcing",
    filename="checkpoints/self_forcing_dmd.pt",
    local_dir=".",              
    local_dir_use_symlinks=False 
)

import os
import re
import random
import argparse
import hashlib
import urllib.request
import time
from PIL import Image
import spaces
import numpy as np
import torch
import gradio as gr
from omegaconf import OmegaConf
from tqdm import tqdm
import imageio

# Original project imports
from pipeline import CausalInferencePipeline
from demo_utils.constant import ZERO_VAE_CACHE
from demo_utils.vae_block3 import VAEDecoderWrapper
from utils.wan_wrapper import WanDiffusionWrapper, WanTextEncoder

from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM

device = "cuda" if torch.cuda.is_available() else "cpu"

model_checkpoint = "meta-llama/Meta-Llama-3-8B-Instruct" 

tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
)

model = AutoModelForCausalLM.from_pretrained(
    model_checkpoint,
    torch_dtype=torch.bfloat16, 
    attn_implementation="flash_attention_2",
    quantization_config=quantization_config,
    device_map="auto"
)
enhancer = pipeline(
    'text-generation',
    model=model,
    tokenizer=tokenizer,
    repetition_penalty=1.2,
)

T2V_CINEMATIC_PROMPT = """You are an expert cinematic director with many award winning movies, When writing prompts based on the user input, focus on detailed, chronological descriptions of actions and scenes.
Include specific movements, appearances, camera angles, and environmental details - all in a single flowing paragraph.
Start directly with the action, and keep descriptions literal and precise.
Think like a cinematographer describing a shot list.
Do not change the user input intent, just enhance it.
Keep within 150 words.
For best results, build your prompts using this structure:
Start with main action in a single sentence
Add specific details about movements and gestures
Describe character/object appearances precisely
Include background and environment details
Specify camera angles and movements
Describe lighting and colors
Note any changes or sudden events
Do not exceed the 150 word limit!
Output the enhanced prompt only.
"""

@spaces.GPU
def enhance_prompt(prompt):
    messages = [
        {"role": "system", "content": T2V_CINEMATIC_PROMPT},
        {"role": "user", "content": f"user_prompt: {prompt}"},
    ]
    answer = enhancer(
        messages,
        max_new_tokens=256,
        return_full_text=False, 
        pad_token_id=tokenizer.eos_token_id
    )
    
    final_answer = answer[0]['generated_text']
    return final_answer.strip()

# --- Argument Parsing ---
parser = argparse.ArgumentParser(description="Gradio Demo for Self-Forcing with Frame Streaming")
parser.add_argument('--port', type=int, default=7860, help="Port to run the Gradio app on.")
parser.add_argument('--host', type=str, default='0.0.0.0', help="Host to bind the Gradio app to.")
parser.add_argument("--checkpoint_path", type=str, default='./checkpoints/self_forcing_dmd.pt', help="Path to the model checkpoint.")
parser.add_argument("--config_path", type=str, default='./configs/self_forcing_dmd.yaml', help="Path to the model config.")
parser.add_argument('--share', action='store_true', help="Create a public Gradio link.")
parser.add_argument('--trt', action='store_true', help="Use TensorRT optimized VAE decoder.")
parser.add_argument('--fps', type=float, default=15.0, help="Playback FPS for frame streaming.")
args = parser.parse_args()

gpu = "cuda"

try:
    config = OmegaConf.load(args.config_path)
    default_config = OmegaConf.load("configs/default_config.yaml")
    config = OmegaConf.merge(default_config, config)
except FileNotFoundError as e:
    print(f"Error loading config file: {e}\n. Please ensure config files are in the correct path.")
    exit(1)

# Initialize Models
print("Initializing models...")
text_encoder = WanTextEncoder()
transformer = WanDiffusionWrapper(is_causal=True)

try:
    state_dict = torch.load(args.checkpoint_path, map_location="cpu")
    transformer.load_state_dict(state_dict.get('generator_ema', state_dict.get('generator')))
except FileNotFoundError as e:
    print(f"Error loading checkpoint: {e}\nPlease ensure the checkpoint '{args.checkpoint_path}' exists.")
    exit(1)

text_encoder.eval().to(dtype=torch.float16).requires_grad_(False)
transformer.eval().to(dtype=torch.float16).requires_grad_(False)

text_encoder.to(gpu)
transformer.to(gpu)

APP_STATE = {
    "torch_compile_applied": False,
    "fp8_applied": False,
    "current_use_taehv": False,
    "current_vae_decoder": None,
}

def initialize_vae_decoder(use_taehv=False, use_trt=False):
    if use_trt:
        from demo_utils.vae import VAETRTWrapper
        print("Initializing TensorRT VAE Decoder...")
        vae_decoder = VAETRTWrapper()
        APP_STATE["current_use_taehv"] = False
    elif use_taehv:
        print("Initializing TAEHV VAE Decoder...")
        from demo_utils.taehv import TAEHV
        taehv_checkpoint_path = "checkpoints/taew2_1.pth"
        if not os.path.exists(taehv_checkpoint_path):
            print(f"Downloading TAEHV checkpoint to {taehv_checkpoint_path}...")
            os.makedirs("checkpoints", exist_ok=True)
            download_url = "https://github.com/madebyollin/taehv/raw/main/taew2_1.pth"
            try:
                urllib.request.urlretrieve(download_url, taehv_checkpoint_path)
            except Exception as e:
                raise RuntimeError(f"Failed to download taew2_1.pth: {e}")
        
        class DotDict(dict): __getattr__ = dict.get
        
        class TAEHVDiffusersWrapper(torch.nn.Module):
            def __init__(self):
                super().__init__()
                self.dtype = torch.float16
                self.taehv = TAEHV(checkpoint_path=taehv_checkpoint_path).to(self.dtype)
                self.config = DotDict(scaling_factor=1.0)
            def decode(self, latents, return_dict=None):
                return self.taehv.decode_video(latents, parallel=not LOW_MEMORY).mul_(2).sub_(1)
        
        vae_decoder = TAEHVDiffusersWrapper()
        APP_STATE["current_use_taehv"] = True
    else:
        print("Initializing Default VAE Decoder...")
        vae_decoder = VAEDecoderWrapper()
        try:
            vae_state_dict = torch.load('wan_models/Wan2.1-T2V-1.3B/Wan2.1_VAE.pth', map_location="cpu")
            decoder_state_dict = {k: v for k, v in vae_state_dict.items() if 'decoder.' in k or 'conv2' in k}
            vae_decoder.load_state_dict(decoder_state_dict)
        except FileNotFoundError:
            print("Warning: Default VAE weights not found.")
        APP_STATE["current_use_taehv"] = False

    vae_decoder.eval().to(dtype=torch.float16).requires_grad_(False).to(gpu)
    APP_STATE["current_vae_decoder"] = vae_decoder
    print(f"βœ… VAE decoder initialized: {'TAEHV' if use_taehv else 'Default VAE'}")

# Initialize with default VAE
initialize_vae_decoder(use_taehv=False, use_trt=args.trt)

pipeline = CausalInferencePipeline(
    config, device=gpu, generator=transformer, text_encoder=text_encoder, 
    vae=APP_STATE["current_vae_decoder"]
)

pipeline.to(dtype=torch.float16).to(gpu)

# --- Frame Streaming Video Generation Handler ---
@torch.no_grad()
@spaces.GPU
def video_generation_handler(prompt, seed=42, fps=15):
    """
    Generator function that yields RGB frames for display in gr.Image.
    Includes timing delays for smooth playback.
    """
    if seed == -1: 
        seed = random.randint(0, 2**32 - 1)
    
    print(f"🎬 Starting video generation with prompt: '{prompt}' and seed: {seed}")
    
    # Calculate frame delay based on FPS
    frame_delay = 1.0 / fps if fps > 0 else 1.0 / 15.0
    
    print("πŸ”€ Encoding text prompt...")
    conditional_dict = text_encoder(text_prompts=[prompt])
    for key, value in conditional_dict.items():
        conditional_dict[key] = value.to(dtype=torch.float16)
    
    # --- Generation Loop ---
    rnd = torch.Generator(gpu).manual_seed(int(seed))
    pipeline._initialize_kv_cache(1, torch.float16, device=gpu)
    pipeline._initialize_crossattn_cache(1, torch.float16, device=gpu)
    noise = torch.randn([1, 21, 16, 60, 104], device=gpu, dtype=torch.float16, generator=rnd)
    
    vae_cache, latents_cache = None, None
    if not APP_STATE["current_use_taehv"] and not args.trt:
        vae_cache = [c.to(device=gpu, dtype=torch.float16) for c in ZERO_VAE_CACHE]

    num_blocks = 7
    current_start_frame = 0
    all_num_frames = [pipeline.num_frame_per_block] * num_blocks
    
    total_frames_yielded = 0
    all_frames_for_video = []
    
    for idx, current_num_frames in enumerate(all_num_frames):
        print(f"πŸ“¦ Processing block {idx+1}/{num_blocks} with {current_num_frames} frames")
        
        noisy_input = noise[:, current_start_frame : current_start_frame + current_num_frames]

        for step_idx, current_timestep in enumerate(pipeline.denoising_step_list):
            timestep = torch.ones([1, current_num_frames], device=noise.device, dtype=torch.int64) * current_timestep
            _, denoised_pred = pipeline.generator(
                noisy_image_or_video=noisy_input, conditional_dict=conditional_dict,
                timestep=timestep, kv_cache=pipeline.kv_cache1,
                crossattn_cache=pipeline.crossattn_cache,
                current_start=current_start_frame * pipeline.frame_seq_length
            )
            if step_idx < len(pipeline.denoising_step_list) - 1:
                next_timestep = pipeline.denoising_step_list[step_idx + 1]
                noisy_input = pipeline.scheduler.add_noise(
                    denoised_pred.flatten(0, 1), torch.randn_like(denoised_pred.flatten(0, 1)),
                    next_timestep * torch.ones([1 * current_num_frames], device=noise.device, dtype=torch.long)
                ).unflatten(0, denoised_pred.shape[:2])

        if idx < len(all_num_frames) - 1:
            pipeline.generator(
                noisy_image_or_video=denoised_pred, conditional_dict=conditional_dict,
                timestep=torch.zeros_like(timestep), kv_cache=pipeline.kv_cache1,
                crossattn_cache=pipeline.crossattn_cache,
                current_start=current_start_frame * pipeline.frame_seq_length,
            )

        # Decode to pixels
        if args.trt:
            pixels, vae_cache = pipeline.vae.forward(denoised_pred.half(), *vae_cache)
        elif APP_STATE["current_use_taehv"]:
            if latents_cache is None: 
                latents_cache = denoised_pred
            else:
                denoised_pred = torch.cat([latents_cache, denoised_pred], dim=1)
                latents_cache = denoised_pred[:, -3:]
            pixels = pipeline.vae.decode(denoised_pred)
        else:
            pixels, vae_cache = pipeline.vae(denoised_pred.half(), *vae_cache)
            
        # Handle frame skipping for first block
        if idx == 0 and not args.trt: 
            pixels = pixels[:, 3:]
        elif APP_STATE["current_use_taehv"] and idx > 0: 
            pixels = pixels[:, 12:]

        print(f"πŸ“Ή Decoded pixels shape: {pixels.shape}")
        
        # Calculate actual frames that will be yielded for this block
        actual_frames_this_block = pixels.shape[1]
        
        # Yield individual frames with timing delays
        for frame_idx in range(actual_frames_this_block):
            frame_tensor = pixels[0, frame_idx]  # Get single frame [C, H, W]
            
            # Normalize from [-1, 1] to [0, 255]
            frame_np = torch.clamp(frame_tensor.float(), -1., 1.) * 127.5 + 127.5
            frame_np = frame_np.to(torch.uint8).cpu().numpy()
            
            # Convert from CHW to HWC format (RGB)
            frame_np = np.transpose(frame_np, (1, 2, 0))  # CHW -> HWC
            
            all_frames_for_video.append(frame_np)
            total_frames_yielded += 1
            
            # Calculate progress based on blocks completed + current block progress
            blocks_completed = idx
            current_block_progress = (frame_idx + 1) / actual_frames_this_block
            total_block_progress = (blocks_completed + current_block_progress) / num_blocks
            frame_progress_percent = total_block_progress * 100
            
            # Cap at 100% to avoid going over
            frame_progress_percent = min(frame_progress_percent, 100.0)
            
            print(f"πŸ“Ί Yielding frame {total_frames_yielded}: shape {frame_np.shape}")
            
            # Create HTML status update
            if frame_idx == actual_frames_this_block - 1 and idx + 1 == num_blocks:  # Last frame
                status_html = (
                    f"<div style='padding: 16px; border: 1px solid #198754; background-color: #d1e7dd; border-radius: 8px; font-family: sans-serif; text-align: center;'>"
                    f"  <h4 style='margin: 0 0 8px 0; color: #0f5132; font-size: 18px;'>πŸŽ‰ Generation Complete!</h4>"
                    f"  <p style='margin: 0; color: #0f5132;'>"
                    f"    Total frames: {total_frames_yielded}. The final video is now available."
                    f"  </p>"
                    f"</div>"
                )
            else:  # Regular frames
                status_html = (
                    f"<div style='padding: 10px; border: 1px solid #ddd; border-radius: 8px; font-family: sans-serif;'>"
                    f"  <p style='margin: 0 0 8px 0; font-size: 16px; font-weight: bold;'>Generating Video...</p>"
                    f"  <div style='background: #e9ecef; border-radius: 4px; width: 100%; overflow: hidden;'>"
                    f"    <div style='width: {frame_progress_percent:.1f}%; height: 20px; background-color: #0d6efd; transition: width 0.2s;'></div>"
                    f"  </div>"
                    f"  <p style='margin: 8px 0 0 0; color: #555; font-size: 14px; text-align: right;'>"
                    f"    Block {idx+1}/{num_blocks}   |   Frame {total_frames_yielded}   |   {frame_progress_percent:.1f}%"
                    f"  </p>"
                    f"</div>"
                )
            
            # Yield frame with a small delay to ensure UI updates
            yield gr.update(visible=True, value=frame_np), gr.update(visible=False), status_html
            
            # Sleep between frames for smooth playback (except for the last frame)
            # Add minimum delay to ensure UI can update
            if not (frame_idx == actual_frames_this_block - 1 and idx + 1 == num_blocks):
                time.sleep(max(frame_delay, 0.1))  # Minimum 100ms delay
            
        current_start_frame += current_num_frames
    
    print(f"βœ… Video generation completed! Total frames yielded: {total_frames_yielded}")
    
    # Save final video
    try:
        video_path = f"gradio_tmp/{seed}_{hashlib.md5(prompt.encode()).hexdigest()}.mp4"
        imageio.mimwrite(video_path, all_frames_for_video, fps=fps, quality=8)
        print(f"βœ… Video saved to {video_path}")
        final_status_html = (
            f"<div style='padding: 16px; border: 1px solid #198754; background-color: #d1e7dd; border-radius: 8px; font-family: sans-serif; text-align: center;'>"
            f"  <h4 style='margin: 0 0 8px 0; color: #0f5132; font-size: 18px;'>πŸŽ‰ Generation Complete!</h4>"
            f"  <p style='margin: 0; color: #0f5132;'>"
            f"    Video saved successfully with {total_frames_yielded} frames at {fps} FPS."
            f"  </p>"
            f"</div>"
        )
        yield gr.update(visible=False), gr.update(value=video_path, visible=True), final_status_html
    except Exception as e:
        print(f"⚠️ Could not save final video: {e}")
        error_status_html = (
            f"<div style='padding: 16px; border: 1px solid #dc3545; background-color: #f8d7da; border-radius: 8px; font-family: sans-serif; text-align: center;'>"
            f"  <h4 style='margin: 0 0 8px 0; color: #721c24; font-size: 18px;'>⚠️ Video Save Error</h4>"
            f"  <p style='margin: 0; color: #721c24;'>"
            f"    Could not save final video: {str(e)}"
            f"  </p>"
            f"</div>"
        )
        yield None, None, error_status_html

@torch.no_grad()
@spaces.GPU
def video_generation_handler_example(prompt, seed=42, fps=15):
    """
    Simplified video generation function that returns the final video path.
    """
    if seed == -1: 
        seed = random.randint(0, 2**32 - 1)
    
    print(f"🎬 Starting video generation with prompt: '{prompt}' and seed: {seed}")
    
    # Encode text prompt
    print("πŸ”€ Encoding text prompt...")
    conditional_dict = text_encoder(text_prompts=[prompt])
    for key, value in conditional_dict.items():
        conditional_dict[key] = value.to(dtype=torch.float16)
    
    # Initialize generation
    rnd = torch.Generator(gpu).manual_seed(int(seed))
    pipeline._initialize_kv_cache(1, torch.float16, device=gpu)
    pipeline._initialize_crossattn_cache(1, torch.float16, device=gpu)
    noise = torch.randn([1, 21, 16, 60, 104], device=gpu, dtype=torch.float16, generator=rnd)
    
    vae_cache, latents_cache = None, None
    if not APP_STATE["current_use_taehv"] and not args.trt:
        vae_cache = [c.to(device=gpu, dtype=torch.float16) for c in ZERO_VAE_CACHE]

    num_blocks = 7
    current_start_frame = 0
    all_num_frames = [pipeline.num_frame_per_block] * num_blocks
    all_frames_for_video = []
    
    # Generation loop
    for idx, current_num_frames in enumerate(all_num_frames):
        print(f"πŸ“¦ Processing block {idx+1}/{num_blocks} with {current_num_frames} frames")
        
        noisy_input = noise[:, current_start_frame : current_start_frame + current_num_frames]

        # Denoising steps
        for step_idx, current_timestep in enumerate(pipeline.denoising_step_list):
            timestep = torch.ones([1, current_num_frames], device=noise.device, dtype=torch.int64) * current_timestep
            _, denoised_pred = pipeline.generator(
                noisy_image_or_video=noisy_input, conditional_dict=conditional_dict,
                timestep=timestep, kv_cache=pipeline.kv_cache1,
                crossattn_cache=pipeline.crossattn_cache,
                current_start=current_start_frame * pipeline.frame_seq_length
            )
            if step_idx < len(pipeline.denoising_step_list) - 1:
                next_timestep = pipeline.denoising_step_list[step_idx + 1]
                noisy_input = pipeline.scheduler.add_noise(
                    denoised_pred.flatten(0, 1), torch.randn_like(denoised_pred.flatten(0, 1)),
                    next_timestep * torch.ones([1 * current_num_frames], device=noise.device, dtype=torch.long)
                ).unflatten(0, denoised_pred.shape[:2])

        if idx < len(all_num_frames) - 1:
            pipeline.generator(
                noisy_image_or_video=denoised_pred, conditional_dict=conditional_dict,
                timestep=torch.zeros_like(timestep), kv_cache=pipeline.kv_cache1,
                crossattn_cache=pipeline.crossattn_cache,
                current_start=current_start_frame * pipeline.frame_seq_length,
            )

        # Decode to pixels
        if args.trt:
            pixels, vae_cache = pipeline.vae.forward(denoised_pred.half(), *vae_cache)
        elif APP_STATE["current_use_taehv"]:
            if latents_cache is None: 
                latents_cache = denoised_pred
            else:
                denoised_pred = torch.cat([latents_cache, denoised_pred], dim=1)
                latents_cache = denoised_pred[:, -3:]
            pixels = pipeline.vae.decode(denoised_pred)
        else:
            pixels, vae_cache = pipeline.vae(denoised_pred.half(), *vae_cache)
            
        # Handle frame skipping for first block
        if idx == 0 and not args.trt: 
            pixels = pixels[:, 3:]
        elif APP_STATE["current_use_taehv"] and idx > 0: 
            pixels = pixels[:, 12:]

        print(f"πŸ“Ή Decoded pixels shape: {pixels.shape}")
        
        # Collect all frames from this block
        for frame_idx in range(pixels.shape[1]):
            frame_tensor = pixels[0, frame_idx]  # Get single frame [C, H, W]
            
            # Normalize from [-1, 1] to [0, 255]
            frame_np = torch.clamp(frame_tensor.float(), -1., 1.) * 127.5 + 127.5
            frame_np = frame_np.to(torch.uint8).cpu().numpy()
            
            # Convert from CHW to HWC format (RGB)
            frame_np = np.transpose(frame_np, (1, 2, 0))  # CHW -> HWC
            
            all_frames_for_video.append(frame_np)
            
        current_start_frame += current_num_frames
    
    print(f"βœ… Video generation completed! Total frames: {len(all_frames_for_video)}")
    
    # Save final video
    video_path = f"gradio_tmp/{seed}_{hashlib.md5(prompt.encode()).hexdigest()}.mp4"
    imageio.mimwrite(video_path, all_frames_for_video, fps=fps, quality=8)
    print(f"βœ… Video saved to {video_path}")
    
    return video_path
    
# --- Gradio UI Layout ---
frame_display = gr.Image(
    label="Generated Frames",
    height=480,
    width=832,
    show_label=True,
    container=True,
    visible=False
)
final_video = gr.Video(
    label="Final Rendered Video", 
    visible=True, 
    interactive=False,
    height=400,
    autoplay=True
)
status_html = gr.HTML(
    value="<div style='text-align: center; padding: 20px; color: #666;'>Ready to start generation...</div>",
    label="Generation Status"
)
with gr.Blocks(title="Self-Forcing Frame Streaming Demo") as demo:
    gr.Markdown("# πŸš€ Self-Forcing Video Generation with Frame Streaming")
    gr.Markdown("Real-time video generation with frame-by-frame display. [[Model]](https://huggingface.co/gdhe17/Self-Forcing), [[Project page]](https://self-forcing.github.io), [[Paper]](https://huggingface.co/papers/2506.08009)")
    
    with gr.Row():
        with gr.Column(scale=2):
            gr.Markdown("### πŸ“ Configure Generation")
            prompt = gr.Textbox(
                label="Prompt", 
                placeholder="A stylish woman walks down a Tokyo street...", 
                lines=4,
            )
            enhance_button = gr.Button("Enhance prompt")
            gr.Examples(
                examples=[
                    "A close-up shot of a ceramic teacup slowly pouring water into a glass mug. The water flows smoothly from the spout of the teacup into the mug, creating gentle ripples as it fills up. Both cups have detailed textures, with the teacup having a matte finish and the glass mug showcasing clear transparency. The background is a blurred kitchen countertop, adding context without distracting from the central action. The pouring motion is fluid and natural, emphasizing the interaction between the two cups.",
                    "A playful cat is seen playing an electronic guitar, strumming the strings with its front paws. The cat has distinctive black facial markings and a bushy tail. It sits comfortably on a small stool, its body slightly tilted as it focuses intently on the instrument. The setting is a cozy, dimly lit room with vintage posters on the walls, adding a retro vibe. The cat's expressive eyes convey a sense of joy and concentration. Medium close-up shot, focusing on the cat's face and hands interacting with the guitar.",
                    "A dynamic over-the-shoulder perspective of a chef meticulously plating a dish in a bustling kitchen. The chef, a middle-aged woman, deftly arranges ingredients on a pristine white plate. Her hands move with precision, each gesture deliberate and practiced. The background shows a crowded kitchen with steaming pots, whirring blenders, and the clatter of utensils. Bright lights highlight the scene, casting shadows across the busy workspace. The camera angle captures the chef's detailed work from behind, emphasizing his skill and dedication.",
                ],
                inputs=[prompt],
                fn=video_generation_handler_example,
                outputs=[final_video],
                cache_examples="lazy"
            )
            
            with gr.Row():
                seed = gr.Number(label="Seed", value=-1, info="Use -1 for a random seed.")
                fps = gr.Slider(
                    label="Playback FPS", 
                    minimum=1, 
                    maximum=30, 
                    value=args.fps, 
                    step=1,
                    visible=False,
                    info="Frames per second for playback"
                )
                
            start_btn = gr.Button("🎬 Start Generation", variant="primary", size="lg")
            
        with gr.Column(scale=3):
            gr.Markdown("### πŸ“Ί Live Frame Stream")
            gr.Markdown("*Click 'Start Generation' to begin frame streaming*")

            final_video.render()
            
            frame_display.render()

            status_html.render()

    # Connect the generator to the image display
    start_btn.click(
        fn=video_generation_handler,
        inputs=[prompt, seed, fps],
        outputs=[frame_display, final_video, status_html]
    )
    enhance_button.click(
        fn=enhance_prompt,
        inputs=[prompt],
        outputs=[prompt]
    )

# --- Launch App ---
if __name__ == "__main__":
    if os.path.exists("gradio_tmp"):
        import shutil
        shutil.rmtree("gradio_tmp")
    os.makedirs("gradio_tmp", exist_ok=True)
    
    demo.queue().launch(
        server_name=args.host, 
        server_port=args.port, 
        share=args.share,
        show_error=True
    )