MariaKaiser commited on
Commit
6e485a0
·
verified ·
1 Parent(s): c1730b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -26
app.py CHANGED
@@ -278,6 +278,8 @@ async def concat_story_audio(story: StoryCreationDTO, base_output: str, final_pa
278
 
279
  app = FastAPI(title="EGTTS Arabic TTS API")
280
 
 
 
281
  #___________________Test end point to test supabase fetch
282
 
283
  from fastapi import Query
@@ -345,7 +347,7 @@ emotion_map = {
345
  "NARRATION": "narration"
346
  }
347
 
348
- def generate_tagged_text(text: str, emotion_enum: str, intensity_enum: int) -> str:
349
  """
350
  Convert enums to <emo_x> <int_y> format and concatenate with text
351
  """
@@ -355,37 +357,110 @@ def generate_tagged_text(text: str, emotion_enum: str, intensity_enum: int) -> s
355
 
356
  #-----------------------------------------------------------
357
 
358
- @app.post("/tts/")
359
- async def process_story(story: StoryCreationDTO):
 
 
360
 
361
- # Optional: print info for debugging
362
- print(story.storyId)
363
- for cast in story.cast:
364
- print(cast.name, cast.voiceReference)
365
- for chapter in story.chapters:
366
- for scene in chapter.scenes:
367
- for sentence in scene.sentences:
368
- print(sentence.speaker, sentence.sentence)
 
 
 
 
 
 
 
 
 
 
369
 
370
- # 1️⃣ Generate all sentence audios and folder structure
371
- await generate_story_audios(story, base_output=OUTPUT_DIR)
 
 
 
372
 
373
- # 2️⃣ Concatenate all into final story audio
374
- final_story_path = os.path.join(OUTPUT_DIR, story.storyId, f"{story.storyId}_full.wav")
375
- final_generated_story_path = await concat_story_audio(story, base_output=OUTPUT_DIR, final_path=final_story_path)
376
-
377
- # Convert to base64 and get duration
378
- audio_b64, duration = audio_to_base64(final_generated_story_path)
379
 
380
- response = TTSResponse(
381
- file_name= os.path.basename(final_generated_story_path),
382
- duration=duration,
383
- audio_base64=audio_b64
384
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
- return response
387
 
388
- #----------------------------Test------------------------------------
389
 
390
  @app.post("/tts_test/")
391
  async def tts_endpoint(
 
278
 
279
  app = FastAPI(title="EGTTS Arabic TTS API")
280
 
281
+ tasks = {}
282
+
283
  #___________________Test end point to test supabase fetch
284
 
285
  from fastapi import Query
 
347
  "NARRATION": "narration"
348
  }
349
 
350
+ def generate_tagged_text(text: str, emotion_enum: str, intensity_enum: str) -> str:
351
  """
352
  Convert enums to <emo_x> <int_y> format and concatenate with text
353
  """
 
357
 
358
  #-----------------------------------------------------------
359
 
360
+ #-----------------Post End Point_____________________________
361
+
362
+ # @app.post("/tts/")
363
+ # async def process_story(story: StoryCreationDTO):
364
 
365
+ # # Optional: print info for debugging
366
+ # print(story.storyId)
367
+ # for cast in story.cast:
368
+ # print(cast.name, cast.voiceReference)
369
+ # for chapter in story.chapters:
370
+ # for scene in chapter.scenes:
371
+ # for sentence in scene.sentences:
372
+ # print(sentence.speaker, sentence.sentence)
373
+
374
+ # # 1️⃣ Generate all sentence audios and folder structure
375
+ # await generate_story_audios(story, base_output=OUTPUT_DIR)
376
+
377
+ # # 2️⃣ Concatenate all into final story audio
378
+ # final_story_path = os.path.join(OUTPUT_DIR, story.storyId, f"{story.storyId}_full.wav")
379
+ # final_generated_story_path = await concat_story_audio(story, base_output=OUTPUT_DIR, final_path=final_story_path)
380
+
381
+ # # Convert to base64 and get duration
382
+ # audio_b64, duration = audio_to_base64(final_generated_story_path)
383
 
384
+ # response = TTSResponse(
385
+ # file_name= os.path.basename(final_generated_story_path),
386
+ # duration=duration,
387
+ # audio_base64=audio_b64
388
+ # )
389
 
390
+ # return response
 
 
 
 
 
391
 
392
+ async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
393
+ try:
394
+ await generate_story_audios(story, base_output=OUTPUT_DIR)
395
+
396
+ final_story_path = os.path.join(
397
+ OUTPUT_DIR,
398
+ story.storyId,
399
+ f"{story.storyId}_full.wav"
400
+ )
401
+
402
+ final_generated_story_path = await concat_story_audio(
403
+ story,
404
+ base_output=OUTPUT_DIR,
405
+ final_path=final_story_path
406
+ )
407
+
408
+ audio_b64, duration = audio_to_base64(final_generated_story_path)
409
+
410
+ tasks[task_id] = {
411
+ "status": "completed",
412
+ "result": {
413
+ "file_name": os.path.basename(final_generated_story_path),
414
+ "duration": duration,
415
+ "audio_base64": audio_b64
416
+ }
417
+ }
418
+
419
+ except Exception as e:
420
+ tasks[task_id] = {
421
+ "status": "failed",
422
+ "error": str(e)
423
+ }
424
+
425
+ from fastapi import BackgroundTasks
426
+ import uuid
427
+
428
+ @app.post("/tts/")
429
+ async def process_story(story: StoryCreationDTO, background_tasks: BackgroundTasks):
430
+
431
+ task_id = str(uuid.uuid4())
432
+
433
+ tasks[task_id] = {
434
+ "status": "processing",
435
+ "result": None
436
+ }
437
+
438
+ background_tasks.add_task(run_tts_pipeline, task_id, story)
439
+
440
+ return {"task_id": task_id}
441
+
442
+ #-----------------------Results Get End Point ______________________________________
443
+
444
+ @app.get("/results/{task_id}")
445
+ async def get_results(task_id: str):
446
+
447
+ if task_id not in tasks:
448
+ return {"status": "not_found"}
449
+
450
+ task = tasks[task_id]
451
+
452
+ if task["status"] == "processing":
453
+ return {"status": "processing"}
454
+
455
+ if task["status"] == "failed":
456
+ return {
457
+ "status": "failed",
458
+ "error": task["error"]
459
+ }
460
 
461
+ return task["result"]
462
 
463
+ #----------------------------Test End Point to test tts inference------------------------------------
464
 
465
  @app.post("/tts_test/")
466
  async def tts_endpoint(