Transcribe the speech using OpenAI in Python

Learn how to transcribe audio to text using OpenAI models in Python. This notebook covers sending transcription requests, processing audio files, handling the response, and printing the transcription results efficiently.

This notebook was created with MLJAR Studio

MLJAR Studio is Python code editior with interactive code recipes and local AI assistant.
You have code recipes UI displayed at the top of code cells.

Documentation

The cell with imports creates automatically:

# import packages
import os
from dotenv import load_dotenv
from openai import OpenAI, AuthenticationError

# we added below import manually
from IPython.display import IFrame

Connection with OpenAI client:

# load .env file
load_dotenv()

# get api key from environment
api_key = os.environ["OPENAI_KEY"]

# create OpenAI client
def create_client(api_key):
    try:
        client = OpenAI(api_key=api_key)
        client.models.list()
        return client
    except AuthenticationError:
        print("Incorrect API")
    return None

client = create_client(api_key)

In this example we will use the piece of new Deadpool trailer:

# we added the cell with IFrame manually to show which video we used
IFrame('https://www.youtube.com/embed/4YBnwjhRapA', width=640, height=360)

This piece of code allows to create the transcription:

# open the file
file = open(r"../../../../Downloads/deadpool-trailer.mp4", "rb")

# send transcription request
transcription = client.audio.transcriptions.create(
    model="whisper-1",
    file=file,
    response_format="text"
)

# print response
print(transcription)

Conclusions

Now you can easily create transcription to almost any files with audio and save it in the format of your choice.

Follow us for more!

Recipes used in the speech-transcription-openai.ipynb

All code recipes used in this notebook are listed below. You can click them to check their documentation.

Packages used in the speech-transcription-openai.ipynb

List of packages that need to be installed in your Python environment to run this notebook. Please note that MLJAR Studio automatically installs and imports required modules for you.

openai>=1.35.14

python-dotenv>=1.0.1