Nasma commited on
Commit
fb89804
·
verified ·
1 Parent(s): d26a5fd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -25
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
- if not os.path.exists(FIXED_SPEAKER_WAV):
33
- raise HTTPException(status_code=400, detail="Fixed speaker WAV file not found.")
34
-
35
- # StreamingResponse to stream audio chunks
36
- def audio_stream():
37
- if tts.is_multi_lingual and not language:
38
- raise ValueError("Language must be specified for multi-lingual models.")
39
 
40
- text_chunks = split_text(text, 20)
41
-
42
- for idx, chunk in enumerate(text_chunks):
43
- # Generate audio for each chunk and yield as bytes
44
- output_file = f"out_{idx}.wav"
45
- tts.tts_to_file(
46
- text=chunk,
47
  file_path=output_file,
48
  speaker_wav=FIXED_SPEAKER_WAV,
49
  language=language
50
- )
51
- print(output_file)
52
- # Read the file content and yield as binary
53
- with open(output_file, "rb") as audio_file:
54
- yield audio_file.read()
55
- # Optionally delete the file after streaming
56
- os.remove(output_file)
57
-
58
- return StreamingResponse(audio_stream(), media_type="audio/wav")
 
 
 
 
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)