Spanicin commited on
Commit
ba68b15
1 Parent(s): 8961f4c

Update app_celery.py

Browse files
Files changed (1) hide show
  1. app_celery.py +17 -17
app_celery.py CHANGED
@@ -522,7 +522,7 @@ def generate_video():
522
  # args.device = "cuda"
523
  # else:
524
  # args.device = "cpu"
525
-
526
  try:
527
  for index, audio_chunk in enumerate(audio_chunks):
528
  print(f"Submitting chunk {index} with audio_chunk: {audio_chunk}")
@@ -540,28 +540,28 @@ def generate_video():
540
  def stream_video_chunks():
541
  global chunk_tasks
542
  print("chunk_tasks:",chunk_tasks)
543
- for index, task in enumerate(chunk_tasks):
544
- print("task:",task)
545
  @stream_with_context
546
  def generate_chunks():
547
  video_chunk_paths = []
548
- for index, task in enumerate(chunk_tasks):
549
- print("task:",task)
550
- try:
551
- video_chunk_path = task.get() # Wait for each task to complete
552
- video_chunk_paths.append(video_chunk_path)
553
- print("video_chunk_paths",video_chunk_paths)
554
- yield f'data: {video_chunk_path}\n\n'
555
- print(f"Chunk {index} generated and sent: {video_chunk_path}")
556
- os.remove(video_chunk_path)
557
- print(f"Deleted video chunk: {video_chunk_path}")
558
- except Exception as e:
559
- print(f"Error while fetching task result: {str(e)}")
560
- yield f'data: error\n\n'
 
 
561
 
562
  preprocess_dir = os.path.join("/tmp", "preprocess_data")
563
  custom_cleanup(TEMP_DIR.name, preprocess_dir)
564
- print("Temporary files cleaned up, but preprocess_data is retained.")
565
 
566
  # Return the generator that streams the data as it becomes available
567
  return Response(generate_chunks(), content_type='text/event-stream')
 
522
  # args.device = "cuda"
523
  # else:
524
  # args.device = "cpu"
525
+ print("audio_chunks:",audio_chunks)
526
  try:
527
  for index, audio_chunk in enumerate(audio_chunks):
528
  print(f"Submitting chunk {index} with audio_chunk: {audio_chunk}")
 
540
  def stream_video_chunks():
541
  global chunk_tasks
542
  print("chunk_tasks:",chunk_tasks)
 
 
543
  @stream_with_context
544
  def generate_chunks():
545
  video_chunk_paths = []
546
+ unfinished_tasks = chunk_tasks[:]
547
+ while unfinished_tasks: # Keep running until all tasks are finished
548
+ for task in unfinished_tasks[:]: # Iterate over a copy of the list
549
+ if task.ready(): # Check if the task is finished
550
+ try:
551
+ video_chunk_path = task.get() # Get the result (chunk path)
552
+ video_chunk_paths.append(video_chunk_path)
553
+ yield f'data: {video_chunk_path}\n\n' # Stream the chunk path to frontend
554
+ app.logger.info(f"Chunk generated and sent: {video_chunk_path}")
555
+ os.remove(video_chunk_path) # Optionally delete the chunk after sending
556
+ unfinished_tasks.remove(task) # Remove the finished task
557
+ except Exception as e:
558
+ app.logger.error(f"Error while fetching task result: {str(e)}")
559
+ yield f'data: error\n\n'
560
+ time.sleep(1) # Avoid busy waiting, check every second
561
 
562
  preprocess_dir = os.path.join("/tmp", "preprocess_data")
563
  custom_cleanup(TEMP_DIR.name, preprocess_dir)
564
+ app.logger.info("Temporary files cleaned up, but preprocess_data is retained.")
565
 
566
  # Return the generator that streams the data as it becomes available
567
  return Response(generate_chunks(), content_type='text/event-stream')