Build a text-to-speech pipeline in Python using OpenAI’s Chat API and Google’s gTTS library for generating and playing spoken responses.
Build a seamless text-to-speech pipeline in Python by combining OpenAI’s Chat API with Google’s gTTS library. Generate natural language responses from an LLM and have them spoken aloud automatically.
Convert text to speech and play the resulting MP3:
Copy
Ask AI
def text_to_speech(text: str, lang: str = "en", slow: bool = False) -> None: """ Generate speech from text using gTTS, save as MP3, and play it. """ tts = gTTS(text=text, lang=lang, slow=slow) filename = "tts_output.mp3" tts.save(filename) # macOS uses 'afplay'; Linux users can install 'mpg123' or 'mpg321' os.system(f"afplay {filename}")
Adjust the playback command (afplay, mpg123, or mpg321) based on your operating system.
Create a helper that prints the generated text, then speaks it:
Copy
Ask AI
def gen_and_speak(prompt: str) -> None: """ Generate text from the prompt, display it, and play the speech. """ text = generate_text(prompt) print("Generated Text:\n") print(text, "\n") text_to_speech(text)
$ python3 text_to_speech_pipeline.pyGenerated Text:Once upon a time in the kingdom of Eldoria, there lived a brave knight named Sir Cedric...# (Audio will start playing automatically)