Spaces:
Sleeping
Sleeping
Update app.py
Browse files259 unmodified lines260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314259 unmodified lines#_______________generate audios and folder structure_______________________async def generate_story_audios(story: StoryCreationDTO, base_output: str):"""Generates audio files and folders for the entire story"""story_dir = Path(base_output) / story.storyIdstory_dir.mkdir(parents=True, exist_ok=True)for chapter in story.chapters:chapter_dir = story_dir / chapter.chapterIdchapter_dir.mkdir(exist_ok=True)# --- Chapter title audio ---prosody_file_title = await download_file_from_url(chapter.title.prosodyReference)title_save_path = chapter_dir / "title.wav"tagged_text_title = generate_tagged_text(chapter.title.sentence,chapter.title.emotion,chapter.title.intensity)title_generated_audio_path = inference_by_model(text=tagged_text_title,audio_file=prosody_file_title,save_path=title_save_path)# os.remove(prosody_file_title)for scene in chapter.scenes:await download_scene_files(scene)scene_dir = chapter_dir / scene.sceneIdscene_dir.mkdir(exist_ok=True)# --- Sentences audio ---for sentence in scene.sentences:# Download the prosody reference audio from Supabaseprosody_file = download_cache[sentence.prosodyReference]sentence_save_path = scene_dir / f"{sentence.sentenceId}.wav"tagged_text = generate_tagged_text(sentence.sentence,sentence.emotion,sentence.intensity)sentence_generated_audio_path = inference_by_model(text=tagged_text,audio_file=prosody_file,save_path=sentence_save_path)# os.remove(prosody_file)#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________259 unmodified lines260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394259 unmodified lines#_______________generate audios and folder structure_______________________# async def generate_story_audios(story: StoryCreationDTO, base_output: str):# """# Generates audio files and folders for the entire story# """# story_dir = Path(base_output) / story.storyId# story_dir.mkdir(parents=True, exist_ok=True)# for chapter in story.chapters:# chapter_dir = story_dir / chapter.chapterId# chapter_dir.mkdir(exist_ok=True)# # --- Chapter title audio ---# prosody_file_title = await download_file_from_url(chapter.title.prosodyReference)# title_save_path = chapter_dir / "title.wav"# tagged_text_title = generate_tagged_text(# chapter.title.sentence,# chapter.title.emotion,# chapter.title.intensity# )# title_generated_audio_path = inference_by_model(# text=tagged_text_title,# audio_file=prosody_file_title,# save_path=title_save_path# )# # os.remove(prosody_file_title)# for scene in chapter.scenes:# await download_scene_files(scene)# scene_dir = chapter_dir / scene.sceneId# scene_dir.mkdir(exist_ok=True)# # --- Sentences audio ---# for sentence in scene.sentences:# # Download the prosody reference audio from Supabase# prosody_file = download_cache[sentence.prosodyReference]# sentence_save_path = scene_dir / f"{sentence.sentenceId}.wav"# tagged_text = generate_tagged_text(# sentence.sentence,# sentence.emotion,# sentence.intensity# )# sentence_generated_audio_path = inference_by_model(# text=tagged_text,# audio_file=prosody_file,# save_path=sentence_save_path# )# # os.remove(prosody_file)import asynciofrom pathlib import Pathasync def generate_story_audios_async(story: StoryCreationDTO, base_output: str, max_concurrent_gpu: int = 1):"""Generates audio files for the story while overlapping GPU inference and disk writes.max_concurrent_gpu: semaphore to limit simultaneous GPU usage (1 if GPU is the bottleneck)"""story_dir = Path(base_output) / story.storyIdstory_dir.mkdir(parents=True, exist_ok=True)# Semaphore ensures we don't overload GPUgpu_semaphore = asyncio.Semaphore(max_concurrent_gpu)async def process_sentence(chapter_dir: Path, scene: SceneDto, sentence: SentenceDto):async with gpu_semaphore:# Get prosody file from cacheprosody_file = download_cache[sentence.prosodyReference]sentence_save_path = chapter_dir / scene.sceneId / f"{sentence.sentenceId}.wav"Path(sentence_save_path).parent.mkdir(parents=True, exist_ok=True)tagged_text = generate_tagged_text(sentence.sentence,sentence.emotion,sentence.intensity)# Run GPU inference in a thread pool to avoid blocking event looploop = asyncio.get_event_loop()generated_path = await loop.run_in_executor(None,inference_by_model,tagged_text,prosody_file,str(sentence_save_path))return generated_path# Prepare tasks for chapterschapter_tasks = []for chapter in story.chapters:chapter_dir = story_dir / chapter.chapterIdchapter_dir.mkdir(exist_ok=True)# --- Chapter title ---title_prosody = download_cache[chapter.title.prosodyReference]title_save_path = chapter_dir / "title.wav"tagged_title = generate_tagged_text(chapter.title.sentence,chapter.title.emotion,chapter.title.intensity)# Run title generation immediatelyloop = asyncio.get_event_loop()await loop.run_in_executor(None,inference_by_model,tagged_title,title_prosody,str(title_save_path))# --- Scenes ---scene_tasks = []for scene in chapter.scenes:# Download scene files (prosody, sfx, bg) concurrentlyawait download_scene_files(scene)for sentence in scene.sentences:scene_tasks.append(process_sentence(chapter_dir, scene, sentence))# Run all sentences in this chapter concurrently but limited by GPU semaphorechapter_tasks.append(asyncio.gather(*scene_tasks))# Wait for all chapters to completeawait asyncio.gather(*chapter_tasks)#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________
app.py+112-32