Mbonea commited on
Commit
40cb044
1 Parent(s): cda1597

removed celery to save memory

Browse files
Files changed (3) hide show
  1. App/Editor/editorRoutes.py +5 -4
  2. App/Worker.py +33 -25
  3. Dockerfile +4 -1
App/Editor/editorRoutes.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import APIRouter, HTTPException, status
2
  from .Schema import EditorRequest, TaskInfo
3
  from App.Worker import celery_task
4
  from celery.result import AsyncResult
@@ -7,9 +7,10 @@ videditor_router = APIRouter(tags=["vidEditor"])
7
 
8
 
9
  @videditor_router.post("/create-video")
10
- async def create_video(videoRequest: EditorRequest):
11
- result = celery_task.delay(videoRequest)
12
- return {"task_id": result.task_id}
 
13
 
14
 
15
  @videditor_router.get("/progress/{task_id}", response_model=TaskInfo)
 
1
+ from fastapi import APIRouter, HTTPException, status, BackgroundTasks
2
  from .Schema import EditorRequest, TaskInfo
3
  from App.Worker import celery_task
4
  from celery.result import AsyncResult
 
7
 
8
 
9
  @videditor_router.post("/create-video")
10
+ async def create_video(videoRequest: EditorRequest, background_task: BackgroundTasks):
11
+ background_task.add_task(celery_task, videoRequest)
12
+ # result = celery_task.delay(videoRequest)
13
+ return {"task_id": "started"}
14
 
15
 
16
  @videditor_router.get("/progress/{task_id}", response_model=TaskInfo)
App/Worker.py CHANGED
@@ -10,15 +10,15 @@ from asgiref.sync import async_to_sync
10
  import json
11
  import os
12
 
13
- celery = Celery()
14
- celery.config_from_object(celery_config)
15
- celery.conf.update(
16
- # Other Celery configuration settings
17
- CELERYD_LOG_LEVEL="DEBUG", # Set log level to DEBUG for the worker
18
- )
19
 
20
 
21
- @celery.task(name="CreateFile")
22
  def create_json_file(assets: List[Assets], asset_dir: str):
23
  for asset in assets:
24
  filename = f"{asset.type.capitalize()}Sequences.json"
@@ -48,7 +48,7 @@ def download_with_wget(link, download_dir, filename):
48
  subprocess.run(["aria2c", link, "-d", download_dir, "-o", filename])
49
 
50
 
51
- @celery.task(name="CopyRemotion")
52
  def copy_remotion_app(src: str, dest: str):
53
  shutil.copytree(src, dest)
54
 
@@ -57,7 +57,7 @@ def copy_remotion_app(src: str, dest: str):
57
  # create_symlink(source_dir, target_dir=dest, symlink_name="node_module")
58
 
59
 
60
- @celery.task(name="Unsilence")
61
  def unsilence(directory: str):
62
  output_dir = os.path.join(directory, "out/video.mp4")
63
  shortered_dir = os.path.join(directory, "out/temp.mp4")
@@ -66,13 +66,13 @@ def unsilence(directory: str):
66
  os.rename(shortered_dir, output_dir)
67
 
68
 
69
- @celery.task(name="InstallDependency")
70
  def install_dependencies(directory: str):
71
  os.chdir(directory)
72
  os.system("npm install")
73
 
74
 
75
- @celery.task(name="DownloadAssets")
76
  def download_assets(links: List[LinkInfo], temp_dir: str):
77
  public_dir = f"{temp_dir}/public"
78
  for link in links:
@@ -81,7 +81,7 @@ def download_assets(links: List[LinkInfo], temp_dir: str):
81
  download_with_wget(file_link, public_dir, file_name)
82
 
83
 
84
- @celery.task(name="RenderFile")
85
  def render_video(directory: str, output_directory: str):
86
  os.chdir(directory)
87
  os.system(f"npm run build --output {output_directory}")
@@ -90,7 +90,7 @@ def render_video(directory: str, output_directory: str):
90
  print("complete")
91
 
92
 
93
- @celery.task(name="send")
94
  def cleanup_temp_directory(
95
  temp_dir: str, output_dir: str, chat_id: int = -1002069945904
96
  ):
@@ -105,7 +105,7 @@ def cleanup_temp_directory(
105
  shutil.rmtree(temp_dir, ignore_errors=True)
106
 
107
 
108
- @celery.task(name="All")
109
  def celery_task(video_task: EditorRequest):
110
  remotion_app_dir = os.path.join("/srv", "Remotion-app")
111
  project_id = str(uuid.uuid4())
@@ -114,17 +114,25 @@ def celery_task(video_task: EditorRequest):
114
 
115
  assets_dir = os.path.join(temp_dir, "src/HelloWorld/Assets")
116
 
117
- chain(
118
- copy_remotion_app.si(remotion_app_dir, temp_dir),
119
- install_dependencies.si(temp_dir),
120
- create_json_file.si(video_task.assets, assets_dir),
121
- download_assets.si(video_task.links, temp_dir) if video_task.links else None,
122
- render_video.si(temp_dir, output_dir),
123
- # unsilence.si(temp_dir),
124
- cleanup_temp_directory.si(temp_dir, output_dir),
125
- ).apply_async(
126
- # link_error=handle_error
127
- ) # Link the tasks and handle errors
 
 
 
 
 
 
 
 
128
 
129
 
130
  def handle_error(task_id, err, *args, **kwargs):
 
10
  import json
11
  import os
12
 
13
+ # celery = Celery()
14
+ # celery.config_from_object(celery_config)
15
+ # celery.conf.update(
16
+ # # Other Celery configuration settings
17
+ # CELERYD_LOG_LEVEL="DEBUG", # Set log level to DEBUG for the worker
18
+ # )
19
 
20
 
21
+ # @celery.task(name="CreateFile")
22
  def create_json_file(assets: List[Assets], asset_dir: str):
23
  for asset in assets:
24
  filename = f"{asset.type.capitalize()}Sequences.json"
 
48
  subprocess.run(["aria2c", link, "-d", download_dir, "-o", filename])
49
 
50
 
51
+ # @celery.task(name="CopyRemotion")
52
  def copy_remotion_app(src: str, dest: str):
53
  shutil.copytree(src, dest)
54
 
 
57
  # create_symlink(source_dir, target_dir=dest, symlink_name="node_module")
58
 
59
 
60
+ # @celery.task(name="Unsilence")
61
  def unsilence(directory: str):
62
  output_dir = os.path.join(directory, "out/video.mp4")
63
  shortered_dir = os.path.join(directory, "out/temp.mp4")
 
66
  os.rename(shortered_dir, output_dir)
67
 
68
 
69
+ # @celery.task(name="InstallDependency")
70
  def install_dependencies(directory: str):
71
  os.chdir(directory)
72
  os.system("npm install")
73
 
74
 
75
+ # @celery.task(name="DownloadAssets")
76
  def download_assets(links: List[LinkInfo], temp_dir: str):
77
  public_dir = f"{temp_dir}/public"
78
  for link in links:
 
81
  download_with_wget(file_link, public_dir, file_name)
82
 
83
 
84
+ # @celery.task(name="RenderFile")
85
  def render_video(directory: str, output_directory: str):
86
  os.chdir(directory)
87
  os.system(f"npm run build --output {output_directory}")
 
90
  print("complete")
91
 
92
 
93
+ # @celery.task(name="send")
94
  def cleanup_temp_directory(
95
  temp_dir: str, output_dir: str, chat_id: int = -1002069945904
96
  ):
 
105
  shutil.rmtree(temp_dir, ignore_errors=True)
106
 
107
 
108
+ # @celery.task(name="All")
109
  def celery_task(video_task: EditorRequest):
110
  remotion_app_dir = os.path.join("/srv", "Remotion-app")
111
  project_id = str(uuid.uuid4())
 
114
 
115
  assets_dir = os.path.join(temp_dir, "src/HelloWorld/Assets")
116
 
117
+ copy_remotion_app(remotion_app_dir, temp_dir),
118
+ install_dependencies(temp_dir),
119
+ create_json_file(video_task.assets, assets_dir),
120
+ download_assets(video_task.links, temp_dir) if video_task.links else None,
121
+ render_video(temp_dir, output_dir),
122
+ # unsilence.si(temp_dir),
123
+ cleanup_temp_directory.si(temp_dir, output_dir),
124
+
125
+ # chain(
126
+ # copy_remotion_app.si(remotion_app_dir, temp_dir),
127
+ # install_dependencies.si(temp_dir),
128
+ # create_json_file.si(video_task.assets, assets_dir),
129
+ # download_assets.si(video_task.links, temp_dir) if video_task.links else None,
130
+ # render_video.si(temp_dir, output_dir),
131
+ # # unsilence.si(temp_dir),
132
+ # cleanup_temp_directory.si(temp_dir, output_dir),
133
+ # ).apply_async(
134
+ # # link_error=handle_error
135
+ # ) # Link the tasks and handle errors
136
 
137
 
138
  def handle_error(task_id, err, *args, **kwargs):
Dockerfile CHANGED
@@ -69,6 +69,9 @@ RUN pipx install unsilence
69
 
70
 
71
  # Command to run the application
72
- CMD python -m uvicorn App.app:app --host 0.0.0.0 --port 7860 & python -m celery -A App.Worker.celery worker -c 4 --max-tasks-per-child=1
 
 
 
73
 
74
  EXPOSE 7860
 
69
 
70
 
71
  # Command to run the application
72
+ # CMD python -m uvicorn App.app:app --host 0.0.0.0 --port 7860 & python -m celery -A App.Worker.celery worker -c 4 --max-tasks-per-child=1
73
+
74
+ CMD python -m uvicorn App.app:app --host 0.0.0.0 --port 7860 --workers 4
75
+
76
 
77
  EXPOSE 7860