File size: 859 Bytes
95841bc
 
 
 
 
 
5883d20
 
95841bc
5883d20
 
 
95841bc
5883d20
 
95841bc
5883d20
 
 
 
95841bc
5883d20
 
 
 
 
 
 
 
95841bc
5883d20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)}")