I have been working on my Podcast Player & Summarizer project and I need to build the component for summarizing a podcast. The main application is built with Next.js but I need the summarizer code to be in Python so I can integrate it to my Audio Transcriber API.
In this blog post, I will walk you through the process of summarizing podcasts with Python. We will use the Whisper model for transcribing the podcast audio, and then we’ll use the Chat Completions API to generate the summary.
Where to find the podcast MP3 file?
You can get the URL of the MP3 file from the RSS feed of your favorite podcast show. For example, one of my favorite podcast shows is Motley Fool Money and here is their RSS Feed.
If you’re unable to get the RSS feed of your favorite shows, you can try to get the feed from other sources such as Apple Podcasts or search on Listen Notes.
Let’s start by installing the required packages for the code. Install using pip:
The first step is to download the MP3 file and save it to local file:
Python
We are using the stream=True when making the GET request to retrieve the data in chunks rather than load everything into memory at once. This is useful if the MP3 file is large. The downloaded audio then saved to podcast.mp3 file in the current directory.
Transcribing the audio
We will use the OpenAI Whisper model to transcribe the downloaded MP3 file in 5 lines of code:
Python
It might take a while for the model to complete the transcribing process, depending on the size of the audio file.
Generating the summary
Now that we have the transcript of the podcast, we can create a prompt to generate the summary using the Chat Completions API .
Get your OpenAI API key and paste to the following code:
Python
Create the prompt:
Python
Keep in mind that each model has maximum number of tokens for the prompt. The gpt-3.5-turbo-16k model has maximum context window of 16,385 tokens. In the code above, we simply cut off the prompt into 16,000 tokens if the podcast transcript exceeds that number.
Now we can submit the prompt to the Chat Completions API and retrieve the summary:
Python
The result
Here is the result of the code from summarizing the “What Real Estate and Stock Investors Have In Common” episode on my local computer:
Text
Overall it gives a very good summary and I am very satisfied with the result.