Tutorials

Building an AI-powered YouTube title and thumbnail generator (Part 1)

April 17, 2024 by stein ove helset

In this article, I’m going to show you how to get started with GPTScript by installing and setting up everything we need, and then build a script that can generate a title and a thumbnail based on the script for a video.

For additional reference I’ve created this video guide below:

<iframe width=”560″ height=”315″ src=”https://www.youtube.com/embed/r9nJ_lZyiKk?si=EGCtqZelOiyTFp6q” title=”YouTube video player” frameborder=”0″ allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share” referrerpolicy=”strict-origin-when-cross-origin” allowfullscreen></iframe>

Getting started

First of all, what is GPTScript?

GPTScript is sort of like a natural programming language where you can make or use tools to do a lot of awesome things, for example using the OpenAI API to understand what a text is all about and then generate an image based on this.

Instead of explaining too much here, you can read the blog post from the official launch here: https://www.acorn.io/resources/blog/introducing-gptscript

Installing and setting up GPTScript

It is very easy to get GPTScript up and running on your own computer.

A short guide:

  1. First you install the CLI (Command line interface) by running this command:
MacOS / Linux:
brew install gptscript-ai/tap/gptscript

Windows:
winget install gptscript-ai.gptscript
  1. Get your OpenAI API key by going here (https://platform.openai.com/api-keys)

  2. Add the API key to your environment by running this command:

MacOS / Linux:
export OPENAI_API_KEY="your-api-key”

Windows:
$env:OPENAI_API_KEY = 'your-api-key'

And now you should be ready to jump to the next step. If you need more details for the installation, you can go to the official getting started guide for GPTScript by clicking here (https://docs.gptscript.ai/getting-started).

Prices

Before you start running the commands for running GPTScript, it might be smart to take a look at the price lists here:
https://openai.com/pricing

Using OpenAI’s API is not free, but you do not pay for more than you actually use.

The script we are building in this tutorial will use a language model called GPT-4 Turbo and an Image model called DALL-E 3. Per thumbnail we are generating, we are spending $0.12 for the image model and a few cents for the rest. This can vary a little bit based on what you actually are sending in to the GPTscript, how long the texts are, etc.

Take a good look at the price lists to familiarize your self with them.

How to create a script

The first thing I want to to now is to create a script we can run. The project we’re building today will be called “gpt-youtube-generator”. You can create a folder with this name on your computer, or you can call the project a different name if you want to do so.

Inside the “gpt-youtube-generator” folder, create a script called “youtube-generator.gpt”.

Open up the file you created in an editor and add the following content:

Say Hello GPTScript!

This is almost as basic as you can make a script. Let’s try running it by typing this command in a terminal:

$ gptscript youtube-generator.gpt

What this does is that it just tells the script to “Say” “Hello GPTScript!”. Your terminal should now be printing that on the screen.

How to get arguments from the command line

Let’s do some changes to the “youtube-generator.gpt” file:

description: Generates a YouTube title and thumbnail based on a script.
args: script: The script to generate title and thumbnail for.

Print the script on the screen.

As you can see, here are a few new things.

  1. The “description” is not required at this point, but it’s nice to provide a description of what you want this script to achieve.

  2. Next we have a key called “args” where we add one argument called “script”. Here you need to provide a short explanation of what the argument is used for

Let’s try running it:

$ gptscript gpttest.gpt

Results:
OUTPUT:
Could you please provide the script you want to be printed on the screen?

Here we did not provide a “script” or any other arguments for the script. This resulted the script to notify about this exact problem. Plus it also says what it wanted with the argument.

Let’s try again, now with a “--script” argument with the value “This is a test”:

$ gptscript gpttest.gpt --script This is a test

Results:
INPUT:
--script This is a test

OUTPUT:
This is a test
```json
Now you can see that the script understands what the input is. When this is processed, we see an output on the screen.

## Instructing the script to generate a title based on the input

Now that we know how to read the arguments from the terminal, we can go one step further and generate a title for a YouTube video based on this.

Let’s make some more changes to the “youtube-generator.gpt” file:
```json
description: Generates a YouTube title and thumbnail based on a script.
args: script: The script to generate title and thumbnail for.

Do the following steps in acsending order:
1. Come up with an appropriate title for the video based on the script.
2. Print the title on the screen.

Here isn’t really any new concepts here. We have just instructed GPTScript in what to do with the script we provide. It can be helpful to be specific like we are here when we say that the script should do the following steps in ascending order.

We can now try running it and see what we get:

$ gptscript gpttest.gpt --script This is a test for a video where you will learn about GPTScript

Results:
INPUT:
--script This is a test for a video where you will learn about GPTScript

OUTPUT:
"Mastering GPTScript: A Comprehensive Guide"

So there you have a unique title generated by the simple GPTScript we have created.

Creating a tool for making a folder

I want to make a folder where we can store the thumbnail we are going to generate. And I want the name of the folder to be the title of the video.

To achieve this, we need to create a tool which our script can use.

Let’s make some more changes to the “youtube-generator.gpt” file:

tools: mkdir, sys.write # 1. new
description: Generates a YouTube title and thumbnail based on a script.
args: script: The script to generate title and thumbnail for.

Do the following steps in acsending order:
1. Come up with an appropriate title for the video based on the script.
2. Create the `${video-title}` folder if it does not already exist. # 2. new/replaced

--- # 3 new
name: mkdir # 4 new
tools: sys.write # 5 new
description: Creates a folder in your system # 6 new
args: dir: Path to the folder to be created. # 7 new

#!bash # 8 new

mkdir -p "${dir}" # 9 new

This time we have done quite a few changes. Let’s go through them one by one.

  1. First we define which tools we are going to use in this script. “mkdir” is a tool we are creating in this step. “sys.write” is a tool built into GPTScript for writing files, folders, etc in your file system.

  2. Instead of just printing the name of the video on the screen, we now call the tool we are creating and instructing it to create a folder with the name of the video. As you can see, GPTScript just automatically understand that it should be using the tool.

  3. All tools should come after the “main” script we are building. Tools are separated by three dashes “---”.

  4. Define a name for the tool. In our case it will be “mkdir”.

  5. Tell the tool which other tools to import and use for this exact tool.

  6. Write a little description for what this tool does.

  7. Just like the main script, we want to receive and use an argument. In this case it’s “dir” (the title of the video).

  8. GPTScript can’t directly interact with our file system, so in this case, we need to create a simple bash script for creating folders.

  9. Run a command “mkdir” (this is a OS specific commando, not the tool) to create a folder.

If we now run the same command as in the previous step, you will see something like this:

$ gptscript gpttest.gpt --script This is a test for a video where you will learn about GPTScript

Results:
INPUT:
--script This is a test for a video where you will learn about GPTScript

OUTPUT:
The folder "Learn About GPTScript: A Comprehensive Guide" has been created

Perfect! You should now see a folder in the projects root folder with the name that our script generated. Not only that, but we see that in the output, it explains what it did. And this explanation is a good confirmation that the script understood what we wanted it to do.

Creating the tool for generating thumbs

Let’s kick things up a little bit and make a tool for generating images. Or at least build a tool that uses a library with DALL-E in the backend for generating images.

At the bottom of the script file, add the following content:

---
name: thumb-generator
tools: github.com/gptscript-ai/image-generation
description: Generates a YouTube thumbnail.
args: script: The script to generate thumbnail from.

Generate a YouTube thumbnail based on the script. Only return the url of the thumbnail.

So here we define a new Tool called “thumb-generator”. We instruct it to use a tool called “github.com/gptscript-ai/image-generation”. This tool can be used for various tasks when it comes to image generations.

Next we add a short description and set ut the arguments we want. This argument comes from the “main” script.

Below there, we give the tool instructions on what we do. We want it to generate a YouTube thumbnail based on the script, and then we want it to return the url for the thumbnail for us.

Last step before we can test this, is to make some changes further up in the script:

tools: thumb-generator, mkdir, sys.write, sys.download # 1 - Changed
description: Generates a YouTube title and thumbnail based on a script.
args: script: The script to generate title and thumbnail for.

Do the following steps in acsending order:
1. Come up with an appropriate title for the video based on the script.
2. Create the `${video-title}` folder if it does not already exist. 
3. Call thumb-generator to illustrate it. # 2 - New
4. Download the illustration to a file at `${video-title}/thumb.png`. # 3 - New

---
name: mkdir
tools: sys.write
description: Creates a folder in your system
args: dir: Path to the folder to be created.

#!bash

mkdir -p "${dir}"

---
name: thumb-generator
tools: github.com/gptscript-ai/image-generation
description: Generates a YouTube thumbnail.
args: script: The script to generate thumbnail from.

Generate a YouTube thumbnail based on the script. Only return the url of the thumbnail.
  1. First, we include the new tool we created “thumb-generator”. Then we include one more tool called “sys.download”. This tool makes it possible to download the url we got in the new tool we created.

  2. Added a step to the script where we tell it to call the new tool we created.

  3. Added a step to the script for downloading the thumbnail and storing it in the folder that was created.

Okay, it’s time to try running this again and see what we get.

$ gptscript gpttest.gpt --script This is a test for a video where you will learn about GPTScript

Results:
INPUT:
--script This is a test for a video where you will learn about GPTScript

OUTPUT:
The illustration has been downloaded to the file at `Learn GPTScript: A Comprehensive Guide/thumb.png`.

This is the image I got when I ran this command:
Image of a book with the text, GPTScript
It doesn’t really look like a YouTube thumbnail, but it is a good starting point.

Improve the image generator

The image we generated is squared and I also want to do some other changes. Open up the script file again and do the following changes:

tools: thumb-generator, mkdir, sys.write, sys.download
description: Generates a YouTube title and thumbnail based on a script.
args: script: The script to generate title and thumbnail for.

Do the following steps in acsending order:
1. Come up with an appropriate title for the video based on the script.
2. Create the `${video-title}` folder if it does not already exist. 
3. Call thumb-generator to illustrate it.
4. Download the illustration to a file at `${video-title}/thumb.png`.

---
name: mkdir
tools: sys.write
description: Creates a folder in your system
args: dir: Path to the folder to be created.

#!bash

mkdir -p "${dir}"

---
name: thumb-generator
tools: github.com/gptscript-ai/image-generation
description: Generates a YouTube thumbnail.
args: script: The script to generate thumbnail from.

Do the following steps in acsending order: # New

1. Come up with a background color to represent the $script which can be used as the background color for the thumbnail. # New
2. Think of a good prompt to generate an image to represent $script. Make sure to to include the ${video-title} of the video as one sentence. 
inside a colored box somewhere in the thumbnail. Only return the URL of the illustration. The thumbnail should be 1792x1024. # New
3. Use the ${background-color} to make sure the edges of the thumbnails fades out. # New

All the changes here was made in the “thumb-generator” tool.

To make the image generator create something that looks more like a YouTube thumbnail, we need to instruct to make the image a different size (1792×1024).

We also did a few other changes like finding a suitable background color, adding the title of the video as text somewhere on the thumbnail and added a fading background color.

When I ran the command one more time, the thumbnail looked better, but it still was shaped as a square. So I changed step 2 in the “thumb-generator” to be like this:

2. Think of a good prompt to generate an image to represent $script. Make sure to to include the ${video-title} of the video as one sentence. 
inside a colored box somewhere in the thumbnail. Only return the URL of the illustration. Make sure the thumbnail is 1792x1024 

This time I was a little bit more specific when I told the script the size of the thumbnail, and here is the result:
thumb2.jpg

The result looks much better now. A little catch is the missing text, but if you run the script a few times, you will see that sometimes it’s there and sometimes it’s not. You can try changing the script a little bit to me more strict for example. But in general, adding text to images using DALL-E isn’t always the best solution.

Summary

That was it for part one. You should now have a basic understanding of what GPTScript is and what it can do. By learning these basics, you have also now built a simple script that can be used for generating images for a YouTube channel, but it can also be used for other things like blog posts and similar.

In the next part of this tutorial, we will make the script even more advanced and also learn how to implement this with a frontend.

Related Articles