shahid202 commited on
Commit
140b44e
·
verified ·
1 Parent(s): f158396

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -7
main.py CHANGED
@@ -1,11 +1,19 @@
1
- from fastapi import FastAPI, Response
 
2
  from kokoro import KPipeline
3
  import soundfile as sf
4
- import io
5
  import numpy as np
6
 
7
  app = FastAPI()
8
- # This uses the specific model you cloned
 
 
 
 
 
 
 
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
- buffer = io.BytesIO()
18
- sf.write(buffer, combined_audio, 24000, format='WAV')
19
- buffer.seek(0)
20
- return Response(content=buffer.read(), media_type="audio/wav")
 
 
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