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"}