Streamline Your YouTube Watching with an AI-Powered Summarizer by Lyzr Low-Code Agent
Tags: ai, beginners, tutorial, python
Published: 2024-03-21 05:18:51

In today's digital age, YouTube serves as a vast repository of information, with countless videos covering a wide array of topics. However, sifting through lengthy videos to find the information you need can be time-consuming and inefficient. But what if there was a way to access key insights and summaries without having to watch the entire video? Enter [Lyzr's ](https://www.lyzr.ai/) YouTube Summarizer, a powerful tool designed to provide users with quick access to essential information extracted from YouTube videos.

Let's see how you can build this LLM by your own with the help of Lyzr's Low-Code Agents.

---
{% embed https://docs.lyzr.ai/homepage %}
---

## Setting Up the Project Initials
**Clone the YouTube Summarizer app repository**
Link- [YouTube Summarizer ](https://github.com/PrajjwalLyzr/YouTube-Summarizer)

**Set up a virtual environment**

```bash
python3 -m venv venv
source venv/bin/activate

```
**Create an environment file named _.env_ and add your OpenAI API key and Weaviate cluster URL and API key**

```makefile
OPENAI_API_KEY = “Your_OpenAI_API_Key”
VECTOR_STORE_URL = “Your_Weaviate_Cluster_URL"
VECTOR_STORE_API = "Your_Weaviate_Cluster_API"

```
**Install the required dependencies**

```bash
pip install -r requirements.txt
```

## Project Structure

The YouTube Summarizer project structure looks as follows

```scss
YouTube-Summarizer
│
├── utils/
│   ├── __init__.py
│   └── utils.py
│
├── app.py
│
├── README.md
│
├── .env
│
├── .gitignore
│
└── requirements.txt

```
The utils directory contains utility functions used throughout the application, while app.py serves as the main entry point for the YouTube Summarizer application.

## Quick Look of YouTube Summarizer

{% embed https://youtu.be/okjMFX2ZHBQ %}

## Summarizing YouTube Videos

The summarizer function utilizes Lyzr's QABot to extract key information from a YouTube video, we'll write this code inside the utils.py file.

```python
def summarizer(agent):
    summary = agent.query("What is this video about? Can you explain in a short paragraph?")
    keypoints = agent.query('Provide 3-5 key points from this video?')
    content_analysis = agent.query('Write any examples or case studies mentioned in the video? If so, what are they?')
    audience_engagement = agent.query("""
                                        "How does the speaker engage with the audience in the video?"
                                        "Are there any interactive elements or calls to action in the video?"
                                        """)
   
    additional_information = agent.query("""
                                        "Are there any references or citations provided in the video?"
                                        "Can you identify any trends or patterns in the content of the video?"
                                        """)
    
    if summary is not None:
        st.subheader('Video Summary')
        st.write(summary.response)
        st.write(keypoints.response)
        if len(content_analysis.response) > 150:
            st.subheader('Content Analysis')
            st.write(content_analysis.response)
            if len(audience_engagement.response) > 150:
                st.subheader('Audience Engagement')
                st.write(audience_engagement.response)
                if len(additional_information.response) > 150:
                    st.header('Additional Information')
                    st.write(additional_information.response)

```
This function queries the QABot agent to extract a short summary, key points, content analysis, audience engagement details, and additional information from the video. Responses exceeding 150 characters are displayed using Streamlit's st.write() function.

## Creating the Entry point for our Application
Now we will write all the core functionalities inside the main file called as app.py, this file work as a entry point for our YouTube-Summarizer app.

```python
import os
from PIL import Image
from utils import utils
import streamlit as st
from urllib.parse import urlparse, parse_qs
from dotenv import load_dotenv; load_dotenv()
from lyzr import QABot

# Youtube Summarizer Application
    
# replace this with your openai api key or create an environment variable for storing the key.
os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY')
```
First we started with the importing of basic libraries, here the QABot from lyzr will help us to achieve our goal. Then, we need to setup the openai api key from fetching it from the .env file.

## Parsing YouTube Video ID

In the app.py file, the youtube_video_id function parses a YouTube video URL to extract the video ID

```python
def youtube_video_id(url):
    parsed_url = urlparse(url)
    query_params = parse_qs(parsed_url.query)
    video_id = query_params.get('v')
    if video_id:
        return video_id[0]  # Returns the YouTube video ID
    else:
        return None

```
This function takes a YouTube video URL as input and returns the video ID if found, or None otherwise.

## Configuring Vector Store

The vector_store_configuration function generates configuration parameters required for interacting with a Weaviate vector store

```python
def vector_store_configuration(id):
    vector_store_params = {
        "vector_store_type": "WeaviateVectorStore",
        "url": os.getenv('VECTOR_STORE_URL'),
        "api_key": os.getenv('VECTOR_STORE_API'),
        "index_name": f"Video{id}IndexName"
    }
   
    return vector_store_params

```
This function constructs a dictionary containing the type of vector store, its URL, API key, and a custom index name based on the video ID.


## Initiating Summarization

The lyzr_yt_summarizer function initializes the YouTube summarizer agent

```python
def lyzr_yt_summarizer(youtube_id, vector_params):
    yt_summarizer = QABot.youtube_qa(
            urls=[youtube_id],
            vector_store_params=vector_params
        )
   
    return yt_summarizer

```
This function initializes the QABot with the YouTube video ID and vector store parameters, enabling it to process and analyze the video content.

With the few lines of code, we have successfully implemented the LLM agent for creating youtube video's summary, this is possible because of lyzr's Agent, we can build our LLM application with the ease without having deep technical understanding of Generative AI and LLM's, we just need to call the lyzr agent and all the things are taken care by agent itself.

## Running the Application
The summarizer function is invoked from the utils module, which generates and displays a summary of the YouTube video based on the initialized summarizer agent.

```python
if __name__ == "__main__":
    style_app()
    yt_url = st.text_input("Paste YouTube video URL here")
    if st.button("Summarize"):
        if len(yt_url) > 0:
            youtube_id = youtube_video_id(url=yt_url)
            if youtube_id is not None:
                vector_config = vector_store_configuration(id=youtube_id)
                summarizer_agent = lyzr_yt_summarizer(youtube_id=youtube_id, vector_params=vector_config)
                utils.summarizer(agent=summarizer_agent)
            else:
                st.error('Please provide a valid YouTube video URL')
        else:
            st.error('Please paste the YouTube video URL')

```
Upon clicking the "Summarize" button, the application verifies the presence of a valid URL, extracts the video ID, configures the vector store parameters, initializes the YouTube summarizer agent, and generates a summary of the video content.

## Conclusion

With Lyzr's YouTube Summarizer, you can now access key insights and summaries from YouTube videos quickly and efficiently. Say goodbye to long hours of video-watching and hello to informed decision-making with just a few clicks. Try out the YouTube Summarizer today and unlock efficiency in your YouTube watching experience!

## References

For further exploration and engagement, refer to Lyzr's website, book a demo, or join the community channels on Discord and Slack.
YouTube Summarizer: [GitHub](https://github.com/PrajjwalLyzr/YouTube-Summarizer)
Lyzr Website: [https://www.lyzr.ai/](https://www.lyzr.ai/)
Book a Demo: [https://www.lyzr.ai/book-demo/](https://www.lyzr.ai/book-demo/)
Lyzr Community Channels: [Discord](https://discord.com/invite/nm7zSyEFA2), [Slack](https://anybodycanai.slack.com/join/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw#/shared-invite/email)