Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -10,15 +10,16 @@ app = FastAPI()
|
|
| 10 |
# Initialize the TTS model
|
| 11 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False) # Set gpu=True if you have GPU support
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to split text into chunks
|
| 14 |
def split_text(text: str, words_per_chunk: int = 20):
|
| 15 |
words = text.split()
|
| 16 |
return [' '.join(words[i:i + words_per_chunk]) for i in range(0, len(words), words_per_chunk)]
|
| 17 |
|
| 18 |
# Function to generate audio chunks
|
| 19 |
-
def generate_audio_chunks(
|
| 20 |
-
text: str, speaker_wav: str, language: str, chunk_size: int = 20
|
| 21 |
-
) -> Generator[bytes, None, None]:
|
| 22 |
if tts.is_multi_lingual and not language:
|
| 23 |
raise ValueError("Language must be specified for multi-lingual models.")
|
| 24 |
|
|
@@ -30,7 +31,7 @@ def generate_audio_chunks(
|
|
| 30 |
tts.tts_to_file(
|
| 31 |
text=chunk,
|
| 32 |
file_path=audio_buffer,
|
| 33 |
-
speaker_wav=
|
| 34 |
language=language
|
| 35 |
)
|
| 36 |
audio_buffer.seek(0)
|
|
@@ -39,16 +40,15 @@ def generate_audio_chunks(
|
|
| 39 |
@app.post("/generate-audio/")
|
| 40 |
async def generate_audio(
|
| 41 |
text: str = Query(..., description="The input text to convert to speech."),
|
| 42 |
-
language: str = Query("en", description="Language code for TTS (e.g., 'en' for English).")
|
| 43 |
-
speaker_wav: str = Query(..., description="Path to the WAV file for voice cloning.")
|
| 44 |
):
|
| 45 |
-
if not os.path.exists(
|
| 46 |
-
raise HTTPException(status_code=400, detail="
|
| 47 |
|
| 48 |
# StreamingResponse to stream audio chunks
|
| 49 |
def audio_stream():
|
| 50 |
try:
|
| 51 |
-
for audio_chunk in generate_audio_chunks(text=text,
|
| 52 |
yield audio_chunk
|
| 53 |
except Exception as e:
|
| 54 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 10 |
# Initialize the TTS model
|
| 11 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False) # Set gpu=True if you have GPU support
|
| 12 |
|
| 13 |
+
# Predefined path to the sample voice clone
|
| 14 |
+
FIXED_SPEAKER_WAV = "C:/Users/nasma/OneDrive/Desktop/voiceclone/voicecloneapi/Bible Verses About Community.wav"
|
| 15 |
+
|
| 16 |
# Function to split text into chunks
|
| 17 |
def split_text(text: str, words_per_chunk: int = 20):
|
| 18 |
words = text.split()
|
| 19 |
return [' '.join(words[i:i + words_per_chunk]) for i in range(0, len(words), words_per_chunk)]
|
| 20 |
|
| 21 |
# Function to generate audio chunks
|
| 22 |
+
def generate_audio_chunks(text: str, language: str, chunk_size: int = 20) -> Generator[bytes, None, None]:
|
|
|
|
|
|
|
| 23 |
if tts.is_multi_lingual and not language:
|
| 24 |
raise ValueError("Language must be specified for multi-lingual models.")
|
| 25 |
|
|
|
|
| 31 |
tts.tts_to_file(
|
| 32 |
text=chunk,
|
| 33 |
file_path=audio_buffer,
|
| 34 |
+
speaker_wav=FIXED_SPEAKER_WAV,
|
| 35 |
language=language
|
| 36 |
)
|
| 37 |
audio_buffer.seek(0)
|
|
|
|
| 40 |
@app.post("/generate-audio/")
|
| 41 |
async def generate_audio(
|
| 42 |
text: str = Query(..., description="The input text to convert to speech."),
|
| 43 |
+
language: str = Query("en", description="Language code for TTS (e.g., 'en' for English).")
|
|
|
|
| 44 |
):
|
| 45 |
+
if not os.path.exists(FIXED_SPEAKER_WAV):
|
| 46 |
+
raise HTTPException(status_code=400, detail="Fixed speaker WAV file not found.")
|
| 47 |
|
| 48 |
# StreamingResponse to stream audio chunks
|
| 49 |
def audio_stream():
|
| 50 |
try:
|
| 51 |
+
for audio_chunk in generate_audio_chunks(text=text, language=language):
|
| 52 |
yield audio_chunk
|
| 53 |
except Exception as e:
|
| 54 |
raise HTTPException(status_code=500, detail=str(e))
|