Spaces:
Sleeping
Sleeping
Update app_celery.py
Browse files- 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 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
|
|
|
|
561 |
|
562 |
preprocess_dir = os.path.join("/tmp", "preprocess_data")
|
563 |
custom_cleanup(TEMP_DIR.name, preprocess_dir)
|
564 |
-
|
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')
|