Commit ·
5f64acd
1
Parent(s): 843e668
Add image retry logic (NVIDIA) and fix video playback mounting
Browse files- app.py +6 -0
- modules/story_reels/services/story_creator.py +33 -2
app.py
CHANGED
|
@@ -104,6 +104,12 @@ static_dir = Path(__file__).parent / "static"
|
|
| 104 |
if static_dir.exists():
|
| 105 |
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
import uvicorn
|
|
|
|
| 104 |
if static_dir.exists():
|
| 105 |
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
|
| 106 |
|
| 107 |
+
# Mount videos directory for direct access and streaming
|
| 108 |
+
videos_dir = config.videos_dir_path
|
| 109 |
+
if not videos_dir.exists():
|
| 110 |
+
videos_dir.mkdir(parents=True, exist_ok=True)
|
| 111 |
+
app.mount("/videos", StaticFiles(directory=str(videos_dir)), name="videos")
|
| 112 |
+
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
import uvicorn
|
modules/story_reels/services/story_creator.py
CHANGED
|
@@ -361,8 +361,39 @@ class StoryCreator:
|
|
| 361 |
else:
|
| 362 |
raise Exception("No image generation client available!")
|
| 363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
# Build generated_scenes from batch results
|
| 365 |
generated_scenes = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
for result in batch_results:
|
| 367 |
if result.get("path"):
|
| 368 |
temp_files.append(Path(result["path"]))
|
|
@@ -381,7 +412,7 @@ class StoryCreator:
|
|
| 381 |
"duration": scene_duration
|
| 382 |
})
|
| 383 |
|
| 384 |
-
logger.info(f"[{job_id}]
|
| 385 |
|
| 386 |
job["scenes"] = generated_scenes
|
| 387 |
job["progress"] = 80
|
|
@@ -400,7 +431,7 @@ class StoryCreator:
|
|
| 400 |
output_path=output_path
|
| 401 |
)
|
| 402 |
|
| 403 |
-
job["video_url"] =
|
| 404 |
job["duration"] = audio_duration
|
| 405 |
job["progress"] = 100
|
| 406 |
|
|
|
|
| 361 |
else:
|
| 362 |
raise Exception("No image generation client available!")
|
| 363 |
|
| 364 |
+
# ====================
|
| 365 |
+
# Retry Logic for Failed Images
|
| 366 |
+
# ====================
|
| 367 |
+
successful_ids = {r["id"] for r in batch_results if r.get("path")}
|
| 368 |
+
missing_ids = [chunk['chunk_id'] for chunk in image_chunks if chunk['chunk_id'] not in successful_ids]
|
| 369 |
+
|
| 370 |
+
if missing_ids and has_nvidia:
|
| 371 |
+
logger.warning(f"[{job_id}] Retrying {len(missing_ids)} failed images with NVIDIA...")
|
| 372 |
+
|
| 373 |
+
# Filter prompts for missing IDs
|
| 374 |
+
retry_prompts = [p for p in prompts_list if p[0] in missing_ids] # p is (idx, prompt_text)
|
| 375 |
+
|
| 376 |
+
# Retry with NVIDIA (more robust)
|
| 377 |
+
retry_results = self.nvidia.generate_batch(
|
| 378 |
+
prompts=retry_prompts,
|
| 379 |
+
output_dir=temp_dir,
|
| 380 |
+
seed=seed + 1, # Change seed slightly
|
| 381 |
+
batch_size=5,
|
| 382 |
+
delay_seconds=2.0 # Slower delay for safety
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
# Add successful retries
|
| 386 |
+
for res in retry_results:
|
| 387 |
+
if res.get("path"):
|
| 388 |
+
batch_results.append(res)
|
| 389 |
+
logger.info(f"[{job_id}] Recovered image {res['id']}")
|
| 390 |
+
|
| 391 |
# Build generated_scenes from batch results
|
| 392 |
generated_scenes = []
|
| 393 |
+
|
| 394 |
+
# Sort all results by ID to ensure correct order
|
| 395 |
+
batch_results.sort(key=lambda x: x["id"])
|
| 396 |
+
|
| 397 |
for result in batch_results:
|
| 398 |
if result.get("path"):
|
| 399 |
temp_files.append(Path(result["path"]))
|
|
|
|
| 412 |
"duration": scene_duration
|
| 413 |
})
|
| 414 |
|
| 415 |
+
logger.info(f"[{job_id}] Final count: {len(generated_scenes)}/{len(ai_prompts)} images")
|
| 416 |
|
| 417 |
job["scenes"] = generated_scenes
|
| 418 |
job["progress"] = 80
|
|
|
|
| 431 |
output_path=output_path
|
| 432 |
)
|
| 433 |
|
| 434 |
+
job["video_url"] = f"/videos/{job_id}.mp4"
|
| 435 |
job["duration"] = audio_duration
|
| 436 |
job["progress"] = 100
|
| 437 |
|