Part 4: How to do Question and Answer on YouTube videos using OpenAI API in Python
Earlier I had written tutorial on how to chat with your documents in a Local Chatbot using OpenAI API.
In this article, I will show how to use chatgpt API to do question and answer on transcription of youtube videos.
We will first transcribe videos using speech to text model named Whisper provided by OpenAI. Then we will feed the text into OpenAI API for chatgpt model.
OpenAI has provided Whisper model for free download. So I will download the whisper model and do the inference on local machine. Since my laptop does not have sufficient RAM, so I have run the code in google Colab.
For extracting the audio of youtube video I have used pytube library.
First we install the necessary libraries
pip install pytube
pip install git+https://github.com/openai/whisper.git -q
pip install openai
Import the necessary libraries
import pytube
import whisper
from openai import OpenAI
Obtain link for youtube video
video= 'https://www.youtube.com/watch?v=p-nOOZ8u1oM'
Extract audio using pytube
data = pytube.YouTube(video)
audio = data.streams.get_audio_only()
audio.download()
Convert speech to text using whisper model
model = whisper.load_model('large')
text = model.transcribe('path to audi')
We can print transcription of video using
text['text']
Next we initialise helper function for calling OpenAI GPT3.5 turbo API.
message = []
message.append({"role": "user", "content": text['text'] })
def helper(message, question):
message.append({"role": "user", "content": question })
response = client.chat.completions.create(
messages=message,
model="gpt-3.5-turbo", # gpt 3.5 turbo
# model="gpt-4",
# model = "gpt-4-1106-preview", #gpt-4 turbo
stream = False
)
return response.choices[0].message.content
Next we can do question and answer on youtube transcription using following code
question = 'write summary of content'
helper(message,question)
This will return the answer of OpenAI API on our questions on youtube video.
If you liked the article, please like and subscribe my account.