Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -172,11 +172,11 @@ async def generate_video(
|
|
| 172 |
files: List[UploadFile] = File(...)
|
| 173 |
):
|
| 174 |
"""
|
| 175 |
-
Full pipeline: Upload
|
| 176 |
|
| 177 |
Form parameters:
|
| 178 |
- manifest: JSON string with title and scenes
|
| 179 |
-
- files: Uploaded image files for each scene
|
| 180 |
|
| 181 |
Returns: MP4 video file
|
| 182 |
"""
|
|
@@ -185,41 +185,41 @@ async def generate_video(
|
|
| 185 |
manifest_data = json.loads(manifest)
|
| 186 |
manifest_req = ManifestRequest(**manifest_data)
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
for file in files:
|
| 197 |
if file.filename:
|
| 198 |
-
# Save
|
| 199 |
-
file_path =
|
| 200 |
with open(file_path, "wb") as f:
|
| 201 |
-
|
| 202 |
-
|
| 203 |
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
raise Exception(
|
| 206 |
-
f"Expected {len(manifest_req.scenes)}
|
| 207 |
)
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
select_result = await _select_scenes(manifest_req, upload_dir)
|
| 211 |
-
if select_result["status"] != "success":
|
| 212 |
-
raise Exception(select_result.get("message", "Scene selection failed"))
|
| 213 |
-
|
| 214 |
-
# Step 4: Compose video with manifest data
|
| 215 |
compose_result = await _compose(manifest_req)
|
| 216 |
if compose_result["status"] != "success":
|
| 217 |
raise Exception(compose_result.get("message", "Composition failed"))
|
| 218 |
|
| 219 |
-
#
|
| 220 |
output_file = Path(compose_result["output_path"])
|
| 221 |
if not output_file.exists():
|
| 222 |
-
raise Exception("Output file not
|
| 223 |
|
| 224 |
return FileResponse(
|
| 225 |
path=output_file,
|
|
|
|
| 172 |
files: List[UploadFile] = File(...)
|
| 173 |
):
|
| 174 |
"""
|
| 175 |
+
Full pipeline: Upload images + manifest, compose MP4 and return it.
|
| 176 |
|
| 177 |
Form parameters:
|
| 178 |
- manifest: JSON string with title and scenes
|
| 179 |
+
- files: Uploaded image files for each scene (one per scene)
|
| 180 |
|
| 181 |
Returns: MP4 video file
|
| 182 |
"""
|
|
|
|
| 185 |
manifest_data = json.loads(manifest)
|
| 186 |
manifest_req = ManifestRequest(**manifest_data)
|
| 187 |
|
| 188 |
+
if len(files) != len(manifest_req.scenes):
|
| 189 |
+
raise Exception(
|
| 190 |
+
f"Expected {len(manifest_req.scenes)} files, received {len(files)}"
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Clean and prepare selected directory
|
| 194 |
+
if SELECTED_DIR.exists():
|
| 195 |
+
shutil.rmtree(SELECTED_DIR)
|
| 196 |
+
SELECTED_DIR.mkdir(exist_ok=True)
|
| 197 |
|
| 198 |
+
# Save uploaded files directly to selected/ directory
|
| 199 |
+
for i, file in enumerate(files):
|
|
|
|
| 200 |
if file.filename:
|
| 201 |
+
# Save with standard naming: scene_00.jpg, scene_01.jpg, etc.
|
| 202 |
+
file_path = SELECTED_DIR / f"scene_{i:02d}.jpg"
|
| 203 |
with open(file_path, "wb") as f:
|
| 204 |
+
content = await file.read()
|
| 205 |
+
f.write(content)
|
| 206 |
|
| 207 |
+
# Verify all files were saved
|
| 208 |
+
selected_images = sorted(SELECTED_DIR.glob("scene_*.jpg"))
|
| 209 |
+
if len(selected_images) != len(manifest_req.scenes):
|
| 210 |
raise Exception(
|
| 211 |
+
f"Failed to save all images. Expected {len(manifest_req.scenes)}, got {len(selected_images)}"
|
| 212 |
)
|
| 213 |
|
| 214 |
+
# Compose video with manifest data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
compose_result = await _compose(manifest_req)
|
| 216 |
if compose_result["status"] != "success":
|
| 217 |
raise Exception(compose_result.get("message", "Composition failed"))
|
| 218 |
|
| 219 |
+
# Return the MP4 file
|
| 220 |
output_file = Path(compose_result["output_path"])
|
| 221 |
if not output_file.exists():
|
| 222 |
+
raise Exception("Output file not created")
|
| 223 |
|
| 224 |
return FileResponse(
|
| 225 |
path=output_file,
|