How to Build a YouTube Video Query AI Model on Wardley Mapping with Python and OpenAI

Have you ever wanted to generate responses to user queries based on the content of a YouTube video

Mark Craddock
Prompt Engineering
4 min readApr 24, 2023

--

Created with Fotor.com

Introduction

Have you ever wanted to generate responses to user queries based on the content of a YouTube video? In this tutorial, we’ll show you how to build a vector database from a video’s transcript and use it to generate AI-generated responses to user queries.

We’ll be using Python, OpenAI, and several key libraries and modules, including openai, streamlit, llama_index, and gpt_index.

Background

Let’s start with some background on the tools we’ll be using. The YoutubeTranscriptReader module from gpt_index is used to extract the transcript from a YouTube video. The transcript is then converted into a list of documents, which are passed to the GPTSimpleVectorIndex class from llama_index. This class builds a vector representation of each document, allowing for efficient similarity matching.

AI Model

The GPTSimpleVectorIndex class from llama_index builds a vector representation of each document using a pre-trained language model (in this case, GPT-3). By representing documents as vectors, it becomes possible to efficiently compare their similarity using vector operations such as cosine similarity.

The PromptHelper class from llama_index is also used to generate a base prompt for the AI model. This prompt provides instructions for the AI to follow when generating responses, such as using Wardley Mapping examples and keeping the language simple.

Web Interface

The resulting index is used to generate responses to user queries via a Streamlit web interface. Users can enter a prompt in a text input field, and the index will generate a response based on the most similar document in its database. The response is displayed in a text area below the input field.

The web interface is customised using Streamlit, and includes a video player for the YouTube video, as well as branding and version information in the sidebar. Users can also clear the input and reset the messages to the default BASE_PROMPT.

Code

Let’s take a closer look at the code. First, we import the necessary libraries and modules:

import openai
import llama_index
from llama_index import LLMPredictor, GPTSimpleVectorIndex, PromptHelper
import streamlit as st
from pathlib import Path
from gpt_index import download_loader

Next, we set up the OpenAI API key:

openai.api_key = st.secrets["OPENAI_API_KEY"]

Then, we use download_loader from gpt_index to extract the transcript from a YouTube video. This will fetch the transcript from the YouTube video. Not all videos have a transcript and it’s never 100% accurate:

YoutubeTranscriptReader = download_loader("YoutubeTranscriptReader")
loader = YoutubeTranscriptReader()
documents = loader.load_data(ytlinks=['https://www.youtube.com/watch?v=KkePAhnkHeg'])

We then use the GPTSimpleVectorIndex class from llama_index to build a vector database from the transcript:

index = GPTSimpleVectorIndex.from_documents(documents)

Now we can set up the Streamlit web interface:

st.set_page_config(page_title="Intro To Wardley Mapping with AI")
st.title("Intro To Wardley Mapping with AI")
st.sidebar.markdown("# Query this video using AI")
st.sidebar.markdown("Developed by Mark Craddock](https://twitter.com/mcraddock)", unsafe_allow_html=True)
st.sidebar.markdown("Current Version: 0.0.2")
st.video('https://youtu.be/KkePAhnkHeg') text = st.empty()prompt = st.text_input("Prompt", value="What is this video about?")query_engine = index.as_query_engine()if st.button("Send"):
with st.spinner("Generating response..."):

response = query_engine.query(prompt)
text.text_area("Messages", response, height=250)
if st.button("Clear"):
st.session_state["messages"] = BASE_PROMPT
show_messages(text)

Deployment

Clone this GitHub repository

Now, pop over to streamlit.io and create an account and link it to your github repository.

Streamlit should start building and deploying your app. If you get the following error when it starts, make sure you add your OpenAI API key to the settings.

You’ll find ‘settings’ on the menu in the bottom right hand corner of the web page.

It should look something like this.

Within seconds, you should have a fully functioning app like this.

Conclusion

This code provides a useful example of how to scrape and index text data from a video source, and use it to generate responses to user queries via an AI model. It could be useful for a variety of applications, such as chatbots, question-answering systems, or content recommendation engines.

--

--

Mark Craddock
Prompt Engineering

Techie. Built VH1, G-Cloud, Unified Patent Court, UN Global Platform. Saved UK Economy £12Bn. Now building AI stuff #datascout #promptengineer #MLOps #DataOps