OpenAI

Generate audio speech from text using OpenAI API in Python

Learn how to generate and play speech from text using OpenAI's API in Python. This recipe covers setting the file path, creating an API request, streaming the audio response to a file, and playing the generated speech. Follow these steps to easily convert text into spoken audio and hear the results.

Required packages

You need below packages to use the code generated by recipe. All packages are automatically installed in MLJAR Studio.

openai>=1.35.14

Interactive recipe

You can use below interactive recipe to generate code. This recipe is available in MLJAR Studio.

In the below recipe, we assume that you have following variables available in your notebook:

  • client (type OpenAI)

Python code

# Python code will be here

Code explanation

  1. Set the file path.
  2. Create the API request.
  3. Save response as the audio file.
  4. Play audio in Jupyter Notebook.

Example Python notebooks

Please find inspiration in example notebooks