File size: 1,003 Bytes
140b44e
 
f158396
 
140b44e
f158396
 
 
140b44e
 
 
 
 
 
 
 
f158396
 
 
 
 
 
 
 
140b44e
 
 
 
 
 
f158396
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
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from kokoro import KPipeline
import soundfile as sf
import os
import numpy as np

app = FastAPI()

# 1. Create a static folder to hold the file
if not os.path.exists("static"):
    os.makedirs("static")

# 2. Mount the static folder so the internet can see it
app.mount("/static", StaticFiles(directory="static"), name="static")

pipeline = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')

@app.get("/tts")
async def generate_tts(text: str, voice: str = "af_heart"):
    generator = pipeline(text, voice=voice, speed=1.0)
    audio_data = [audio for _, _, audio in generator]
    combined_audio = np.concatenate(audio_data)
    
    # 3. Always save as the SAME file name (this replaces the old one)
    file_path = "static/output.wav"
    sf.write(file_path, combined_audio, 24000)
    
    # 4. Return the URL instead of the audio data
    return {"url": "https://shahid202-kokoro-api.hf.space/static/output.wav"}