Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -151,6 +151,24 @@ APP_STATE = {
|
|
| 151 |
# Global variable to store generated video chunks
|
| 152 |
generated_video_chunks = []
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
def frames_to_ts_file(frames, filepath, fps = 15):
|
| 155 |
"""
|
| 156 |
Convert frames directly to .ts file using PyAV.
|
|
@@ -309,10 +327,10 @@ pipeline.to(dtype=torch.float16).to(gpu)
|
|
| 309 |
|
| 310 |
@torch.no_grad()
|
| 311 |
@spaces.GPU
|
| 312 |
-
def video_generation_handler_streaming(prompt, seed=42, fps=15):
|
| 313 |
"""
|
| 314 |
Generator function that yields .ts video chunks using PyAV for streaming.
|
| 315 |
-
Now optimized for block-based processing.
|
| 316 |
"""
|
| 317 |
global generated_video_chunks
|
| 318 |
generated_video_chunks = [] # Reset chunks for new generation
|
|
@@ -320,7 +338,13 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15):
|
|
| 320 |
if seed == -1:
|
| 321 |
seed = random.randint(0, 2**32 - 1)
|
| 322 |
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
# Setup
|
| 326 |
conditional_dict = text_encoder(text_prompts=[prompt])
|
|
@@ -330,7 +354,9 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15):
|
|
| 330 |
rnd = torch.Generator(gpu).manual_seed(int(seed))
|
| 331 |
pipeline._initialize_kv_cache(1, torch.float16, device=gpu)
|
| 332 |
pipeline._initialize_crossattn_cache(1, torch.float16, device=gpu)
|
| 333 |
-
|
|
|
|
|
|
|
| 334 |
|
| 335 |
vae_cache, latents_cache = None, None
|
| 336 |
if not APP_STATE["current_use_taehv"] and not args.trt:
|
|
@@ -464,7 +490,7 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15):
|
|
| 464 |
if all_frames_for_download:
|
| 465 |
try:
|
| 466 |
mp4_uuid = str(uuid.uuid4())[:8]
|
| 467 |
-
mp4_filename = f"generated_video_{mp4_uuid}.mp4"
|
| 468 |
mp4_path = os.path.join("gradio_tmp", mp4_filename)
|
| 469 |
frames_to_mp4_file(all_frames_for_download, mp4_path, fps)
|
| 470 |
final_mp4_path = mp4_path
|
|
@@ -486,7 +512,7 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15):
|
|
| 486 |
f" 📊 Generated {total_frames_yielded} frames across {num_blocks} blocks"
|
| 487 |
f" </p>"
|
| 488 |
f" <p style='margin: 4px 0 0 0; color: #0f5132; font-size: 14px;'>"
|
| 489 |
-
f" 🎬 Playback: {fps} FPS • 📁 Format: MPEG-TS/H.264"
|
| 490 |
f" </p>"
|
| 491 |
f" </div>"
|
| 492 |
f"</div>"
|
|
@@ -533,6 +559,14 @@ with gr.Blocks(title="Self-Forcing Streaming Demo") as demo:
|
|
| 533 |
info="Use -1 for random seed",
|
| 534 |
precision=0
|
| 535 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
fps = gr.Slider(
|
| 537 |
label="Playback FPS",
|
| 538 |
minimum=1,
|
|
@@ -585,7 +619,7 @@ with gr.Blocks(title="Self-Forcing Streaming Demo") as demo:
|
|
| 585 |
# Connect the generator to the streaming video
|
| 586 |
generation_event = start_btn.click(
|
| 587 |
fn=video_generation_handler_streaming,
|
| 588 |
-
inputs=[prompt, seed, fps],
|
| 589 |
outputs=[streaming_video, status_display, complete_video, download_file]
|
| 590 |
)
|
| 591 |
|
|
|
|
| 151 |
# Global variable to store generated video chunks
|
| 152 |
generated_video_chunks = []
|
| 153 |
|
| 154 |
+
# Video aspect ratio configurations
|
| 155 |
+
ASPECT_RATIOS = {
|
| 156 |
+
"16:9": {
|
| 157 |
+
"width": 832,
|
| 158 |
+
"height": 468,
|
| 159 |
+
"latent_w": 104,
|
| 160 |
+
"latent_h": 60,
|
| 161 |
+
"display_name": "16:9 (Landscape)"
|
| 162 |
+
},
|
| 163 |
+
"9:16": {
|
| 164 |
+
"width": 468,
|
| 165 |
+
"height": 832,
|
| 166 |
+
"latent_w": 60,
|
| 167 |
+
"latent_h": 104,
|
| 168 |
+
"display_name": "9:16 (Portrait)"
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
def frames_to_ts_file(frames, filepath, fps = 15):
|
| 173 |
"""
|
| 174 |
Convert frames directly to .ts file using PyAV.
|
|
|
|
| 327 |
|
| 328 |
@torch.no_grad()
|
| 329 |
@spaces.GPU
|
| 330 |
+
def video_generation_handler_streaming(prompt, seed=42, fps=15, aspect_ratio="16:9"):
|
| 331 |
"""
|
| 332 |
Generator function that yields .ts video chunks using PyAV for streaming.
|
| 333 |
+
Now optimized for block-based processing with aspect ratio support.
|
| 334 |
"""
|
| 335 |
global generated_video_chunks
|
| 336 |
generated_video_chunks = [] # Reset chunks for new generation
|
|
|
|
| 338 |
if seed == -1:
|
| 339 |
seed = random.randint(0, 2**32 - 1)
|
| 340 |
|
| 341 |
+
# Get aspect ratio configuration
|
| 342 |
+
ar_config = ASPECT_RATIOS[aspect_ratio]
|
| 343 |
+
latent_w = ar_config["latent_w"]
|
| 344 |
+
latent_h = ar_config["latent_h"]
|
| 345 |
+
|
| 346 |
+
print(f"🎬 Starting PyAV streaming: '{prompt}', seed: {seed}, aspect ratio: {aspect_ratio}")
|
| 347 |
+
print(f"📐 Video dimensions: {ar_config['width']}x{ar_config['height']}, Latent: {latent_w}x{latent_h}")
|
| 348 |
|
| 349 |
# Setup
|
| 350 |
conditional_dict = text_encoder(text_prompts=[prompt])
|
|
|
|
| 354 |
rnd = torch.Generator(gpu).manual_seed(int(seed))
|
| 355 |
pipeline._initialize_kv_cache(1, torch.float16, device=gpu)
|
| 356 |
pipeline._initialize_crossattn_cache(1, torch.float16, device=gpu)
|
| 357 |
+
|
| 358 |
+
# Create noise with appropriate dimensions for the aspect ratio
|
| 359 |
+
noise = torch.randn([1, 21, 16, latent_h, latent_w], device=gpu, dtype=torch.float16, generator=rnd)
|
| 360 |
|
| 361 |
vae_cache, latents_cache = None, None
|
| 362 |
if not APP_STATE["current_use_taehv"] and not args.trt:
|
|
|
|
| 490 |
if all_frames_for_download:
|
| 491 |
try:
|
| 492 |
mp4_uuid = str(uuid.uuid4())[:8]
|
| 493 |
+
mp4_filename = f"generated_video_{mp4_uuid}_{aspect_ratio.replace(':', 'x')}.mp4"
|
| 494 |
mp4_path = os.path.join("gradio_tmp", mp4_filename)
|
| 495 |
frames_to_mp4_file(all_frames_for_download, mp4_path, fps)
|
| 496 |
final_mp4_path = mp4_path
|
|
|
|
| 512 |
f" 📊 Generated {total_frames_yielded} frames across {num_blocks} blocks"
|
| 513 |
f" </p>"
|
| 514 |
f" <p style='margin: 4px 0 0 0; color: #0f5132; font-size: 14px;'>"
|
| 515 |
+
f" 🎬 Playback: {fps} FPS • 📁 Format: MPEG-TS/H.264 • 📐 Aspect Ratio: {aspect_ratio}"
|
| 516 |
f" </p>"
|
| 517 |
f" </div>"
|
| 518 |
f"</div>"
|
|
|
|
| 559 |
info="Use -1 for random seed",
|
| 560 |
precision=0
|
| 561 |
)
|
| 562 |
+
aspect_ratio = gr.Radio(
|
| 563 |
+
label="Aspect Ratio",
|
| 564 |
+
choices=["16:9", "9:16"],
|
| 565 |
+
value="16:9",
|
| 566 |
+
info="Choose video aspect ratio"
|
| 567 |
+
)
|
| 568 |
+
|
| 569 |
+
with gr.Row():
|
| 570 |
fps = gr.Slider(
|
| 571 |
label="Playback FPS",
|
| 572 |
minimum=1,
|
|
|
|
| 619 |
# Connect the generator to the streaming video
|
| 620 |
generation_event = start_btn.click(
|
| 621 |
fn=video_generation_handler_streaming,
|
| 622 |
+
inputs=[prompt, seed, fps, aspect_ratio],
|
| 623 |
outputs=[streaming_video, status_display, complete_video, download_file]
|
| 624 |
)
|
| 625 |
|