|
8 | 8 | import tempfile |
9 | 9 |
|
10 | 10 | load_dotenv() |
11 | | -DEFAULT_SPEED = float(os.getenv('DEFAULT_SPEED')) |
12 | | -DEFAULT_LANGUAGE = os.getenv('DEFAULT_LANGUAGE') |
13 | | -DEFAULT_SPEAKER_ID = os.getenv('DEFAULT_SPEAKER_ID') |
14 | | -device = 'auto' # Will automatically use GPU if available |
| 11 | +DEFAULT_SPEED = float(os.getenv("DEFAULT_SPEED")) |
| 12 | +DEFAULT_LANGUAGE = os.getenv("DEFAULT_LANGUAGE") |
| 13 | +DEFAULT_SPEAKER_ID = os.getenv("DEFAULT_SPEAKER_ID") |
| 14 | +device = "auto" # Will automatically use GPU if available |
| 15 | + |
15 | 16 |
|
16 | 17 | class TextModel(BaseModel): |
17 | 18 | text: str |
18 | 19 | speed: float = DEFAULT_SPEED |
19 | 20 | language: str = DEFAULT_LANGUAGE |
20 | 21 | speaker_id: str = DEFAULT_SPEAKER_ID |
21 | 22 |
|
| 23 | + |
22 | 24 | app = FastAPI() |
23 | 25 |
|
| 26 | + |
24 | 27 | def get_tts_model(body: TextModel): |
25 | 28 | return TTS(language=body.language, device=device) |
26 | 29 |
|
| 30 | + |
27 | 31 | @app.post("/convert/tts") |
28 | | -async def create_upload_file(body: TextModel = Body(...), model: TTS = Depends(get_tts_model)): |
| 32 | +async def create_upload_file( |
| 33 | + body: TextModel = Body(...), model: TTS = Depends(get_tts_model) |
| 34 | +): |
29 | 35 | speaker_ids = model.hps.data.spk2id |
30 | 36 |
|
31 | | - # Create a temporary file |
32 | | - output_path = body.language + "_" + body.speaker_id + ".wav" |
33 | | - model.tts_to_file(body.text, speaker_ids[body.speaker_id], output_path, speed=body.speed) |
| 37 | + print(os.path.basename(body.text)) |
| 38 | + |
| 39 | + # Use a temporary file |
| 40 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp: |
| 41 | + output_path = tmp.name |
| 42 | + model.tts_to_file( |
| 43 | + body.text, speaker_ids[body.speaker_id], output_path, speed=body.speed |
| 44 | + ) |
34 | 45 |
|
35 | | - print(os.path.basename(output_path)) |
36 | | - # Return the audio file |
37 | | - response = FileResponse(output_path, media_type="audio/mpeg", filename=os.path.basename(output_path)) |
| 46 | + # Return the audio file, ensure the file is not deleted until after the response is sent |
| 47 | + response = FileResponse( |
| 48 | + output_path, media_type="audio/mpeg", filename=os.path.basename(output_path) |
| 49 | + ) |
38 | 50 |
|
39 | 51 | return response |
40 | 52 |
|
| 53 | + |
41 | 54 | if __name__ == "__main__": |
42 | 55 | uvicorn.run(app, host="0.0.0.0", port=8080) |
0 commit comments