Spaces:
Paused
Paused
Update app_celery.py
Browse files- app_celery.py +40 -26
app_celery.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
import torch
|
3 |
import shutil
|
4 |
import os
|
@@ -532,33 +532,47 @@ def generate_video():
|
|
532 |
chunk_tasks.append(task)
|
533 |
print("chunk_tasks",chunk_tasks)
|
534 |
|
535 |
-
# video_chunk_paths = [task.get() for task in chunk_tasks]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
536 |
# print(f"Video chunks generated: {video_chunk_paths}")
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
video_chunk_paths.append(video_chunk_path)
|
542 |
-
except Exception as e:
|
543 |
-
print(f"Error while fetching task result: {str(e)}")
|
544 |
-
return jsonify({'status': 'error', 'message': str(e)}), 500
|
545 |
-
|
546 |
-
print(f"Video chunks generated: {video_chunk_paths}")
|
547 |
-
preprocess_dir = os.path.join("/tmp", "preprocess_data")
|
548 |
-
custom_cleanup(TEMP_DIR.name, preprocess_dir)
|
549 |
-
|
550 |
-
print("Temporary files cleaned up, but preprocess_data is retained.")
|
551 |
-
return jsonify({
|
552 |
-
'status': 'completed',
|
553 |
-
'video_chunk_paths': video_chunk_paths
|
554 |
-
})
|
555 |
-
|
556 |
# return jsonify({
|
557 |
-
#
|
558 |
-
#
|
559 |
-
#
|
560 |
-
|
561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
562 |
|
563 |
except Exception as e:
|
564 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|
|
|
1 |
+
from flask import Flask, request, jsonify, Response, stream_with_context
|
2 |
import torch
|
3 |
import shutil
|
4 |
import os
|
|
|
532 |
chunk_tasks.append(task)
|
533 |
print("chunk_tasks",chunk_tasks)
|
534 |
|
535 |
+
# # video_chunk_paths = [task.get() for task in chunk_tasks]
|
536 |
+
# # print(f"Video chunks generated: {video_chunk_paths}")
|
537 |
+
# video_chunk_paths = []
|
538 |
+
# for task in chunk_tasks:
|
539 |
+
# try:
|
540 |
+
# video_chunk_path = task.get() # Wait for the task to complete
|
541 |
+
# video_chunk_paths.append(video_chunk_path)
|
542 |
+
# except Exception as e:
|
543 |
+
# print(f"Error while fetching task result: {str(e)}")
|
544 |
+
# return jsonify({'status': 'error', 'message': str(e)}), 500
|
545 |
+
|
546 |
# print(f"Video chunks generated: {video_chunk_paths}")
|
547 |
+
# preprocess_dir = os.path.join("/tmp", "preprocess_data")
|
548 |
+
# custom_cleanup(TEMP_DIR.name, preprocess_dir)
|
549 |
+
|
550 |
+
# print("Temporary files cleaned up, but preprocess_data is retained.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
551 |
# return jsonify({
|
552 |
+
# 'status': 'completed',
|
553 |
+
# 'video_chunk_paths': video_chunk_paths
|
554 |
+
# })
|
555 |
+
|
556 |
+
|
557 |
+
@stream_with_context
|
558 |
+
def generate_chunks():
|
559 |
+
video_chunk_paths = []
|
560 |
+
for index, task in enumerate(chunk_tasks):
|
561 |
+
try:
|
562 |
+
video_chunk_path = task.get() # Wait for each task to complete
|
563 |
+
video_chunk_paths.append(video_chunk_path)
|
564 |
+
yield f'data: {video_chunk_path}\n\n' # Send the chunk path to the frontend
|
565 |
+
print(f"Chunk {index} generated and sent: {video_chunk_path}")
|
566 |
+
except Exception as e:
|
567 |
+
print(f"Error while fetching task result: {str(e)}")
|
568 |
+
yield f'data: error\n\n'
|
569 |
+
|
570 |
+
preprocess_dir = os.path.join("/tmp", "preprocess_data")
|
571 |
+
custom_cleanup(TEMP_DIR.name, preprocess_dir)
|
572 |
+
print("Temporary files cleaned up, but preprocess_data is retained.")
|
573 |
+
|
574 |
+
# Return the generator that streams the data as it becomes available
|
575 |
+
return Response(generate_chunks(), content_type='text/event-stream')
|
576 |
|
577 |
except Exception as e:
|
578 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|