MariaKaiser commited on
Commit
98c540c
·
verified ·
1 Parent(s): 43abbfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -19
app.py CHANGED
@@ -393,24 +393,24 @@ def root():
393
  class TTSResponse(BaseModel):
394
  fileName: str
395
  duration: float # seconds
396
- audioBase64: str
397
 
398
- ######## Convert your audio to Base64
399
- import base64
400
- import torchaudio
401
- import io
402
 
403
- def audio_to_base64(audio_path: str) -> (str, float):
404
- # load audio to get duration
405
- waveform, sr = torchaudio.load(audio_path) # waveform shape: [channels, samples]
406
- duration = waveform.shape[1] / sr # seconds
407
 
408
- # read file bytes
409
- with open(audio_path, "rb") as f:
410
- audio_bytes = f.read()
411
 
412
- audio_b64 = base64.b64encode(audio_bytes).decode("utf-8")
413
- return audio_b64, duration
414
 
415
  #---------------------------concatenate text with tags ---------------------------
416
 
@@ -474,10 +474,57 @@ def generate_tagged_text(text: str, emotion_enum: str, intensity_enum: str) -> s
474
 
475
  # return response
476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477
  async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
478
  try:
 
479
  await generate_story_audios(story, base_output=OUTPUT_DIR)
480
 
 
481
  final_story_path = os.path.join(
482
  OUTPUT_DIR,
483
  story.storyId,
@@ -490,19 +537,36 @@ async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
490
  final_path=final_story_path
491
  )
492
 
493
- audio_b64, duration = audio_to_base64(final_generated_story_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
 
 
495
  tasks[task_id] = {
496
  "status": "completed",
497
  "result": {
498
  "fileName": os.path.basename(final_generated_story_path),
499
- "duration": duration,
500
- "audioBase64": audio_b64
501
  }
502
  }
503
 
504
  except Exception as e:
505
- print(f"Exception caught at run tts pipeline {str(e)} and status is now failed")
506
  tasks[task_id] = {
507
  "status": "failed",
508
  "error": str(e)
@@ -564,7 +628,7 @@ async def get_results(task_id: str):
564
 
565
  # Ensure result exists and has all required fields
566
  result = task.get("result")
567
- if result and all(k in result for k in ("fileName", "duration", "audioBase64")):
568
  #clearing cache
569
  for file_path in download_cache.values():
570
  if os.path.exists(file_path):
 
393
  class TTSResponse(BaseModel):
394
  fileName: str
395
  duration: float # seconds
396
+ audioPath: str
397
 
398
+ # ######## Convert your audio to Base64
399
+ # import base64
400
+ # import torchaudio
401
+ # import io
402
 
403
+ # def audio_to_base64(audio_path: str) -> (str, float):
404
+ # # load audio to get duration
405
+ # waveform, sr = torchaudio.load(audio_path) # waveform shape: [channels, samples]
406
+ # duration = waveform.shape[1] / sr # seconds
407
 
408
+ # # read file bytes
409
+ # with open(audio_path, "rb") as f:
410
+ # audio_bytes = f.read()
411
 
412
+ # audio_b64 = base64.b64encode(audio_bytes).decode("utf-8")
413
+ # return audio_b64, duration
414
 
415
  #---------------------------concatenate text with tags ---------------------------
416
 
 
474
 
475
  # return response
476
 
477
+
478
+ # async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
479
+ # try:
480
+ # await generate_story_audios(story, base_output=OUTPUT_DIR)
481
+
482
+ # final_story_path = os.path.join(
483
+ # OUTPUT_DIR,
484
+ # story.storyId,
485
+ # f"{story.storyId}_full.wav"
486
+ # )
487
+
488
+ # final_generated_story_path = await concat_story_audio(
489
+ # story,
490
+ # base_output=OUTPUT_DIR,
491
+ # final_path=final_story_path
492
+ # )
493
+
494
+ # audio_b64, duration = audio_to_base64(final_generated_story_path)
495
+
496
+ # tasks[task_id] = {
497
+ # "status": "completed",
498
+ # "result": {
499
+ # "fileName": os.path.basename(final_generated_story_path),
500
+ # "duration": duration,
501
+ # "audioPath": audio_b64
502
+ # }
503
+ # }
504
+
505
+ # except Exception as e:
506
+ # print(f"Exception caught at run tts pipeline {str(e)} and status is now failed")
507
+ # tasks[task_id] = {
508
+ # "status": "failed",
509
+ # "error": str(e)
510
+ # }
511
+
512
+ import os
513
+ import uuid
514
+ from supabase import create_client, Client
515
+ from pydub import AudioSegment # For duration in seconds
516
+
517
+ # Initialize Supabase client
518
+ SUPABASE_URL = "https://kvlxvhdgacktsgykyckm.supabase.co/"
519
+ SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imt2bHh2aGRnYWNrdHNneWt5Y2ttIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3MTk2MTQ5MSwiZXhwIjoyMDg3NTM3NDkxfQ.tzfHcbzwzctHDDDp3vk4JGz30ajN2szncAV-1wK7_pM"
520
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
521
+
522
  async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
523
  try:
524
+ # 1️⃣ Generate story audios
525
  await generate_story_audios(story, base_output=OUTPUT_DIR)
526
 
527
+ # 2️⃣ Concatenate final story audio
528
  final_story_path = os.path.join(
529
  OUTPUT_DIR,
530
  story.storyId,
 
537
  final_path=final_story_path
538
  )
539
 
540
+ # 3️⃣ Calculate duration
541
+ audio_segment = AudioSegment.from_file(final_generated_story_path)
542
+ duration_seconds = len(audio_segment) / 1000 # pydub gives length in milliseconds
543
+
544
+ # 4️⃣ Prepare the file for upload
545
+ file_name = f"{uuid.uuid4()}_{os.path.basename(final_generated_story_path)}"
546
+ storage_path = f"{story.storyId}/final/{file_name}"
547
+
548
+ # 5️⃣ Upload to Supabase
549
+ with open(final_generated_story_path, "rb") as f:
550
+ supabase.storage.from_("story-audio-files").upload(
551
+ storage_path,
552
+ f,
553
+ content_type="audio/wav"
554
+ )
555
+
556
+ # 6️⃣ Get public URL
557
+ audio_url = supabase.storage.from_("story-audio-files").get_public_url(storage_path)
558
 
559
+ # 7️⃣ Update task status with audio URL and duration
560
  tasks[task_id] = {
561
  "status": "completed",
562
  "result": {
563
  "fileName": os.path.basename(final_generated_story_path),
564
+ "duration": duration_seconds,
565
+ "audioPath": audio_url
566
  }
567
  }
568
 
569
  except Exception as e:
 
570
  tasks[task_id] = {
571
  "status": "failed",
572
  "error": str(e)
 
628
 
629
  # Ensure result exists and has all required fields
630
  result = task.get("result")
631
+ if result and all(k in result for k in ("fileName", "duration", "audioPath")):
632
  #clearing cache
633
  for file_path in download_cache.values():
634
  if os.path.exists(file_path):