OpenAI Tutorial #5 - Adding a Frontend

Net Ninja · Beginner ·🌐 Frontend Engineering ·3y ago

Key Takeaways

This video tutorial series covers the basics of leveraging AI to make small applications that generate text and images, with a focus on adding a frontend using Express, OpenAI, and JavaScript.

Full Transcript

all right then so now we've built our backend API which sends requests to open AI to get some AI generated content so it grabs that and then it sends that back as a response to the clients now I want to make a front-end web page to hit this API and do something with those responses like show them in a browser so the first thing we need to do is make a public folder for all the public files accessible to the browser including our HTML file and any other assets that we might need like a style sheet or images so let's create that first of all and in order to tell Express that this should be a public folder available to the browser we need to use a little bit of middleware so underneath where we use the Json middleware we can say app.use to use some more middleware and then this time we're going to say express.static and invoke that function to say that we want to make use of a static folder which is in our case going to be the public folder so we say to express look this is the folder public that is going to be accessible to the browser okay so now we'll be able to access any of the files inside this folder at the root of the application so just localhost Port 4000 forward slash and then the name of whatever file we want from inside here all right cool so now we've set that up let's add some HTML and CSS to this public folder okay so I'm going to add a few things to this folder first of all an index.html file and then I'm going to add a styles.css file I'm also going to add in an image I'm just going to copy this across you can get this from my repo it's just the YouTube logo and then after that we also want an index .js file because we will be using a bit of JavaScript to send those post requests to the back end all right so inside index.html I'm going to do Doc and right here we'll say YouTube meta generator and then we also in the head want to link to the style sheet so link and the href is going to be forward slash Styles dot CSS at the bottom of the body we'll do a script tag and the source of that is going to be equal to forward slash index .js all right so oops that needs to go inside quotations like so okay then so now we have those four files let's flesh out the HTML and the CSS now then instead of me writing out a whole bunch of HTML which is what you're not here to learn I'm just copying this from my repo I will quickly walk through it though this is just to save a little bit of time so you don't have to watch me type all this out so we have a nav at the top with the image of that YouTube logo inside it then we have an H1 it says YouTube video data generator then we have a container which surrounds everything else then we have a form at the top for the metadata so we give it a class of metaform title and then a label for the input span to say video title and then the input itself and then a button to submit the form that says generate metadata so that's the first form then down here we have two divs one for the description and one for the tags this is when we get a response back from open Ai and then therefore back from our API on the back end when we get that we're going to Output the description we get back right here and the tags that we get right here okay so that's the first form and that's going to be sending a post request eventually to forward slash open AI forward slash meta to get the metadata now the second form is for the image so we give that a class of image form a title then a label span for the label which is describe the image the input itself where we describe the image and then a button to submit the form to generate the thumbnail so again that's the form for sending a post request this time to forward slash open AI forward slash image I think it was and then when we get a response back from our Express app with that image URL we're going to update this image URL right here this image source now at the minute this is just a placeholder image which is a gray block but that will get dynamically updated via our JavaScript when we get a response so that's pretty much it for the HTML and now we can go to the CSS again I'm just going to copy all of this from my repo over here so if you're free to grab that I'm going to copy and paste right here so we have the ball uh the body with a few basic Styles then P tags again a few basic Styles the container with a Max width to hold everything in the middle then we have the nav at the top just with some basic Styles display Flex so the image and the texting next to each other um the image in and out if we give a Max width of 70 pixels the forms themselves the background of red and the color of the text is white inside so it's the line Center a bit of padding strip out the margin at the bottom every element inside the form that's what this asterisk is we say display block and the margin is 10 pixels top and bottom also left and right now where we output the description eventually the tags and the thumbnail we give that a background of white a bit of padding bit of margin and a border which is dashed as well the thumbnail we say text align Center so the image will sit in the center for the inputs background of Gray strip out the Border a bit of padding border radius width and margin uh the buttons again some basic styles to color it give it a border radius or the bottom or the left we're making it into like a chunky button that's what these things do right here but a padding a cursor display block position relative top zero uh when it's active so when we press it the Border bottom width is two pixels and the top is three pixels so this is a little effect we're applying right here using the top and the Border width and stuff to make it look like we're pressing a button into the page we'll see that in a second but again I mean this stuff is not really important the HTML and the CSS is secondary I just wanted to build a front end which didn't look absolutely tripe so um yeah don't worry about the CSS then for the image inside the thumbnail div we say Max width is 100 alright so now we want to preview this in a browser open this up and cancel out of this process I've just realized I've completely missed about this but never mind anyway node app again so now we can view this in a browser at Port 4000. all right so this is what it looks like in a browser not too shabby it doesn't look amazing but not tripe so we have a form at the top right here this is for the video metadata so we're going to type in our description or rather our video title right here then we generate the metadata and it's going to Output the response here and here and then down here we describe the image generated thumbnail and output it right here and this is the placeholder image to begin with so that's going to get updated so this is how it looks now we need to implement the functionality because at the minute if we do this it's just going to refresh the page we don't have any JavaScript sending a post request so let's do that now all right then so remember inside the HTML we link to the index.js file and that's where we're going to be sending the post requests and we're going to do that when these forms right here have been submitted and then we're going to Output the responses in these different divs right here so we need basically to grab these different elements from the Dom the forms and where we're outputting them and then we need to handle the submission with these forms right here and then we need to inject content into these divs right now to speed things up I've already grabbed the forms using query selector so the metaform and the image form using these classes and then the output elements where we're going to Output the data we get back we have as well description tags and thumbnails and that's the P tag inside description p-tag inside tags and then the image tag inside the thumbnail div all right so I've also added event listeners to both of the forms the metaform and the image form right here so when we submit that we find an asynchronous function and pass in the event object now it needs to be asynchronous because we're sending asynchronous request we're going to be using the await keyword and inside the functions to begin with we say e dot prevent default and that prevents the default action of the form being submitted which is to refresh the page so we're not going to be doing that so we'll start with this one and we want to send a post request to our API and send along the value that a user has typed into this input field right here okay so let's do that first okay so we can say const response is equal to a weight then we're going to use the fetch API and we're going to fetch from forward slash open API so this is going to be localhost Port 4000 forward slash open API forward slash I think it was meta wasn't it for the metadata all right so we're sending a post request meaning we have to say inside the second argument which is an object that the method is going to be post like so okay so after that we want some headers to say the content type so we say content hyphen type and that is going to be application forward slash Json like so and then after the headers we can say the body so the data that we're sending along with this and we need to send a Json string so we say json.stringify and then we pass in the object which has the title property now the title is going to be from The Meta form so that's this element that we grabbed right here and then we can say dot and then the name of the input so if we go back to the HTML we see the name is title so we can say dot title so dot title and then get the value of that by saying dot value so we're sending in the value of that input form as the title property right here which is what is expected if we go to the open AI controller up here we expect a title property on the body and that's what we're sending right here a title property Okay cool so that's the um the fetch request the post request so now down here we can get the response so const data is equal to a weight response dot Json and involt that to pass it into an object that we can work with we can log that data to the console if you want as well we can see it console.log data but also I want to update the description and also the tags div so we have those two Fields right here that P tag and this P tag sure I'm going to say description dot text contents is equal to the data we get back which is this thing right here then it's dot description because remember we have the description property on it if we go to the open AI controller go down here we have a description property and a tags property so we want the description property and then on that there is a Content property so we're getting the content of that response okay so the other thing we need to do is set the tags so tags.txt contents is equal to data dot tags this time Dot content all right cool so we've done that now we want to do the same thing for the image down here so let me copy all this and paste it in because it's going to be pretty similar so then this time it's going to be two forward slash image and then it's still going to be a post request Json and this time it's the image form and then dot prompt that is the name of I think this thing right here yes it is prompt and then dot value and that's also going to be a prompt property that we expect and then what else do we need to do so we have the response we have the data we console log the data okay so all we need to do is update the image so we can say thumbnail which remember is this thing right here so that's the image tag then we can set the attribute and we want to set the source attribute and it's going to be the data and then we send back I think a URL property on it yep we do so data dot URL so we're updating the source with the URL that we get back all right cool so fingers crossed this should all work all right then so let's give this a whirl and I'm going to say right here python crash course I'm gonna send this request and while that's working I'm going to come down here and describe the image and I will say python in a space suit floating through space all right generate the thumbnail let's scroll up okay so now we have the description back which is cool and then down here we have the different tags as well which is good so let's go down here and wait for the image now oh it's done so there we go there's a python I think in a spacesuit floating through space awesome so there we have it we have now completed this little money project where we're interacting with openai to generate some content both in text format and in image format so my friends hopefully now you've got a better grasp of how open AI works and how we can Leverage The Power of AI into our own applications let me know what you think about it and if you'd want to see any more kind of AI related content in the future some of my friends I really really hope you enjoyed this series and you learned something along the way if you did please please please don't forget to share subscribe and like that really means a lot and if you want to access all of my YouTube courses without adverts also get access to premium courses and Early Access courses as well you can do at netninja.dev you can sign up for net Ninja Pro which is just nine dollars a month and also half price for the first month with this promo code right here and for that like I said you get access to every course without adverts without YouTube adverts you also get access to exclusive course it is not found anywhere else you get access to my premium courses on udemy and also Early Access to all of my YouTube courses as well so the link to this page to sign up is going to be down below again I really hope you enjoyed this series and I'm going to see you in the very next one foreign [Music]

Original Description

In this OpenAI tutorial series you'll learn the basics of how to leverage AI to make small application that generates text and images. 🚀🥷🏼Get early access to this entire course now on Net Ninja Pro: https://netninja.dev/p/openai-tutorial-the-basics 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/openAI-basics 💻🥷🏼 Modern JavaScript Tutorial: On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 💻🥷🏼 Node.js Crash Course: On Net Ninja Pro - https://netninja.dev/p/node-js-crash-course On YouTube - https://www.youtube.com/watch?v=zb3Qk8SG5Ms&list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU 🔗🥷🏼 OpenAI - https://openai.com/ 🔗🥷🏼 VS Code - https://code.visualstudio.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video tutorial series teaches you how to add a frontend to an AI application using Express, OpenAI, and JavaScript, covering topics such as form handling, post requests, and AI-powered image generation. By following this tutorial, you'll learn how to craft effective prompts, handle form submissions, and integrate frontend and backend components.

Key Takeaways
  1. Create a public folder for static files
  2. Use Express.static middleware to make the public folder accessible
  3. Add HTML, CSS, and JavaScript files to the public folder
  4. Create forms for metadata and image generation
  5. Send post requests to the backend API for metadata and image generation
  6. Grab forms and output elements using query selector
  7. Add event listeners to forms to handle submission
  8. Send post request to API using fetch API
  9. Update description and tags divs with response data
  10. Set tags to match data.tags
💡 To build an effective AI application, it's essential to understand how to craft effective prompts, handle form submissions, and integrate frontend and backend components.

Related Reads

Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →