> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Text to Speech

> 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](https://platform.openai.com/docs/guides/chat) with Google’s [gTTS library](https://pypi.org/project/gTTS/). Generate natural language responses from an LLM and have them spoken aloud automatically.

## Prerequisites

### 1. Install Dependencies

| Package                | Purpose                                                       | Install Command                                    |
| ---------------------- | ------------------------------------------------------------- | -------------------------------------------------- |
| gTTS                   | Google Text-to-Speech Python client                           | `pip install gTTS`                                 |
| OpenAI Python client   | Official OpenAI API SDK                                       | `pip install openai`                               |
| Audio playback utility | Play MP3 files (macOS: `afplay`; Linux: `mpg123` or `mpg321`) | `brew install mpg123` or `sudo apt install mpg123` |

<Callout icon="lightbulb" color="#1CB2FE">
  This example is tested on Python 3.7+. If you use a different version, adjust commands as needed.
</Callout>

### 2. Set Your OpenAI API Key

```bash theme={null}
export OPENAI_API_KEY="your_openai_api_key"
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Never commit your API key to public repositories. Use a secure vault or environment manager in production.
</Callout>

***

## Imports and Client Initialization

Begin by importing standard libraries, gTTS, and initializing the OpenAI client:

```python theme={null}
import os
from gtts import gTTS
from openai import OpenAI

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
```

***

## 1. Define the Prompt

Decide what you want the model to say. For example:

```python theme={null}
prompt = "Tell me a story about a brave knight who loves basketball."
```

***

## 2. Text-to-Speech Function

Convert text to speech and play the resulting MP3:

```python theme={null}
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}")
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Adjust the playback command (`afplay`, `mpg123`, or `mpg321`) based on your operating system.
</Callout>

***

## 3. Generate Text from OpenAI

Send the prompt to the Chat API and retrieve the response:

```python theme={null}
def generate_text(
    prompt: str,
    model: str = "gpt-3.5-turbo",
    temperature: float = 0.8,
    max_tokens: int = 150
) -> str:
    """
    Generate a chat completion for the given prompt.
    """
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
        temperature=temperature,
        max_tokens=max_tokens
    )
    return response.choices[0].message.content
```

***

## 4. Combine Generation and Speech

Create a helper that prints the generated text, then speaks it:

```python theme={null}
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)
```

***

## 5. Entry Point

Run the full pipeline with your defined prompt:

```python theme={null}
if __name__ == "__main__":
    gen_and_speak(prompt)
```

***

## Example Console Output

```bash theme={null}
$ python3 text_to_speech_pipeline.py
Generated Text:

Once upon a time in the kingdom of Eldoria, there lived a brave knight named Sir Cedric...
# (Audio will start playing automatically)
```

***

## Next Steps & Extensions

* Support **multiple languages** by changing `lang` in `text_to_speech()`.
* Experiment with **different voices** and speech parameters.
* Integrate other audio libraries like `pydub` or `playsound` for advanced playback.

***

## Links and References

* [OpenAI Chat API Guide](https://platform.openai.com/docs/guides/chat)
* [gTTS GitHub Repository](https://pypi.org/project/gTTS/)
* [Python os.system Documentation](https://docs.python.org/3/library/os.html#os.system)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-openai/module/b6b7bec7-ed21-47d5-afbb-663df59f5e97/lesson/12a9fcf0-2ab9-461b-bf9a-67fc1c132eb4" />
</CardGroup>
