Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from fastapi import FastAPI, Query, HTTPException
|
2 |
from fastapi.responses import StreamingResponse
|
|
|
3 |
from TTS.api import TTS
|
4 |
import os
|
5 |
from io import BytesIO
|
@@ -27,32 +28,31 @@ def split_text(text: str, words_per_chunk: int = 20):
|
|
27 |
@app.post("/generate-audio/")
|
28 |
async def generate_audio(
|
29 |
text: str = Query(..., description="The input text to convert to speech."),
|
30 |
-
language: str = Query("en", description="Language code for TTS (e.g., 'en' for English).")
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if tts.is_multi_lingual and not language:
|
38 |
-
raise ValueError("Language must be specified for multi-lingual models.")
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
tts.tts_to_file(
|
46 |
-
text=chunk,
|
47 |
file_path=output_file,
|
48 |
speaker_wav=FIXED_SPEAKER_WAV,
|
49 |
language=language
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, Query, HTTPException
|
2 |
from fastapi.responses import StreamingResponse
|
3 |
+
from fastapi.responses import FileResponse
|
4 |
from TTS.api import TTS
|
5 |
import os
|
6 |
from io import BytesIO
|
|
|
28 |
@app.post("/generate-audio/")
|
29 |
async def generate_audio(
|
30 |
text: str = Query(..., description="The input text to convert to speech."),
|
31 |
+
language: str = Query("en", description="Language code for TTS (e.g., 'en' for English).")):
|
32 |
+
|
33 |
+
if not os.path.exists(FIXED_SPEAKER_WAV):
|
34 |
+
raise HTTPException(status_code=400, detail="Fixed speaker WAV file not found.")
|
35 |
+
|
36 |
+
if tts.is_multi_lingual and not language:
|
37 |
+
raise ValueError("Language must be specified for multi-lingual models.")
|
|
|
|
|
38 |
|
39 |
+
# Generate audio for each chunk and yield as bytes
|
40 |
+
output_file = f"out.wav"
|
41 |
+
try:
|
42 |
+
tts.tts_to_file(
|
43 |
+
text=text,
|
|
|
|
|
44 |
file_path=output_file,
|
45 |
speaker_wav=FIXED_SPEAKER_WAV,
|
46 |
language=language
|
47 |
+
)
|
48 |
+
print(output_file)
|
49 |
+
# Return the generated audio file as a response
|
50 |
+
return FileResponse(output_file, media_type="audio/wav")
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
raise HTTPException(status_code=500, detail=f"Error generating audio: {str(e)}")
|
54 |
+
|
55 |
+
# finally:
|
56 |
+
# # Clean up the generated file after the response is sent
|
57 |
+
# if os.path.exists(output_file):
|
58 |
+
# os.remove(output_file)
|