triflix commited on
Commit
86716a1
·
verified ·
1 Parent(s): 2061a01

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +42 -15
main.py CHANGED
@@ -1,3 +1,9 @@
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import StreamingResponse
3
  from fastapi.staticfiles import StaticFiles
@@ -7,29 +13,50 @@ import numpy as np
7
  import soundfile as sf
8
 
9
  app = FastAPI()
 
10
  pipeline = KPipeline(lang_code='a')
11
 
12
- # Mount static/ at /static
13
- app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
14
 
15
  @app.get("/tts-stream")
16
  def tts_stream(text: str):
17
- # Generate Kokoro audio segments without splitting
18
- segs = list(pipeline(text, voice='af_sky', speed=1.0, split_pattern=r'$^'))
 
 
 
 
 
 
 
 
 
 
19
  # Concatenate into one array
20
- full = np.concatenate([audio for (_, _, audio) in segs])
21
- # Write WAV into in-memory buffer
22
  buf = io.BytesIO()
23
- sf.write(buf, full, 24000, format="WAV")
24
  buf.seek(0)
25
- # Stream as audio/wav
26
- return StreamingResponse(buf, media_type="audio/wav")
27
 
28
  @app.get("/tts-file")
29
  def tts_file(text: str):
30
- segs = list(pipeline(text, voice='af_sky', speed=1.0, split_pattern=r'$^'))
31
- full = np.concatenate([audio for (_, _, audio) in segs])
32
- # Save to static/output_full.wav
33
- path = "static/output_full.wav"
34
- sf.write(path, full, 24000)
35
- return {"url": f"/static/output_full.wav"}
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # Ensure HF cache dirs are set before any HF imports
3
+ os.environ['HF_HOME'] = '/app/.cache/huggingface'
4
+ os.environ['HUGGINGFACE_HUB_CACHE'] = '/app/.cache/huggingface/hub'
5
+ os.environ['TRANSFORMERS_CACHE'] = '/app/.cache/huggingface'
6
+
7
  from fastapi import FastAPI
8
  from fastapi.responses import StreamingResponse
9
  from fastapi.staticfiles import StaticFiles
 
13
  import soundfile as sf
14
 
15
  app = FastAPI()
16
+ # Initialize Kokoro TTS pipeline for American English
17
  pipeline = KPipeline(lang_code='a')
18
 
19
+ # Mount the static/ directory at /static
20
+ app.mount(
21
+ "/static", StaticFiles(directory="static"), name="static"
22
+ )
23
 
24
  @app.get("/tts-stream")
25
  def tts_stream(text: str):
26
+ """
27
+ Generate and stream a single continuous WAV audio using af_sky voice.
28
+ """
29
+ # Generate segments without splitting
30
+ segments = list(
31
+ pipeline(
32
+ text,
33
+ voice='af_sky',
34
+ speed=1.0,
35
+ split_pattern=r'$^' # never matches → full text as one segment
36
+ )
37
+ )
38
  # Concatenate into one array
39
+ audio_full = np.concatenate([audio for (_, _, audio) in segments])
40
+ # Write to in-memory buffer as WAV
41
  buf = io.BytesIO()
42
+ sf.write(buf, audio_full, 24000, format='WAV')
43
  buf.seek(0)
44
+ return StreamingResponse(buf, media_type='audio/wav')
 
45
 
46
  @app.get("/tts-file")
47
  def tts_file(text: str):
48
+ """
49
+ Generate a full WAV file, save to static/, and return its URL.
50
+ """
51
+ segments = list(
52
+ pipeline(
53
+ text,
54
+ voice='af_sky',
55
+ speed=1.0,
56
+ split_pattern=r'$^'
57
+ )
58
+ )
59
+ audio_full = np.concatenate([audio for (_, _, audio) in segments])
60
+ output_path = 'static/output_full.wav'
61
+ sf.write(output_path, audio_full, 24000)
62
+ return {"url": f"/static/output_full.wav"}