Spaces:
Running
Running
import os | |
from gtts import gTTS | |
from pydub import AudioSegment | |
import tempfile | |
def text_to_speech_with_gtts(input_text, output_filepath): | |
""" | |
Convert text to speech using gTTS and save as an MP3 file. | |
Args: | |
input_text (str): The text to convert to speech. | |
output_filepath (str): Path to save the output MP3 file. | |
Returns: | |
str: Path to the generated MP3 file (for Gradio playback in Spaces). | |
Raises: | |
Exception: If text-to-speech or file saving fails. | |
""" | |
language = "en" | |
try: | |
# Generate MP3 using gTTS | |
audio_obj = gTTS( | |
text=input_text, | |
lang=language, | |
slow=False | |
) | |
audio_obj.save(output_filepath) | |
return output_filepath | |
except Exception as e: | |
raise Exception(f"Error generating text-to-speech: {str(e)}") |