sourxbhh commited on
Commit
f464d55
Β·
1 Parent(s): d8a502b

Fix: Batch generation - use persistent directory for videos

Browse files
Files changed (1) hide show
  1. app.py +84 -16
app.py CHANGED
@@ -337,9 +337,13 @@ def generate_motion_batch(
337
  video_paths = []
338
 
339
  # Create a persistent directory for batch videos (not temp, so Gradio can access them)
 
340
  batch_output_dir = pjoin('generation', 'batch_temp')
341
  os.makedirs(batch_output_dir, exist_ok=True)
342
 
 
 
 
343
  for idx, (text, motion_length) in enumerate(text_prompts):
344
  try:
345
  video_path, gen_msg = generate_motion_single(
@@ -349,36 +353,99 @@ def generate_motion_batch(
349
  # If video was created, copy it to persistent location for batch gallery
350
  if video_path and os.path.exists(video_path):
351
  # Create a unique filename for this batch item
352
- batch_video_path = pjoin(batch_output_dir, f'motion_{idx:04d}{os.path.splitext(video_path)[1]}')
 
 
353
 
354
  # Copy file to persistent location
355
  import shutil
356
- shutil.copy2(video_path, batch_video_path)
357
-
358
- # Verify the copied file exists
359
- if os.path.exists(batch_video_path):
360
- video_paths.append(batch_video_path)
361
- status_msg += f"\n[{idx+1}/{len(text_prompts)}] βœ“ {text[:50]}... - Video saved"
362
- else:
363
- status_msg += f"\n[{idx+1}/{len(text_prompts)}] ⚠ {text[:50]}... - Video copy failed"
 
 
 
 
 
 
 
364
  video_paths.append(None)
365
  else:
366
- status_msg += f"\n[{idx+1}/{len(text_prompts)}] ❌ {text[:50]}... - Generation failed"
367
  video_paths.append(None)
368
 
369
  except Exception as e:
370
  status_msg += f"\n[{idx+1}/{len(text_prompts)}] ❌ Error: {str(e)}"
 
 
371
  video_paths.append(None)
372
 
373
- # Filter out None values - Gradio Gallery doesn't handle None well
374
- valid_video_paths = [path for path in video_paths if path is not None and os.path.exists(path)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375
 
376
  if len(valid_video_paths) == 0:
377
- status_msg += "\n\n❌ No videos were successfully generated."
 
 
 
378
  else:
379
- status_msg += f"\n\nβœ“ Successfully generated {len(valid_video_paths)}/{len(text_prompts)} motions."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
 
381
- return valid_video_paths, status_msg
382
 
383
 
384
  # Gradio Interface
@@ -529,7 +596,8 @@ def create_interface():
529
  elem_id="gallery",
530
  columns=2,
531
  rows=2,
532
- height="auto"
 
533
  )
534
 
535
  batch_generate_btn.click(
 
337
  video_paths = []
338
 
339
  # Create a persistent directory for batch videos (not temp, so Gradio can access them)
340
+ # Use a directory that Gradio can serve from
341
  batch_output_dir = pjoin('generation', 'batch_temp')
342
  os.makedirs(batch_output_dir, exist_ok=True)
343
 
344
+ # Also ensure the parent directory exists
345
+ os.makedirs('generation', exist_ok=True)
346
+
347
  for idx, (text, motion_length) in enumerate(text_prompts):
348
  try:
349
  video_path, gen_msg = generate_motion_single(
 
353
  # If video was created, copy it to persistent location for batch gallery
354
  if video_path and os.path.exists(video_path):
355
  # Create a unique filename for this batch item
356
+ # Get file extension from original path
357
+ file_ext = os.path.splitext(video_path)[1] or '.mp4'
358
+ batch_video_path = pjoin(batch_output_dir, f'motion_{idx:04d}{file_ext}')
359
 
360
  # Copy file to persistent location
361
  import shutil
362
+ try:
363
+ shutil.copy2(video_path, batch_video_path)
364
+
365
+ # Verify the copied file exists
366
+ if os.path.exists(batch_video_path):
367
+ # Use relative path for Gradio (works better in HuggingFace Spaces)
368
+ # Gradio can serve files from relative paths within the app directory
369
+ video_paths.append(batch_video_path)
370
+ file_size = os.path.getsize(batch_video_path)
371
+ status_msg += f"\n[{idx+1}/{len(text_prompts)}] βœ“ {text[:50]}... - Video saved ({file_size/1024:.1f} KB)"
372
+ else:
373
+ status_msg += f"\n[{idx+1}/{len(text_prompts)}] ⚠ {text[:50]}... - Video copy failed (file not found after copy)"
374
+ video_paths.append(None)
375
+ except Exception as copy_error:
376
+ status_msg += f"\n[{idx+1}/{len(text_prompts)}] ⚠ {text[:50]}... - Copy error: {str(copy_error)}"
377
  video_paths.append(None)
378
  else:
379
+ status_msg += f"\n[{idx+1}/{len(text_prompts)}] ❌ {text[:50]}... - Generation failed (no video path returned)"
380
  video_paths.append(None)
381
 
382
  except Exception as e:
383
  status_msg += f"\n[{idx+1}/{len(text_prompts)}] ❌ Error: {str(e)}"
384
+ import traceback
385
+ status_msg += f"\n Traceback: {traceback.format_exc()[:200]}"
386
  video_paths.append(None)
387
 
388
+ # Filter out None values and verify files exist - Gradio Gallery needs valid paths
389
+ valid_video_paths = []
390
+ for path in video_paths:
391
+ if path is not None:
392
+ # Use relative path (Gradio handles these better in cloud environments)
393
+ # Verify file exists
394
+ if os.path.exists(path):
395
+ # Verify it's a video file
396
+ if path.lower().endswith(('.mp4', '.gif', '.mov', '.avi')):
397
+ # Get file size for debugging
398
+ file_size = os.path.getsize(path)
399
+ if file_size > 0: # Ensure file is not empty
400
+ valid_video_paths.append(path) # Keep relative path
401
+ status_msg += f"\n βœ“ Verified: {os.path.basename(path)} ({file_size/1024:.1f} KB)"
402
+ else:
403
+ status_msg += f"\n ⚠ Empty file: {path}"
404
+ else:
405
+ status_msg += f"\n ⚠ Skipping non-video file: {path}"
406
+ else:
407
+ # Try absolute path as fallback
408
+ abs_path = os.path.abspath(path)
409
+ if os.path.exists(abs_path):
410
+ valid_video_paths.append(abs_path)
411
+ status_msg += f"\n βœ“ Found via absolute path: {abs_path}"
412
+ else:
413
+ status_msg += f"\n ⚠ File not found (relative: {path}, absolute: {abs_path})"
414
 
415
  if len(valid_video_paths) == 0:
416
+ status_msg += "\n\n❌ No videos were successfully generated for the gallery."
417
+ status_msg += "\nπŸ’‘ Check the error messages above for each prompt."
418
+ # Return empty list explicitly
419
+ return [], status_msg
420
  else:
421
+ status_msg += f"\n\nβœ… Successfully generated {len(valid_video_paths)}/{len(text_prompts)} motions."
422
+ status_msg += f"\nπŸ“ Videos saved in: {batch_output_dir}"
423
+ status_msg += f"\nπŸ“‹ Gallery will display {len(valid_video_paths)} video(s)."
424
+
425
+ # Debug: Show first few paths
426
+ status_msg += f"\n\n🎬 First few video paths:\n"
427
+ for i, p in enumerate(valid_video_paths[:3]):
428
+ abs_p = os.path.abspath(p) if not os.path.isabs(p) else p
429
+ exists = "βœ“" if os.path.exists(p) else "βœ—"
430
+ status_msg += f" {exists} [{i+1}] {p} (exists: {os.path.exists(p)})\n"
431
+ if len(valid_video_paths) > 3:
432
+ status_msg += f" ... and {len(valid_video_paths) - 3} more\n"
433
+
434
+ # Return list of file paths (Gradio Gallery expects list of strings)
435
+ # Ensure all paths are valid and accessible
436
+ final_paths = []
437
+ for p in valid_video_paths:
438
+ if os.path.exists(p):
439
+ # Normalize path (use forward slashes for cross-platform compatibility)
440
+ normalized_path = p.replace('\\', '/')
441
+ final_paths.append(normalized_path)
442
+ else:
443
+ status_msg += f"\n⚠ Warning: Path not found when returning: {p}"
444
+
445
+ if len(final_paths) != len(valid_video_paths):
446
+ status_msg += f"\n⚠ Some paths were filtered out. Returning {len(final_paths)}/{len(valid_video_paths)} videos."
447
 
448
+ return final_paths, status_msg
449
 
450
 
451
  # Gradio Interface
 
596
  elem_id="gallery",
597
  columns=2,
598
  rows=2,
599
+ height="auto",
600
+ type="filepath" # Explicitly specify filepath type
601
  )
602
 
603
  batch_generate_btn.click(