jbilcke-hf HF Staff commited on
Commit
f5f96d3
·
1 Parent(s): 3a23852

testing something

Browse files
Files changed (1) hide show
  1. app.py +54 -9
app.py CHANGED
@@ -225,23 +225,22 @@ pipeline.to(dtype=torch.float16).to(gpu)
225
  def video_generation_handler_streaming(prompt, seed=42, fps=15, width=400, height=224, duration=5, buffering=2):
226
  """
227
  Generator function that yields .ts video chunks using PyAV for streaming.
228
- Now optimized for block-based processing.
229
  """
230
  if seed == -1:
231
  seed = random.randint(0, 2**32 - 1)
232
 
233
  print(f"🎬 Starting PyAV streaming: '{prompt}', seed: {seed}, duration: {duration}s, buffering: {buffering}s")
234
 
235
- # Buffering delay
236
  if buffering > 0:
237
  buffering_status_html = (
238
  f"<div style='padding: 10px; border: 1px solid #ffc107; background: #fff3cd; border-radius: 8px; font-family: sans-serif;'>"
239
  f" <p style='margin: 0 0 8px 0; font-size: 16px; font-weight: bold;'>⏳ Buffering...</p>"
240
- f" <p style='margin: 0; color: #856404; font-size: 14px;'>Waiting {buffering} seconds before starting stream</p>"
241
  f"</div>"
242
  )
243
  yield None, buffering_status_html
244
- time.sleep(buffering)
245
 
246
  # Setup
247
  conditional_dict = text_encoder(text_prompts=[prompt])
@@ -272,6 +271,12 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15, width=400, heigh
272
  # Ensure temp directory exists
273
  os.makedirs("gradio_tmp", exist_ok=True)
274
 
 
 
 
 
 
 
275
  # Generation loop
276
  for idx, current_num_frames in enumerate(all_num_frames):
277
  print(f"📦 Processing block {idx+1}/{num_blocks}")
@@ -359,7 +364,7 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15, width=400, heigh
359
  # Yield None for video but update status (frame-by-frame tracking)
360
  yield None, frame_status_html
361
 
362
- # Encode entire block as one chunk immediately
363
  if all_frames_from_block:
364
  print(f"📹 Encoding block {idx} with {len(all_frames_from_block)} frames")
365
 
@@ -370,11 +375,45 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15, width=400, heigh
370
 
371
  frames_to_ts_file(all_frames_from_block, ts_path, fps)
372
 
373
- # Calculate final progress for this block
374
- total_progress = (idx + 1) / num_blocks * 100
 
 
 
 
375
 
376
- # Yield the actual video chunk
377
- yield ts_path, gr.update()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
 
379
  except Exception as e:
380
  print(f"⚠️ Error encoding block {idx}: {e}")
@@ -383,6 +422,12 @@ def video_generation_handler_streaming(prompt, seed=42, fps=15, width=400, heigh
383
 
384
  current_start_frame += current_num_frames
385
 
 
 
 
 
 
 
386
  # Final completion status
387
  final_status_html = (
388
  f"<div style='padding: 16px; border: 1px solid #198754; background: linear-gradient(135deg, #d1e7dd, #f8f9fa); border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);'>"
 
225
  def video_generation_handler_streaming(prompt, seed=42, fps=15, width=400, height=224, duration=5, buffering=2):
226
  """
227
  Generator function that yields .ts video chunks using PyAV for streaming.
228
+ Now optimized for block-based processing with smart buffering.
229
  """
230
  if seed == -1:
231
  seed = random.randint(0, 2**32 - 1)
232
 
233
  print(f"🎬 Starting PyAV streaming: '{prompt}', seed: {seed}, duration: {duration}s, buffering: {buffering}s")
234
 
235
+ # Show initial buffering status but don't wait - start generating immediately
236
  if buffering > 0:
237
  buffering_status_html = (
238
  f"<div style='padding: 10px; border: 1px solid #ffc107; background: #fff3cd; border-radius: 8px; font-family: sans-serif;'>"
239
  f" <p style='margin: 0 0 8px 0; font-size: 16px; font-weight: bold;'>⏳ Buffering...</p>"
240
+ f" <p style='margin: 0; color: #856404; font-size: 14px;'>Generating content, will stream when {buffering} seconds of video is ready</p>"
241
  f"</div>"
242
  )
243
  yield None, buffering_status_html
 
244
 
245
  # Setup
246
  conditional_dict = text_encoder(text_prompts=[prompt])
 
271
  # Ensure temp directory exists
272
  os.makedirs("gradio_tmp", exist_ok=True)
273
 
274
+ # Buffer management - collect chunks before streaming
275
+ buffer_chunks = []
276
+ buffer_duration = 0.0
277
+ frames_per_second = fps
278
+ streaming_started = False
279
+
280
  # Generation loop
281
  for idx, current_num_frames in enumerate(all_num_frames):
282
  print(f"📦 Processing block {idx+1}/{num_blocks}")
 
364
  # Yield None for video but update status (frame-by-frame tracking)
365
  yield None, frame_status_html
366
 
367
+ # Encode entire block as one chunk
368
  if all_frames_from_block:
369
  print(f"📹 Encoding block {idx} with {len(all_frames_from_block)} frames")
370
 
 
375
 
376
  frames_to_ts_file(all_frames_from_block, ts_path, fps)
377
 
378
+ # Calculate duration of this chunk
379
+ chunk_duration = len(all_frames_from_block) / frames_per_second
380
+
381
+ # Add to buffer
382
+ buffer_chunks.append(ts_path)
383
+ buffer_duration += chunk_duration
384
 
385
+ # Check if we have enough buffered content to start streaming
386
+ if not streaming_started and buffer_duration >= buffering:
387
+ print(f"🚀 Buffer filled ({buffer_duration:.2f}s >= {buffering}s), starting stream!")
388
+ streaming_started = True
389
+
390
+ # Stream all buffered chunks
391
+ for buffered_chunk in buffer_chunks:
392
+ yield buffered_chunk, gr.update()
393
+
394
+ # Clear buffer since we've streamed it
395
+ buffer_chunks.clear()
396
+ buffer_duration = 0.0
397
+
398
+ elif streaming_started:
399
+ # Stream immediately if we're already streaming
400
+ yield ts_path, gr.update()
401
+ elif buffering == 0:
402
+ # No buffering requested, stream immediately
403
+ yield ts_path, gr.update()
404
+ else:
405
+ # Still buffering, show progress
406
+ buffer_progress = (buffer_duration / buffering) * 100
407
+ buffering_progress_html = (
408
+ f"<div style='padding: 10px; border: 1px solid #ffc107; background: #fff3cd; border-radius: 8px; font-family: sans-serif;'>"
409
+ f" <p style='margin: 0 0 8px 0; font-size: 16px; font-weight: bold;'>⏳ Buffering... ({buffer_duration:.1f}s/{buffering}s)</p>"
410
+ f" <div style='background: #e9ecef; border-radius: 4px; width: 100%; overflow: hidden;'>"
411
+ f" <div style='width: {buffer_progress:.1f}%; height: 20px; background-color: #ffc107; transition: width 0.2s;'></div>"
412
+ f" </div>"
413
+ f" <p style='margin: 4px 0 0 0; color: #856404; font-size: 14px;'>Generating content for smooth playback...</p>"
414
+ f"</div>"
415
+ )
416
+ yield None, buffering_progress_html
417
 
418
  except Exception as e:
419
  print(f"⚠️ Error encoding block {idx}: {e}")
 
422
 
423
  current_start_frame += current_num_frames
424
 
425
+ # Stream any remaining buffered content
426
+ if buffer_chunks:
427
+ print(f"🎬 Streaming remaining {len(buffer_chunks)} buffered chunks")
428
+ for buffered_chunk in buffer_chunks:
429
+ yield buffered_chunk, gr.update()
430
+
431
  # Final completion status
432
  final_status_html = (
433
  f"<div style='padding: 16px; border: 1px solid #198754; background: linear-gradient(135deg, #d1e7dd, #f8f9fa); border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);'>"