Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,19 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
| 2 |
from kokoro import KPipeline
|
| 3 |
import soundfile as sf
|
| 4 |
-
import
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
pipeline = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')
|
| 10 |
|
| 11 |
@app.get("/tts")
|
|
@@ -14,8 +22,10 @@ async def generate_tts(text: str, voice: str = "af_heart"):
|
|
| 14 |
audio_data = [audio for _, _, audio in generator]
|
| 15 |
combined_audio = np.concatenate(audio_data)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
from kokoro import KPipeline
|
| 4 |
import soundfile as sf
|
| 5 |
+
import os
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# 1. Create a static folder to hold the file
|
| 11 |
+
if not os.path.exists("static"):
|
| 12 |
+
os.makedirs("static")
|
| 13 |
+
|
| 14 |
+
# 2. Mount the static folder so the internet can see it
|
| 15 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 16 |
+
|
| 17 |
pipeline = KPipeline(lang_code='a', model='shahid202/Kokoro-82M-TTS')
|
| 18 |
|
| 19 |
@app.get("/tts")
|
|
|
|
| 22 |
audio_data = [audio for _, _, audio in generator]
|
| 23 |
combined_audio = np.concatenate(audio_data)
|
| 24 |
|
| 25 |
+
# 3. Always save as the SAME file name (this replaces the old one)
|
| 26 |
+
file_path = "static/output.wav"
|
| 27 |
+
sf.write(file_path, combined_audio, 24000)
|
| 28 |
+
|
| 29 |
+
# 4. Return the URL instead of the audio data
|
| 30 |
+
return {"url": "https://shahid202-kokoro-api.hf.space/static/output.wav"}
|
| 31 |
|