Spaces:
Sleeping
Sleeping
Commit
·
ad443b1
1
Parent(s):
f74a9b7
update: add detail timing and error handling
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import tempfile
|
|
|
3 |
|
4 |
import requests
|
5 |
from fastapi import FastAPI
|
@@ -19,9 +20,9 @@ RESOURCES_DIR = "/app/resources"
|
|
19 |
os.makedirs(RESOURCES_DIR, exist_ok=True)
|
20 |
TEMP_VIDEO_DIR = f"{RESOURCES_DIR}/temp_video"
|
21 |
|
22 |
-
|
23 |
os.environ['MOVIEPY_TEMP_DIR'] = '/tmp/moviepy'
|
24 |
|
|
|
25 |
def upload_to_supabase(video_path, bucket_name="JewelmirrorVideoGeneration"):
|
26 |
try:
|
27 |
with open(video_path, 'rb') as f:
|
@@ -76,6 +77,7 @@ class VideoGenerator(BaseModel):
|
|
76 |
|
77 |
@app.post("/create-video/")
|
78 |
async def create_video(request: VideoGenerator):
|
|
|
79 |
try:
|
80 |
temp_files = {
|
81 |
'necklace': download_image(request.necklace_image),
|
@@ -83,6 +85,7 @@ async def create_video(request: VideoGenerator):
|
|
83 |
'clothing': [download_image(url) for url in request.clothing_output_images],
|
84 |
'makeup': [download_image(url) for url in request.makeup_output_images]
|
85 |
}
|
|
|
86 |
|
87 |
if any(file is None for files in temp_files.values() for file in
|
88 |
(files if isinstance(files, list) else [files])):
|
@@ -114,13 +117,21 @@ async def create_video(request: VideoGenerator):
|
|
114 |
|
115 |
# Generate video
|
116 |
video_creator.create_final_video()
|
|
|
117 |
|
118 |
# Upload to Supabase
|
119 |
url = upload_to_supabase(video_path=output_path)
|
|
|
|
|
120 |
response = {
|
121 |
"status": "success",
|
122 |
-
"message": "Video created successfully",
|
123 |
-
"
|
|
|
|
|
|
|
|
|
|
|
124 |
}
|
125 |
|
126 |
for files in temp_files.values():
|
@@ -135,7 +146,7 @@ async def create_video(request: VideoGenerator):
|
|
135 |
return JSONResponse(content=response, status_code=200)
|
136 |
|
137 |
except Exception as e:
|
138 |
-
|
139 |
|
140 |
|
141 |
@app.get("/resources")
|
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
+
import time
|
4 |
|
5 |
import requests
|
6 |
from fastapi import FastAPI
|
|
|
20 |
os.makedirs(RESOURCES_DIR, exist_ok=True)
|
21 |
TEMP_VIDEO_DIR = f"{RESOURCES_DIR}/temp_video"
|
22 |
|
|
|
23 |
os.environ['MOVIEPY_TEMP_DIR'] = '/tmp/moviepy'
|
24 |
|
25 |
+
|
26 |
def upload_to_supabase(video_path, bucket_name="JewelmirrorVideoGeneration"):
|
27 |
try:
|
28 |
with open(video_path, 'rb') as f:
|
|
|
77 |
|
78 |
@app.post("/create-video/")
|
79 |
async def create_video(request: VideoGenerator):
|
80 |
+
start_time = time.time()
|
81 |
try:
|
82 |
temp_files = {
|
83 |
'necklace': download_image(request.necklace_image),
|
|
|
85 |
'clothing': [download_image(url) for url in request.clothing_output_images],
|
86 |
'makeup': [download_image(url) for url in request.makeup_output_images]
|
87 |
}
|
88 |
+
file_download_time = time.time() - start_time
|
89 |
|
90 |
if any(file is None for files in temp_files.values() for file in
|
91 |
(files if isinstance(files, list) else [files])):
|
|
|
117 |
|
118 |
# Generate video
|
119 |
video_creator.create_final_video()
|
120 |
+
video_creation_time = time.time() - start_time - file_download_time
|
121 |
|
122 |
# Upload to Supabase
|
123 |
url = upload_to_supabase(video_path=output_path)
|
124 |
+
supabase_upload_time = time.time() - start_time - file_download_time - video_creation_time
|
125 |
+
|
126 |
response = {
|
127 |
"status": "success",
|
128 |
+
"message": "Video created successfully.",
|
129 |
+
"video_url": url,
|
130 |
+
"timings": {
|
131 |
+
"file_download_time": file_download_time,
|
132 |
+
"video_creation_time": video_creation_time,
|
133 |
+
"supabase_upload_time": supabase_upload_time
|
134 |
+
}
|
135 |
}
|
136 |
|
137 |
for files in temp_files.values():
|
|
|
146 |
return JSONResponse(content=response, status_code=200)
|
147 |
|
148 |
except Exception as e:
|
149 |
+
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
150 |
|
151 |
|
152 |
@app.get("/resources")
|