ishworrsubedii commited on
Commit
a11a289
·
1 Parent(s): 36f6588

fix: add model name as suffix for video gen output

Browse files
Files changed (1) hide show
  1. app.py +103 -103
app.py CHANGED
@@ -13,7 +13,6 @@ from supabase import create_client
13
 
14
  from src.components.each_necklace_video_gen import EachVideoCreator
15
  from src.components.product_page_nto_cto_gen import ProductPageImageGeneration
16
- from src.components.vidgen import VideoCreator
17
  from src.utils.logs import logger
18
 
19
  supabase_url = os.getenv('SUPABASE_URL')
@@ -36,11 +35,10 @@ RESOURCES_DIR = "/app/resources"
36
 
37
  os.makedirs(RESOURCES_DIR, exist_ok=True)
38
  TEMP_VIDEO_DIR = f"{RESOURCES_DIR}/temp_video"
39
-
40
  os.environ['MOVIEPY_TEMP_DIR'] = '/tmp/moviepy'
41
 
42
 
43
- def upload_to_supabase(video_path, bucket_name="JewelmirrorVideoGeneration"):
44
  logger.info(f"Uploading video to Supabase: {video_path}")
45
 
46
  try:
@@ -48,7 +46,7 @@ def upload_to_supabase(video_path, bucket_name="JewelmirrorVideoGeneration"):
48
  raise FileNotFoundError(f"Video file not found: {video_path}")
49
 
50
  content_type = mimetypes.guess_type(video_path)[0] or 'video/mp4'
51
- file_name = os.path.basename(video_path)
52
 
53
  options = {
54
  "content-type": content_type,
@@ -101,104 +99,105 @@ def download_image(url):
101
  return None
102
 
103
 
104
- class VideoGenerator(BaseModel):
105
- necklace_image: str
106
- necklace_try_on_output_images: list[str]
107
- clothing_output_images: list[str]
108
- makeup_output_images: list[str]
109
- intro_video_path: str = "JewelMirror_intro.mp4"
110
- background_audio_path: str = "TraditionalIndianVlogMusic.mp3"
111
- font_path: str = "PlayfairDisplay-VariableFont.ttf"
112
- image_display_duration: float = 2.5
113
- fps: int = 30
114
- necklace_title: str = "Necklace Try-On"
115
- clothing_title: str = "Clothing Try-On"
116
- makeup_title: str = "Makeup Try-On"
117
- necklace_image_title: str = "Necklace Preview"
118
- nto_image_title: str = "Necklace Try-On"
119
- nto_cto_image_title: str = "Clothing Try-On"
120
- makeup_image_title: str = "Makeup Try-On"
121
- transition_duration: float = 0.5
122
-
123
-
124
- @app.post("/createvideo/")
125
- async def create_video(request: VideoGenerator):
126
- logger.info(f"Creating video with request: {request.dict()}")
127
- start_time = time.time()
128
- try:
129
- logger.info("Downloading images...")
130
- temp_files = {
131
- 'necklace': download_image(request.necklace_image),
132
- 'necklace_tryon': [download_image(url) for url in request.necklace_try_on_output_images],
133
- 'clothing': [download_image(url) for url in request.clothing_output_images],
134
- 'makeup': [download_image(url) for url in request.makeup_output_images]
135
- }
136
- file_download_time = time.time() - start_time
137
-
138
- if any(file is None for files in temp_files.values() for file in
139
- (files if isinstance(files, list) else [files])):
140
- return JSONResponse(content={"status": "error", "message": "Failed to download all required images."},
141
- status_code=400)
142
-
143
- intro_path = f"{RESOURCES_DIR}/intro/{request.intro_video_path}"
144
- output_path = f"{TEMP_VIDEO_DIR}/video_{os.urandom(8).hex()}.mp4"
145
- font_path = f"{RESOURCES_DIR}/fonts/{request.font_path}"
146
- audio_path = f"{RESOURCES_DIR}/audio/{request.background_audio_path}"
147
- logger.info(f"Creating video with paths: {intro_path}, {output_path}, {font_path}, {audio_path}")
148
-
149
- video_creator = VideoCreator(
150
- intro_video_path=intro_path,
151
- necklace_image=temp_files['necklace'],
152
- nto_outputs=temp_files['necklace_tryon'],
153
- nto_cto_outputs=temp_files['clothing'],
154
- makeup_outputs=temp_files['makeup'],
155
- font_path=font_path,
156
- output_path=output_path,
157
- audio_path=audio_path,
158
- image_display_duration=request.image_display_duration,
159
- fps=request.fps,
160
- necklace_image_title=request.necklace_image_title,
161
- nto_image_title=request.nto_image_title,
162
- nto_cto_image_title=request.nto_cto_image_title,
163
- makeup_image_title=request.makeup_image_title,
164
- )
165
- logger.info("Creating video...")
166
-
167
- video_creator.create_final_video()
168
- video_creation_time = time.time() - start_time - file_download_time
169
-
170
- url = upload_to_supabase(video_path=output_path)
171
- logger.info(f"Video created successfully: {url}")
172
- supabase_upload_time = time.time() - start_time - file_download_time - video_creation_time
173
- logger.info(f"Video uploaded to Supabase: {url}")
174
-
175
- response = {
176
- "status": "success",
177
- "message": "Video created successfully.",
178
- "video_url": url,
179
- "timings": {
180
- "file_download_time": file_download_time,
181
- "video_creation_time": video_creation_time,
182
- "supabase_upload_time": supabase_upload_time
183
- }
184
- }
185
- logger.info(f"Response: {response}")
186
-
187
- for files in temp_files.values():
188
- if isinstance(files, list):
189
- for file in files:
190
- if os.path.exists(file):
191
- os.unlink(file)
192
- elif os.path.exists(files):
193
- os.unlink(files)
194
-
195
- os.remove(output_path)
196
- logger.info("Files cleaned up successfully.")
197
- return JSONResponse(content=response, status_code=200)
198
-
199
- except Exception as e:
200
- logger.error(f"Error creating video: {str(e)}")
201
- return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
 
202
 
203
 
204
  class EachNecklaceVideoGeneratorRequest(BaseModel):
@@ -265,6 +264,7 @@ async def create_combined_video(request: EachNecklaceVideoGeneratorRequest):
265
  font_path = f"{RESOURCES_DIR}/fonts/{request.font_path}"
266
  audio_path = f"{RESOURCES_DIR}/audio/{request.background_audio_path}"
267
  outro_path = f"{RESOURCES_DIR}/outro/{request.outro_video_path}"
 
268
 
269
  video_creator = EachVideoCreator(
270
  intro_video_path=intro_path,
@@ -298,7 +298,7 @@ async def create_combined_video(request: EachNecklaceVideoGeneratorRequest):
298
  video_creation_time = time.time() - start_time - file_download_time
299
  logger.info("Video created successfully.")
300
 
301
- url = upload_to_supabase(video_path=output_path)
302
  supabase_upload_time = time.time() - start_time - file_download_time - video_creation_time
303
  logger.info(f"Video uploaded to Supabase: {url}")
304
 
 
13
 
14
  from src.components.each_necklace_video_gen import EachVideoCreator
15
  from src.components.product_page_nto_cto_gen import ProductPageImageGeneration
 
16
  from src.utils.logs import logger
17
 
18
  supabase_url = os.getenv('SUPABASE_URL')
 
35
 
36
  os.makedirs(RESOURCES_DIR, exist_ok=True)
37
  TEMP_VIDEO_DIR = f"{RESOURCES_DIR}/temp_video"
 
38
  os.environ['MOVIEPY_TEMP_DIR'] = '/tmp/moviepy'
39
 
40
 
41
+ def upload_to_supabase(video_path, bucket_name="JewelmirrorVideoGeneration", model_name="MOD"):
42
  logger.info(f"Uploading video to Supabase: {video_path}")
43
 
44
  try:
 
46
  raise FileNotFoundError(f"Video file not found: {video_path}")
47
 
48
  content_type = mimetypes.guess_type(video_path)[0] or 'video/mp4'
49
+ file_name = f"{os.path.basename(video_path)}-{model_name}"
50
 
51
  options = {
52
  "content-type": content_type,
 
99
  return None
100
 
101
 
102
+ #
103
+ # class VideoGenerator(BaseModel):
104
+ # necklace_image: str
105
+ # necklace_try_on_output_images: list[str]
106
+ # clothing_output_images: list[str]
107
+ # makeup_output_images: list[str]
108
+ # intro_video_path: str = "JewelMirror_intro.mp4"
109
+ # background_audio_path: str = "TraditionalIndianVlogMusic.mp3"
110
+ # font_path: str = "PlayfairDisplay-VariableFont.ttf"
111
+ # image_display_duration: float = 2.5
112
+ # fps: int = 30
113
+ # necklace_title: str = "Necklace Try-On"
114
+ # clothing_title: str = "Clothing Try-On"
115
+ # makeup_title: str = "Makeup Try-On"
116
+ # necklace_image_title: str = "Necklace Preview"
117
+ # nto_image_title: str = "Necklace Try-On"
118
+ # nto_cto_image_title: str = "Clothing Try-On"
119
+ # makeup_image_title: str = "Makeup Try-On"
120
+ # transition_duration: float = 0.5
121
+ #
122
+ #
123
+ # @app.post("/createvideo/")
124
+ # async def create_video(request: VideoGenerator):
125
+ # logger.info(f"Creating video with request: {request.dict()}")
126
+ # start_time = time.time()
127
+ # try:
128
+ # logger.info("Downloading images...")
129
+ # temp_files = {
130
+ # 'necklace': download_image(request.necklace_image),
131
+ # 'necklace_tryon': [download_image(url) for url in request.necklace_try_on_output_images],
132
+ # 'clothing': [download_image(url) for url in request.clothing_output_images],
133
+ # 'makeup': [download_image(url) for url in request.makeup_output_images]
134
+ # }
135
+ # file_download_time = time.time() - start_time
136
+ #
137
+ # if any(file is None for files in temp_files.values() for file in
138
+ # (files if isinstance(files, list) else [files])):
139
+ # return JSONResponse(content={"status": "error", "message": "Failed to download all required images."},
140
+ # status_code=400)
141
+ #
142
+ # intro_path = f"{RESOURCES_DIR}/intro/{request.intro_video_path}"
143
+ # output_path = f"{TEMP_VIDEO_DIR}/video_{os.urandom(8).hex()}.mp4"
144
+ # font_path = f"{RESOURCES_DIR}/fonts/{request.font_path}"
145
+ # audio_path = f"{RESOURCES_DIR}/audio/{request.background_audio_path}"
146
+ # logger.info(f"Creating video with paths: {intro_path}, {output_path}, {font_path}, {audio_path}")
147
+ #
148
+ # video_creator = VideoCreator(
149
+ # intro_video_path=intro_path,
150
+ # necklace_image=temp_files['necklace'],
151
+ # nto_outputs=temp_files['necklace_tryon'],
152
+ # nto_cto_outputs=temp_files['clothing'],
153
+ # makeup_outputs=temp_files['makeup'],
154
+ # font_path=font_path,
155
+ # output_path=output_path,
156
+ # audio_path=audio_path,
157
+ # image_display_duration=request.image_display_duration,
158
+ # fps=request.fps,
159
+ # necklace_image_title=request.necklace_image_title,
160
+ # nto_image_title=request.nto_image_title,
161
+ # nto_cto_image_title=request.nto_cto_image_title,
162
+ # makeup_image_title=request.makeup_image_title,
163
+ # )
164
+ # logger.info("Creating video...")
165
+ #
166
+ # video_creator.create_final_video()
167
+ # video_creation_time = time.time() - start_time - file_download_time
168
+ #
169
+ # url = upload_to_supabase(video_path=output_path)
170
+ # logger.info(f"Video created successfully: {url}")
171
+ # supabase_upload_time = time.time() - start_time - file_download_time - video_creation_time
172
+ # logger.info(f"Video uploaded to Supabase: {url}")
173
+ #
174
+ # response = {
175
+ # "status": "success",
176
+ # "message": "Video created successfully.",
177
+ # "video_url": url,
178
+ # "timings": {
179
+ # "file_download_time": file_download_time,
180
+ # "video_creation_time": video_creation_time,
181
+ # "supabase_upload_time": supabase_upload_time
182
+ # }
183
+ # }
184
+ # logger.info(f"Response: {response}")
185
+ #
186
+ # for files in temp_files.values():
187
+ # if isinstance(files, list):
188
+ # for file in files:
189
+ # if os.path.exists(file):
190
+ # os.unlink(file)
191
+ # elif os.path.exists(files):
192
+ # os.unlink(files)
193
+ #
194
+ # os.remove(output_path)
195
+ # logger.info("Files cleaned up successfully.")
196
+ # return JSONResponse(content=response, status_code=200)
197
+ #
198
+ # except Exception as e:
199
+ # logger.error(f"Error creating video: {str(e)}")
200
+ # return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
201
 
202
 
203
  class EachNecklaceVideoGeneratorRequest(BaseModel):
 
264
  font_path = f"{RESOURCES_DIR}/fonts/{request.font_path}"
265
  audio_path = f"{RESOURCES_DIR}/audio/{request.background_audio_path}"
266
  outro_path = f"{RESOURCES_DIR}/outro/{request.outro_video_path}"
267
+ model_name = request.necklace_try_on_output_images[0][0].split("/")[-1].split(".")[0].split("-")[1]
268
 
269
  video_creator = EachVideoCreator(
270
  intro_video_path=intro_path,
 
298
  video_creation_time = time.time() - start_time - file_download_time
299
  logger.info("Video created successfully.")
300
 
301
+ url = upload_to_supabase(video_path=output_path, model_name=model_name)
302
  supabase_upload_time = time.time() - start_time - file_download_time - video_creation_time
303
  logger.info(f"Video uploaded to Supabase: {url}")
304