Using Node Js, Chat GPT API, and Whisper API to Generate a Summary of a YouTube Video

Using Node Js, Chat GPT API, and Whisper API to Generate a Summary of a YouTube Video

Introduction

Everyone is talking about Chat GPT these days, and I'm sure you've already tried it and been amazed by its capability to understand and solve problems, and even perform certain tasks. As of early March, the Open AI Team has released a new API for the Chat GPT model, as well as their speech-to-text model, Whisper. The best part? They're easy to use and super cheap. Just think of all the possibilities and fun things we can create with these!

For today Let's make a simple program to summarize a YouTube video using these two APIs in Node.Js.

Let's start with the diagram:

Diagram

Diagram Simple

The diagram is pretty straightforward and simple.

Code

Initiate the Node.js project and install this library.

npm install openai
npm install ytdl-core
npm install dotenv
  1. openai is Node js client library for OpenAi APIS

  2. ytdl-core is a library for downloading the youtube video

  3. dotenv for read .env file

Our project structure will be like this

package.json
package-lock.json
index.js
summary.js
.env
node_modules

First, before we jump into coding, get your API key from Open AI API Keys Page

Store it in .env

//.env
OPENAI_API_KEY=your_key

First, create the step that we want to code:

//index js
/*
* 1. Import all necessary library
* 2. Initiate OpenAI Client
* 3. Download the youtube video
* 4. When the download done, transcribe it
* 5. When transcribing done, now summarize it
* 6. Store the summarize text to .txt file
*/
//index js

//1. Import all necessary library
require('dotenv').config();
const { Configuration, OpenAIApi } = require("openai");
const fs = require('fs');
const ytdl = require('ytdl-core');
const summary = require('./summary');

//2. Initiate OpenAI Client
const main = () => {
    const configuration = new Configuration({
      apiKey: process.env.OPENAI_API_KEY,
    });
    const openai = new OpenAIApi(configuration);
    // 3. Download the youtube video
    const videoStream = fs.createWriteStream('video.mp4');
    ytdl(link, { quality: 'lowest'}).pipe(videoStream);
}

/* 4. When the download done, transcribe it
* 5. When transcribing done, now summarize it
* 6. Store the summarize text to .txt file
*/
//index js

//1. Import all necessary library
require('dotenv').config();
const { Configuration, OpenAIApi } = require("openai");
const fs = require('fs');
const ytdl = require('ytdl-core');
const summary = require('./summary');

//2. Initiate OpenAI Client
const main = () => {
    const configuration = new Configuration({
      apiKey: process.env.OPENAI_API_KEY,
    });
    const openai = new OpenAIApi(configuration);
    // 3. Download the youtube video
    const videoStream = fs.createWriteStream('video.mp4');
    ytdl(link, { quality: 'lowest'}).pipe(videoStream);

    //4. When the download done, transcribe it
    videoStream.on('finish', async () => {
            const transcribe = await openai.createTranscription(fs.createReadStream('./video.mp4'), 'whisper-1');
            //5. When transcribing done, summarize it
            summary.main(transcribe.data.text)
    })
}

/* 
* 
* 6. Store the summarize text to .txt file
*/

Now let us code the summary function in summary.js

//summary.js
const { Configuration, OpenAIApi } = require("openai");
require('dotenv').config();
const fs = require('fs');

const main = async (text) => {
    const configuration = new Configuration({
        apiKey: process.env.OPENAI_API_KEY,
    });
    const openai = new OpenAIApi(configuration);
    const summaryStream = fs.createWriteStream('summary.txt');

    const prompt = `Please summary this text: ${text}`

    const completion = await openai.createChatCompletion({
        model: "gpt-3.5-turbo",
        messages: [{role: "user", "content": prompt}]
    });
    //6. Store the summarize text to .txt file
    summaryStream.write(completion.data.choices[0].message.content)
}

module.exports = {
    main
}

And done! Our simple youtube summarizer is ready to use.

For example, I use this video link as a parameter inindex.js

//index.js
require('dotenv').config();

const { Configuration, OpenAIApi } = require("openai");
const fs = require('fs');
const ytdl = require('ytdl-core');
const summary = require('./summary');


const main = (link) => {
 //...all the code above
}

main('https://youtu.be/KO3h_IoqN3g');

Then run in the terminal:

node index.js

Voila! I got the summary of that video in my summary.txt

The text discusses how negative character traits can be turned into positive things if you can figure out how to do it. Using the example of Joan Crawford, who had a difficult childhood and suffered abuse, the author explains how she was able to turn her pain into great acting. Trauma survivors tend to be extremely sensitive to the emotions of others, which can be both positive and negative, but Crawford was able to use this sensitivity to read the minds of her directors and turn her negative experiences into something productive.

Limitation

Due to the beta release of Whisper API, the limit of files that can be transcribed is 25 MB. We can split the video/audio first before we transcribe it.

Thanks for reading! Please comment if there are any questions

I would appreciate any feedback or suggestions that could help enhance the quality and readability of my writing. Thank you!