File size: 21,725 Bytes
6c66ab2
 
 
 
 
450c267
6c66ab2
cf9540e
bd26fd7
6335b4b
 
 
 
 
6c66ab2
2a6e0aa
6c66ab2
 
 
187546a
a55f6c3
187546a
a55f6c3
 
187546a
 
 
 
a55f6c3
 
187546a
 
 
 
a55f6c3
 
187546a
 
967a19c
6c66ab2
 
 
 
6335b4b
187546a
6335b4b
 
 
 
6c66ab2
6335b4b
6c66ab2
6335b4b
 
 
6c66ab2
450c267
cda4205
 
 
 
 
6c66ab2
cda4205
450c267
cda4205
 
 
 
e06d89a
cda4205
731b06c
 
 
6c66ab2
cda4205
 
aad149b
cda4205
 
 
 
 
 
 
450c267
6c66ab2
cda4205
450c267
 
cda4205
 
 
6c66ab2
cda4205
 
 
 
450c267
6c66ab2
 
 
2d49f87
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea76117
 
 
 
 
 
74eeae2
ea76117
 
 
 
 
6c66ab2
ea76117
 
 
 
6c66ab2
 
343b576
6c66ab2
 
 
ea76117
 
 
 
 
 
de3f29f
6c66ab2
 
ea76117
6c66ab2
343b576
6c66ab2
 
 
ea76117
6c66ab2
 
 
 
 
 
 
ea76117
 
6c66ab2
343b576
 
ea76117
6c66ab2
ea76117
6c66ab2
 
343b576
 
6c66ab2
 
 
343b576
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
5423838
 
6c66ab2
 
 
 
 
5423838
6c66ab2
04890a6
6c66ab2
 
 
04890a6
6c66ab2
 
04890a6
6c66ab2
 
04890a6
5423838
6c66ab2
 
 
 
fe0e5ad
 
6c66ab2
fe0e5ad
 
 
 
6c66ab2
fe0e5ad
6c66ab2
fe0e5ad
 
 
6c66ab2
 
 
 
fe0e5ad
 
 
 
 
6c66ab2
fe0e5ad
 
6c66ab2
 
fe0e5ad
6c66ab2
cbcccb0
6c66ab2
918ced7
 
6c66ab2
 
 
 
 
37516b2
6c66ab2
fe0e5ad
6c66ab2
 
5423838
cbcccb0
6c66ab2
 
b27085f
6c66ab2
fe0e5ad
6c66ab2
 
 
fe0e5ad
6c66ab2
fe0e5ad
6c66ab2
fe0e5ad
 
 
6c66ab2
b27085f
fe0e5ad
 
6c66ab2
fe0e5ad
 
 
6c66ab2
 
ea76117
6c66ab2
6e485a0
 
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e485a0
6c66ab2
6e485a0
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398ffb2
 
 
 
 
6c66ab2
37516b2
6c66ab2
 
 
 
 
 
398ffb2
98c540c
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
98c540c
6e485a0
6c66ab2
6e485a0
 
 
e65a2fe
98c540c
 
6e485a0
 
 
6c66ab2
 
 
e5595a9
 
6e485a0
6c66ab2
 
 
 
 
 
 
 
6e485a0
 
 
6c66ab2
6e485a0
6c66ab2
 
 
 
 
 
6e485a0
6c66ab2
6e485a0
 
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e27bc6d
6e485a0
 
 
529168a
6e485a0
529168a
6e485a0
 
529168a
6e485a0
529168a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a55f6c3
6c66ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfb8f1e
ab25d78
6c66ab2
 
59f9520
6c66ab2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.responses import FileResponse
import torch
import torchaudio
import os
from pydantic import BaseModel
from typing import List, Optional
from pathlib import Path

OUTPUT_DIR = "outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

device = "cuda" if torch.cuda.is_available() else "cpu"

from huggingface_hub import hf_hub_download

# ------------------------
# Download model files from Hugging Face if not present
# ------------------------
MODEL_DIR = "my_model"

config_path = hf_hub_download(
    repo_id="MariaKaiser/egtts_finetuned_with_vocab",
    filename="my_model/config.json",
    cache_dir=MODEL_DIR
)

vocab_path = hf_hub_download(
    repo_id="MariaKaiser/egtts_finetuned_with_vocab",
    filename="my_model/vocab.json",
    cache_dir=MODEL_DIR
)

model_path = hf_hub_download(
    repo_id="MariaKaiser/egtts_finetuned_with_vocab",
    filename="my_model/model.pth",
    cache_dir=MODEL_DIR
)

from TTS.tts.models.xtts import Xtts
from TTS.tts.configs.xtts_config import XttsConfig

# Load model
config = XttsConfig()
config.load_json(config_path)

model = Xtts.init_from_config(config)
model.load_checkpoint(
    config,
    checkpoint_dir= os.path.dirname(model_path),
    use_deepspeed=False,
    vocab_path= vocab_path
)
model.to(device)

# --------- Define your models ----------

class BGMusicDto(BaseModel):
    musicPath: str
    emotion: str
    volume: float


class SentenceDto(BaseModel):
    speaker: str
    sentenceId: str
    sentence: str
    prosodyReference: str
    emotion: str
    intensity: str

class LocationDto(BaseModel):
    locationName: str
    path: str
    
class SceneDto(BaseModel):
    sceneId: str
    location: LocationDto
    sentences: List[SentenceDto]
    bgMusic: BGMusicDto

class ChapterDto(BaseModel):
    chapterId: str
    title: SentenceDto
    scenes: List[SceneDto]


class CastDto(BaseModel):
    name: str
    gender: str
    isAdult: bool
    voiceReference: str


class StoryCreationDTO(BaseModel):
    storyId: str
    chapters: List[ChapterDto]
    cast: List[CastDto]

#-----------------------------------------------------------

#__________ func to get file from supabase__________________

import httpx
import tempfile
import asyncio

# async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
#     """
#     Downloads a file from a URL and returns the path to a temporary file.
#     Retries on failure up to `retries` times, waiting `delay` seconds between attempts.
#     Returns None if all attempts fail.
#     """
#     for attempt in range(1, retries + 1):
#         try:
#             async with httpx.AsyncClient(timeout=60.0) as client:  # increased timeout
#                 response = await client.get(url)
#                 response.raise_for_status()  # raises for non-200 status codes

#             # Save to a temporary file
#             temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
#             temp_file.write(response.content)
#             temp_file.close()
#             print(f"Downloaded {url} successfully on attempt {attempt}")
#             return temp_file.name

#         except Exception as e:
#             print(f"Attempt {attempt} failed for {url}: {e}")
#             if attempt < retries:
#                 await asyncio.sleep(delay)  # wait before retrying

#     print(f"All {retries} attempts failed for {url}")
#     return None


download_cache = {}

async def download_scene_files(scene: SceneDto):
    tasks = []

    # Sentence prosody references
    for sentence in scene.sentences:
        tasks.append(download_file_from_url(sentence.prosodyReference))

    # Location SFX
    if scene.location.path:
        tasks.append(download_file_from_url(scene.location.path))

    # Background music
    if scene.bgMusic and scene.bgMusic.musicPath:
        tasks.append(download_file_from_url(scene.bgMusic.musicPath))

    # Run all downloads concurrently
    downloaded_files = await asyncio.gather(*tasks)
    return downloaded_files

async def download_file_from_url(url: str, retries: int = 3, delay: float = 2.0) -> str | None:
    """
    Downloads a file from a URL and returns the path to a temporary file.
    If download fails after `retries` attempts, returns None instead of raising an error.
    Caches successful downloads to avoid repeated requests.
    """
    if url in download_cache:
        #print(f"{url} is got from cache")
        return download_cache[url]
    
    for attempt in range(1, retries + 1):
        try:
            async with httpx.AsyncClient(timeout=60.0) as client:
                response = await client.get(url)
                response.raise_for_status()

            temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
            temp_file.write(response.content)
            temp_file.close()
            
            #print(f"{url} is downloaded and saved in cache")
            
            download_cache[url] = temp_file.name
            return temp_file.name

        except Exception as e:
            #print(f"Attempt {attempt} failed for {url}: {e}")
            if attempt < retries:
                await asyncio.sleep(delay)

    #print(f"All {retries} attempts failed for {url}, skipping...")
    return None

#-----------------------------------------------------------

#takes the text to be said and path to the prosody audio and path to save the generated audio and returns path to the generated audio 
# (save_path -> full path including the filename, not just a folder.)
def inference_by_model(text: str, audio_file: str, save_path: str) -> str: 
    gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(audio_path=[audio_file])
    out = model.inference(
        text=text,
        language="ar",
        gpt_cond_latent=gpt_cond_latent,
        speaker_embedding=speaker_embedding,
        temperature=0.65,
        top_k=model.config.top_k,
        length_penalty=model.config.length_penalty,
        repetition_penalty=model.config.repetition_penalty,
        top_p=model.config.top_p,
    )
    
    os.makedirs(os.path.dirname(save_path), exist_ok=True)
    torchaudio.save(save_path, torch.tensor(out["wav"]).unsqueeze(0), 24000)
    return save_path

#_______________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)

#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________

from pydub import AudioSegment
import os
import subprocess

def ensure_wav(file_path: str) -> str:
    """
    Convert a single audio file to WAV using ffmpeg.
    Returns the path to the WAV file.
    If the file is already WAV, returns the original path.
    """
    ext = os.path.splitext(file_path)[1].lower()
    
    if ext == ".wav":
        return file_path  # Already WAV
    
    # Output path: same folder, same name, .wav extension
    wav_path = os.path.splitext(file_path)[0] + ".wav"
    
    # Run ffmpeg conversion
    subprocess.run(["ffmpeg", "-y", "-i", file_path, wav_path], check=True)
    
    print(f"Converted: {file_path}{wav_path}")
    return wav_path

from pydub import AudioSegment
import asyncio

async def concat_story_audio(story: StoryCreationDTO, base_output: str, final_path: str = None): # full path including filename
    story_dir = Path(base_output) / story.storyId
    story_dir.mkdir(parents=True, exist_ok=True)
    
    if final_path is None:
        final_path = story_dir / f"{story.storyId}_full.wav"
    else:
        final_path = Path(final_path)
        final_path.parent.mkdir(parents=True, exist_ok=True)  # ensure folder exists

    chapters_audio = AudioSegment.silent(duration=0)  # start empty

    for chapter in story.chapters:
        chapter_dir = story_dir / chapter.chapterId

        # --- Chapter title ---
        title_path = chapter_dir / "title.wav"
        chapter_audio = AudioSegment.from_wav(title_path)

        for scene in chapter.scenes:
            scene_dir = chapter_dir / scene.sceneId
            scene_audio = AudioSegment.silent(duration=0)

            # --- Concatenate sentence audios ---
            for sentence in scene.sentences:
                sentence_path = scene_dir / f"{sentence.sentenceId}.wav"
                sentence_audio = AudioSegment.from_wav(sentence_path)
                scene_audio += sentence_audio

           # --- Add SFX for location if available ---
            if scene.location.path:
                sfx_file = await download_file_from_url(scene.location.path)
                if sfx_file:
                    sfx_file_wav = ensure_wav(sfx_file)
                    sfx_audio = AudioSegment.from_wav(sfx_file_wav)
                    scene_audio = scene_audio.overlay(sfx_audio)
                    # os.remove(sfx_file)
                #else:
                    #print(f"SFX skipped for {scene.location.locationName}")

            # --- Add background music if available ---
            if scene.bgMusic and scene.bgMusic.musicPath:
                bg_url = scene.bgMusic.musicPath
                bg_file = await download_file_from_url(bg_url)
                bg_file_wav = ensure_wav(bg_file)
                bg_audio = AudioSegment.from_file(bg_file_wav)

                # Adjust volume
                bg_audio = bg_audio - (1 - scene.bgMusic.volume) * 30  # approximate
                # Loop if shorter than scene
                if len(bg_audio) < len(scene_audio):
                    loops = (len(scene_audio) // len(bg_audio)) + 1
                    bg_audio = bg_audio * loops
                bg_audio = bg_audio[:len(scene_audio)]  # trim to match scene
                scene_audio = scene_audio.overlay(bg_audio)
                # os.remove(bg_file)

            # Add 2 seconds of silence between scenes
            scene_audio += AudioSegment.silent(duration=2000)
            chapter_audio += scene_audio

        # Add 3 seconds of silence between chapters
        chapter_audio += AudioSegment.silent(duration=3000)
        chapters_audio += chapter_audio

    # Export final story
    chapters_audio.export(final_path, format="wav")
    return final_path

#-------------------------------------------------------------

app = FastAPI(title="EGTTS Arabic TTS API")

tasks = {}

#___________________Test end point to test supabase fetch

from fastapi import Query
from fastapi.responses import Response

@app.get("/test-download/")
async def test_download(url: str = Query(...)):
    try:
        file_bytes = await download_file_from_url(url)

        return Response(
            content=file_bytes,
            media_type="audio/wav"  # change if needed
        )

    except Exception as e:
        return {"error": str(e)}
#_________________________________________

@app.get("/")
def root():
    return {"message": "Welcome! Visit /docs for Swagger UI."}

#-----------------------------------------------------------

class TTSResponse(BaseModel):
    fileName: str
    duration: float  # seconds
    audioPath: str   

#---------------------------concatenate text with tags ---------------------------

# Map Intensity numbers to tag strings
intensity_map = {
    "LOW": "low",
    "MEDIUM": "mid",
    "HIGH": "high"
}

# Map Emotion enum names to lowercase tag strings
emotion_map = {
    "HAPPINESS": "happiness",
    "SADNESS": "sadness",
    "FEAR": "fear",
    "ANGER": "anger",
    "SURPRISE": "surprise",
    "WHISPER": "whisper",
    "NARRATION": "narration"
}

def generate_tagged_text(text: str, emotion_enum: str, intensity_enum: str) -> str:
    """
    Convert enums to <emo_x> <int_y> format and concatenate with text
    """
    emo_tag = f"<emo_{emotion_map[emotion_enum]}>"
    int_tag = f"<int_{intensity_map[intensity_enum]}>"
    return f"{emo_tag} {int_tag} {text}"

#-----------------------------------------------------------

#-----------------Post End Point_____________________________

# @app.post("/tts/")
# async def process_story(story: StoryCreationDTO):
    
#   # Optional: print info for debugging
#     print(story.storyId)
#     for cast in story.cast:
#         print(cast.name, cast.voiceReference)
#     for chapter in story.chapters:
#         for scene in chapter.scenes:
#             for sentence in scene.sentences:
#                 print(sentence.speaker, sentence.sentence)

#     # 1️⃣ Generate all sentence audios and folder structure
#     await generate_story_audios(story, base_output=OUTPUT_DIR)

#      # 2️⃣ Concatenate all into final story audio
#     final_story_path = os.path.join(OUTPUT_DIR, story.storyId, f"{story.storyId}_full.wav")
#     final_generated_story_path = await concat_story_audio(story, base_output=OUTPUT_DIR, final_path=final_story_path)
    
#     # Convert to base64 and get duration
#     audio_b64, duration = audio_to_base64(final_generated_story_path)

#     response = TTSResponse(
#         file_name= os.path.basename(final_generated_story_path),
#         duration=duration,
#         audio_base64=audio_b64
#     )

#     return response


# async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
#     try:
#         await generate_story_audios(story, base_output=OUTPUT_DIR)

#         final_story_path = os.path.join(
#             OUTPUT_DIR,
#             story.storyId,
#             f"{story.storyId}_full.wav"
#         )

#         final_generated_story_path = await concat_story_audio(
#             story,
#             base_output=OUTPUT_DIR,
#             final_path=final_story_path
#         )

#         audio_b64, duration = audio_to_base64(final_generated_story_path)

#         tasks[task_id] = {
#             "status": "completed",
#             "result": {
#                 "fileName": os.path.basename(final_generated_story_path),
#                 "duration": duration,
#                 "audioPath": audio_b64
#             }
#         }

#     except Exception as e:
#         print(f"Exception caught at run tts pipeline {str(e)} and status is now failed")
#         tasks[task_id] = {
#             "status": "failed",
#             "error": str(e)
#         }

import os
import uuid
from supabase import create_client, Client
from pydub import AudioSegment  # For duration in seconds

# Initialize Supabase client
SUPABASE_URL = "https://kvlxvhdgacktsgykyckm.supabase.co/"
SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imt2bHh2aGRnYWNrdHNneWt5Y2ttIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3MTk2MTQ5MSwiZXhwIjoyMDg3NTM3NDkxfQ.tzfHcbzwzctHDDDp3vk4JGz30ajN2szncAV-1wK7_pM"
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)

import time
async def run_tts_pipeline(task_id: str, story: StoryCreationDTO):
    start_time = time.time()  # start timer
    try:
        # 1️⃣ Generate story audios
        await generate_story_audios(story, base_output=OUTPUT_DIR)

        # 2️⃣ Concatenate final story audio
        final_story_path = os.path.join(
            OUTPUT_DIR,
            story.storyId,
            f"{story.storyId}_full.wav"
        )

        final_generated_story_path = await concat_story_audio(
            story,
            base_output=OUTPUT_DIR,
            final_path=final_story_path
        )

        print(f" final_generated_story_path: {final_generated_story_path}")

        wav = AudioSegment.from_wav(final_generated_story_path)
        mp3_path = final_generated_story_path.with_suffix(".mp3")
        wav.export(mp3_path, format="mp3", bitrate="192k")

        print(f" final_generated_story_path after conversion to mp3: {mp3_path}")

        
        # 3️⃣ Calculate duration
        audio_segment = AudioSegment.from_file(mp3_path)
        duration_seconds = len(audio_segment) / 1000  # pydub gives length in milliseconds
        
        # 4️⃣ Prepare the file for upload
        file_name = f"{uuid.uuid4()}_{os.path.basename(mp3_path)}"
        storage_path = f"{story.storyId}/final/{file_name}"

        # with open(final_generated_story_path, "rb") as f:
        #     file_bytes = f.read()


        
        supabase.storage.from_("story-audio-files").upload(
            storage_path,
            mp3_path
        )

        # 6️⃣ Get public URL
        audio_url = supabase.storage.from_("story-audio-files").get_public_url(storage_path)

        # 7️⃣ Update task status with audio URL and duration
        tasks[task_id] = {
            "status": "completed",
            "result": {
                "fileName": os.path.basename(mp3_path),
                "duration": duration_seconds,
                "audioPath": audio_url
            }
        }

        # --- Print processing time ---
        end_time = time.time()
        elapsed = end_time - start_time
        print(f"Story {story.storyId} processed in {elapsed:.2f} seconds")

    except Exception as e:
        print(f"exception caught at run tts pipeline {str(e)}")
        tasks[task_id] = {
            "status": "failed",
            "error": str(e)
        }

from fastapi import BackgroundTasks
import uuid

@app.post("/tts/")
async def process_story(story: StoryCreationDTO, background_tasks: BackgroundTasks):

    task_id = str(uuid.uuid4())

    tasks[task_id] = {
        "status": "processing",
        "result": None
    }

    background_tasks.add_task(run_tts_pipeline, task_id, story)

    return {"task_id": task_id}

#-----------------------Results Get End Point ______________________________________

# @app.get("/tts/results/{task_id}")
# async def get_results(task_id: str):

#     if task_id not in tasks:
#         return {"status": "not_found"}

#     task = tasks[task_id]

#     if task["status"] == "processing":
#         return {"status": "processing"}

#     if task["status"] == "failed":
#         return {
#             "status": "failed",
#             "error": task["error"]
#         }

#     return task["result"]

@app.get("/tts/results/{task_id}")
async def get_results(task_id: str):
    if task_id not in tasks:
        return {"status": "not_found"}

    task = tasks[task_id]

    if task["status"] == "processing":
        return {"status": "processing"}

    if task["status"] == "failed":
        return {
            "status": "failed",
            "error": task.get("error", "Unknown error")
        }

    # Ensure result exists and has all required fields
    result = task.get("result")
    if result and all(k in result for k in ("fileName", "duration", "audioPath")):
        #clearing cache
        print(f"all fields are available {result}")
        for file_path in download_cache.values():
            if os.path.exists(file_path):
                os.remove(file_path)
        download_cache.clear()
        
        return {"status": "completed", **result}
    else:
        print(f"missing field {result}")
        # If result is missing fields, mark as still processing
        return {"status": "processing"}

#----------------------------Test End Point to test tts inference------------------------------------

@app.post("/tts_test/")
async def tts_endpoint(
    text: str = Form(...),
    audio_file: UploadFile = File(...),
    emotionName: str = Form(...),
    intensity: int = Form(...)
):
    
    file_path = os.path.join(OUTPUT_DIR, audio_file.filename)
    with open(file_path, "wb") as f:
        f.write(await audio_file.read())

    tagged_text = generate_tagged_text(text, emotionName, intensity)

    output_path = os.path.join(OUTPUT_DIR, "out_test.wav")
    output_wav = inference_by_model(tagged_text, file_path,output_path)
    return FileResponse(output_wav, media_type="audio/wav", filename="output.wav")

import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)


# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=7860)