Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -321,14 +321,20 @@ async def generate_story_audios_async(story: StoryCreationDTO, base_output: str,
|
|
| 321 |
story_dir = Path(base_output) / story.storyId
|
| 322 |
story_dir.mkdir(parents=True, exist_ok=True)
|
| 323 |
|
| 324 |
-
print("
|
|
|
|
| 325 |
# Semaphore ensures we don't overload GPU
|
| 326 |
gpu_semaphore = asyncio.Semaphore(max_concurrent_gpu)
|
| 327 |
|
| 328 |
async def process_sentence(chapter_dir: Path, scene: SceneDto, sentence: SentenceDto):
|
|
|
|
| 329 |
async with gpu_semaphore:
|
|
|
|
| 330 |
# Get prosody file from cache
|
| 331 |
-
prosody_file = download_cache
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
sentence_save_path = chapter_dir / scene.sceneId / f"{sentence.sentenceId}.wav"
|
| 334 |
Path(sentence_save_path).parent.mkdir(parents=True, exist_ok=True)
|
|
@@ -348,7 +354,7 @@ async def generate_story_audios_async(story: StoryCreationDTO, base_output: str,
|
|
| 348 |
prosody_file,
|
| 349 |
str(sentence_save_path)
|
| 350 |
)
|
| 351 |
-
|
| 352 |
return generated_path
|
| 353 |
|
| 354 |
# Prepare tasks for chapters
|
|
@@ -356,9 +362,14 @@ async def generate_story_audios_async(story: StoryCreationDTO, base_output: str,
|
|
| 356 |
for chapter in story.chapters:
|
| 357 |
chapter_dir = story_dir / chapter.chapterId
|
| 358 |
chapter_dir.mkdir(exist_ok=True)
|
|
|
|
| 359 |
|
| 360 |
# --- Chapter title ---
|
| 361 |
-
title_prosody = download_cache
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
title_save_path = chapter_dir / "title.wav"
|
| 363 |
tagged_title = generate_tagged_text(
|
| 364 |
chapter.title.sentence,
|
|
@@ -366,7 +377,7 @@ async def generate_story_audios_async(story: StoryCreationDTO, base_output: str,
|
|
| 366 |
chapter.title.intensity
|
| 367 |
)
|
| 368 |
|
| 369 |
-
|
| 370 |
loop = asyncio.get_event_loop()
|
| 371 |
await loop.run_in_executor(
|
| 372 |
None,
|
|
@@ -375,21 +386,29 @@ async def generate_story_audios_async(story: StoryCreationDTO, base_output: str,
|
|
| 375 |
title_prosody,
|
| 376 |
str(title_save_path)
|
| 377 |
)
|
|
|
|
| 378 |
|
| 379 |
# --- Scenes ---
|
| 380 |
scene_tasks = []
|
| 381 |
for scene in chapter.scenes:
|
| 382 |
-
|
| 383 |
await download_scene_files(scene)
|
| 384 |
|
| 385 |
for sentence in scene.sentences:
|
| 386 |
scene_tasks.append(process_sentence(chapter_dir, scene, sentence))
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
# Wait for all chapters to complete
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________
|
| 395 |
|
|
|
|
| 321 |
story_dir = Path(base_output) / story.storyId
|
| 322 |
story_dir.mkdir(parents=True, exist_ok=True)
|
| 323 |
|
| 324 |
+
print(f"[INFO] Generating story '{story.storyId}' in {story_dir}")
|
| 325 |
+
|
| 326 |
# Semaphore ensures we don't overload GPU
|
| 327 |
gpu_semaphore = asyncio.Semaphore(max_concurrent_gpu)
|
| 328 |
|
| 329 |
async def process_sentence(chapter_dir: Path, scene: SceneDto, sentence: SentenceDto):
|
| 330 |
+
print(f"[INFO] Starting sentence '{sentence.sentenceId}' in scene '{scene.sceneId}'")
|
| 331 |
async with gpu_semaphore:
|
| 332 |
+
print(f"[GPU] Acquired GPU for sentence '{sentence.sentenceId}'")
|
| 333 |
# Get prosody file from cache
|
| 334 |
+
prosody_file = download_cache.get(sentence.prosodyReference)
|
| 335 |
+
if not prosody_file:
|
| 336 |
+
print(f"[WARN] Prosody file for '{sentence.sentenceId}' not found in cache")
|
| 337 |
+
return None
|
| 338 |
|
| 339 |
sentence_save_path = chapter_dir / scene.sceneId / f"{sentence.sentenceId}.wav"
|
| 340 |
Path(sentence_save_path).parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 354 |
prosody_file,
|
| 355 |
str(sentence_save_path)
|
| 356 |
)
|
| 357 |
+
print(f"[DONE] Generated audio for sentence '{sentence.sentenceId}' -> {generated_path}")
|
| 358 |
return generated_path
|
| 359 |
|
| 360 |
# Prepare tasks for chapters
|
|
|
|
| 362 |
for chapter in story.chapters:
|
| 363 |
chapter_dir = story_dir / chapter.chapterId
|
| 364 |
chapter_dir.mkdir(exist_ok=True)
|
| 365 |
+
print(f"[INFO] Processing chapter '{chapter.chapterId}'")
|
| 366 |
|
| 367 |
# --- Chapter title ---
|
| 368 |
+
title_prosody = download_cache.get(chapter.title.prosodyReference)
|
| 369 |
+
if not title_prosody:
|
| 370 |
+
print(f"[WARN] Prosody file for chapter title '{chapter.chapterId}' not found in cache")
|
| 371 |
+
continue
|
| 372 |
+
|
| 373 |
title_save_path = chapter_dir / "title.wav"
|
| 374 |
tagged_title = generate_tagged_text(
|
| 375 |
chapter.title.sentence,
|
|
|
|
| 377 |
chapter.title.intensity
|
| 378 |
)
|
| 379 |
|
| 380 |
+
print(f"[GPU] Generating title audio for chapter '{chapter.chapterId}'")
|
| 381 |
loop = asyncio.get_event_loop()
|
| 382 |
await loop.run_in_executor(
|
| 383 |
None,
|
|
|
|
| 386 |
title_prosody,
|
| 387 |
str(title_save_path)
|
| 388 |
)
|
| 389 |
+
print(f"[DONE] Generated title audio for chapter '{chapter.chapterId}' -> {title_save_path}")
|
| 390 |
|
| 391 |
# --- Scenes ---
|
| 392 |
scene_tasks = []
|
| 393 |
for scene in chapter.scenes:
|
| 394 |
+
print(f"[INFO] Downloading files for scene '{scene.sceneId}'")
|
| 395 |
await download_scene_files(scene)
|
| 396 |
|
| 397 |
for sentence in scene.sentences:
|
| 398 |
scene_tasks.append(process_sentence(chapter_dir, scene, sentence))
|
| 399 |
|
| 400 |
+
if scene_tasks:
|
| 401 |
+
print(f"[INFO] Running {len(scene_tasks)} sentences for chapter '{chapter.chapterId}' concurrently")
|
| 402 |
+
chapter_tasks.append(asyncio.gather(*scene_tasks))
|
| 403 |
+
else:
|
| 404 |
+
print(f"[WARN] No sentences found in chapter '{chapter.chapterId}'")
|
| 405 |
|
| 406 |
# Wait for all chapters to complete
|
| 407 |
+
if chapter_tasks:
|
| 408 |
+
await asyncio.gather(*chapter_tasks)
|
| 409 |
+
print(f"[INFO] Completed generating all chapters for story '{story.storyId}'")
|
| 410 |
+
else:
|
| 411 |
+
print(f"[WARN] No chapters/tasks to process for story '{story.storyId}'")
|
| 412 |
|
| 413 |
#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________
|
| 414 |
|