Spring AI Image Generator | How to Generate Image Using Spring AI in Spring Boot | Open AI| EnggAdda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone Ravi this side welcome to engineering Adra today in this video we are going to discuss about the spring AI image generator so what is a spring AI image generator we are going to discuss that and how we can use it in our spring boot application that is what also we are going to practically see so if you're new to the channel let me try to tell you that in the previous video I have already discussed about the spring AI introduction in springbot where we have discussed about the chatboard how we can develop a chatboard in our springbot application we also have the videos on my channel where we have discussed about the spring AI prompt API and spring AI output parser in this video we are going to see about the image generator API of spring Ai and how we can use it in our spring boot application that is what we are going to see here so before going to do the actual development what I will do I will brief you about the springi image generator I will I will walk you through the springi website and we'll also see uh the use cases and the classes and the interfaces in the image generator of spring Ai and then we'll try to practically create the springbot application and there we'll try to create our own custom image generator board or API you can say so this is the today's topic let get it started for that let me try to come out of it and go to intellig or let's say go to the is bring I website and then discuss about the image generator first so so here we are on the browser let me try to search for the spring Ai and go to Spring AI if you'll see here spring AI apis you have to go there and we already have discussed about the chat completion API and we also discussed about the output parser and the prompt API we are going to discuss about the image generator API and here you can see that image generation API the spring image generation API is design designed to be a simple and portable interface for in interacting with the various AI models is specialized in image generation so this is a interface which help you in interacting with the various AI models okay and those AI models are specialized in the image generation allowing developer to switch between the different image related models with minimal code changes this design align with the spring philosophy of modularity and interchangeability insuring developers can quickly adapt their application to different AI capabilities related to the image processing so this is the main purpose of image generation API introduced in his PRI and here you can see uh these are the API overviews so you can see that first of all we have to autoare or you can say the image client is the main uh interface that you can use to call the uh images okay so you can see that inside this uh image client interface it is a functional interface and we have only single uh like abstract method here which is call and we have to provide the image prompt there as a request and then get the desired image if you go to this image prompt you can see how it is looking like it is having the two variables the messages and the image model options and these are the methods that it has if you'll go inside the image messages this is the thing that it contains it contains the text weight and in the image option you can see we have the methods like get and get models width height and response format so these are the classes and interfaces related to the image generation API you can uh walk walk through these things and then get more information on it now if we go down you can see that the available implementation for the image API are these two one is the open AI image generation and the second one is the stability I image generation so we are going to use the openi image generation here so go to this and here you will find the uh information how you can uh configure your springbot application for this openi image generation integration okay so you can see that springi support chat G p d e the image generation model from openi and this is the prerequisite so what is the prerequisite you need to create a API key with the open a to access chat GPD models in the previous video we already have discussed this how we can do that but let me try to tell you how you can create the uh key and what is the things that you need to do in order to create the open AI keys so you can see this you have to go to this site and you you make sure that you have created One account on this uh open AI okay and you have to log to that account so just let me try to log in there so I already have created One account I'm just logging there and uh let me try to see how it is working so you can see that okay okay let me try to uh sign up with the okay first of all let me try to close it and just open the open Ai and let me try to go to the homepage and here let me try to continue with the Google and try to the email here and then just go on next and you can see that I am logged in inside the openi so make sure that you have created One open account and you are able to log in into that account once you will log in you will find this homepage which is the overview page and here inside uh this homepage on the left side you'll find this API keys so you have to go inside that API keys and you can see that on 12 sery I have created one API key how you can create it just you have to select here and then you have to provide the key name let's say testore key and then just create the key it will give you the key you have to copy it somewhere I already have copied this key and I'm going to use this one so this is how you can create the open a key okay now let me try to go back to the open AI sorry spring AI P here so you can see that in order like this is the prerequisite you have to create open Ice uh account and then you have to generate the open a API key now what is the next step the next step is auto configuration you have to include this dependency in order to automatically configure your spring AI open AI spring boot starter in your spring boot application so let me try to go to the intell and try to create one spring boot application first and then first of all we are going to set our uh spring boot open a API key and then we'll include this dependency in our pom.xml and then we'll start developing our image generator okay so first of all go to the intellig and here go to the file new go to the create sorry project and then select this string initializer I using the intell ultimate version so what I can do is I directly can create here uh this spring boot application from intellig itself but if you're not using intelligent ultimate version then make sure you visit this is start. spring.io Page and the same page will open up there and the same thing you have to do what I'm going to show you here so you have to provide the project name so let's say it is uh spring AI image generator okay and uh select the MAV here and then go next select the lbook and and just web this two dependency are fine as of now just create it and wait for few second and try to open it in a new tab so once you have uh opened your project in the intellig or any ID that you are comfortable in let's get started the development here so you can see that it is resolving the dependency so it will take few second or few minute of your time to resolve the dependency that you have added and do the indexing and then you can do your development here so just wait and what you have to do is the first thing that we are going to do is uh we are going to set the key here so you can see that this is uh the key and the value you have to provide uh the key that you have generated here from the openi so copy this key or you can say the property and go to intell go to the source go to man and the sources go to the application. properties file past it here and then just copy this key that you have generated and paste it there okay now you're good now what we have to do the next step is you have to include the spring AI open API is start a dependency here so just go to this and then copy this and then go to your application go to the pom.xml file under the dependency section you have to put this dependency now load your Maven and wait for a few second so what it is saying it is unknown J let me try to do cannot find this artifact just for update snapshot we'll try to see if it is working or not go to the MAV try to reload the MAV again and then see okay it is not working okay okay white is not resolving it let me try to to go to the m and go here let me try to do one clean install so it is staying that dependency version for the spring AI open AI spring boot starter is missing so let me try to go to the MAV and try to get the dependency for this spring AI open a open AI spring Bo starter so again what we we can do is we can load the MAV we'll get this uh error and go to this repository so where it is or what you can do is you can search for this uh spring AI open AI M dependency okay what it is giving us let me open this and then see if you're getting something so there is basically the version mismatch is there and uh I think we have to downgrade our springbot uh version that is why it is giving us the error okay so here you can see we're getting some version so just copy this version and try to see if it works so copy this and let me open the notepad here okay it is not being copied copy this rest it here let me try to use this version and go to our springbot application minimize this and put the version downside of the artifact inside the dependency uh which is spring AI open AI spring stter dependency and then load the maven again and then see if it works so I think it is working uh let me try to minimize this and then see so you can see that it is uh downloaded and you can see we are in sync so this like you have to provide the version if you are using the latest version of a spring boot so you have to give the version of your uh spring AI open AI springbot starter okay so that it will be in sync so we are good now now what we have to do is uh go to the spring AI again and uh where we are so here you can see that we have added the started dependency for the auto configuration of spring AI open AI now if you go down you'll find this image generation properties so these are the others properties that you can set for the image generation like you can set the base URL and you have to set the API key that we already have set and go down these are the others properties that you can use now if you'll go down you will find this piece of code so this is what uh will help us in generating the image so you have to provide the prompt for the image generation and then you can set the quality and this uh range height and width everything you can set and then create one open a i image option Builder and then get the required image according to the prompt that you have set and then find the result for that so let me try to copy this piece of code and go to our intellig or our application and try to go to the Java let me try to create one package here let's say it is uh controller and okay so we have created this package controller let me try to create one class here let's say image generator controller okay now we have to annotate this with at the rate rest controller so that it will work like a controller here so we have annotated it now let me try to create one get mapping here and let's say this is like image and as of now just select this public string get image and uh just put it like this and paste the code that you have copied so you can see that we have pasted the code now let me Ty to import this image response and import this so you can see this this is the openi image client you have to autoare this so I will do that let me try to import the other stuff like image prompt and imagei open AI image option so we are good now now we have to Auto add the open AI image client here so how you can do that just do autoare and create the instance of uh image client and just let's say it is open AI image client and this is what we are using downside here you can see just paste it here so we are good now here we are returning the string and uh let me try to return the response dot get result Dot get output okay so if we'll go to the output let me try to see what it is running it is returning the image here so we'll return the image out of our API or you can say the method that we have created okay so this is what we are returning here so let me try to brief you about what we are doing here is we have created one API and inside that API what we are doing is we are creating one image response okay how we are creating it with the help of open AI image client we are calling the call method of open AI image client and then we are passing this prompt there and then we are getting the result out of the prompt that we are passing and then in the response we are restoring those result and then we are getting the result out of the response and getting the output and then returning it out this is what we are doing here so if we'll go inside this uh call method of the image client you'll find that it it is taking the image prompt here as a request and that is what we are passing there okay so inside this call method we are passing the image prompt here inside the image prompt you can see that uh you have to pass this prompt based upon this prompt it will try to get your result get your emage okay so you can see that here we are passing a light cream colored mini golden doodle so this is the search or this is the prom prompt that we are searching and this is the like this is the prompt that we are passing and for this prompt only we are going to get the result as a image okay so of like inside this image promt we have to pass this uh open AI image option as well and you can set this quality height width and N which is a frame I think so everything you can set here and then build it out and then return it so this is what we are doing here now let me try to run this application and try to hit our image API so just copy this and wait for a few second so here you can see that our application is up on the port number 8080 now go to the postman and the API that we have created is image and our application is up on the port number 80 80 so this will be our API now let me try to hit it and then see the result so it is processing and will take few second and then it will give us the result so you can see that in the result we are getting the URL and the base 64 Json and it Valu is null but in the URL we are getting some URL here okay so you can see that if you'll go to this imaage class which we are returning here it has two properties one is the URL and the second word is b64 Json so we are getting the URL now let me try to go to that URL and try to hit it so if you'll open this up or if you'll click on it in the postman it will open a new tab and there you can hit this URL so you can send it out and then see the result so it is waiting for a few second like you can wait for a few second it is taking some time okay let me try to hit it again and here we are getting the image here just wait you can see that we are getting the image of a doodle so this is what AI has generated for you according to the prom that you have provided so just copy this and go there to uh any of the browser let's say we are going to open it here just uh like this is the URL that we have generated I'm just pasting it out in the browser and getting the results so this is how our image is looking like so based upon your prompt that you have uh set here you will get the result now let me try to tell you one more thing so let's say here you can see that we are hardcoding the prompt here okay but if you want to uh not hardcode this prompt prompt in your code what you can do is you can pass this prompt as a path variable or as a request parameter in the URL itself so you can do those things this is the good practice to do so let me try to tell you how you can do those things so you can remove the prompt from here and uh there's a two way either you can set your prompt as a path variable if you're going to set it as a path variable you have to provide it in uh the URL itself and the other way is you using the request parameter you can uh send your image prompt so both of the way you can use and let me try to tell you both of them one by one so first of all we are going to set uh this uh image prompt as a path variable so you have to use the path variable and let's say it is of a string type and let's say it is uh image prompt okay so you have set it as a path variable so in that case you have to pass it in the URL okay so just copy this and paste it here okay so we are good in the URL we are passing uh the image prompt and then that is coming as a path variable and then you have to pass this here in the image prompt and the place of the hardcoded value okay now we are good let me try to run this application and try to hit this API again so now the API will be changed along with the image you have to provide the image prompt as well in order to get the result so just go to the postman and try to uh provide the image prompt here so let's say what you have to do you have to provide the image prompt here so let's say you want to get the image of uh something some some animal like dog so let's say give me an image of dog something like that and then hit the UR and just wait for a few second it will give you the result so you can see that we are getting one URL here just uh what you can do let me try to copy this URL and and go to the browser and then open it up and here we are getting the image of dog okay so this is the other way like the best way to uh develop the API is uh not hardcode any value inside the API okay so you can pass the image prompt either as a path variable or as a request parameter so we have seen that how we can pass it as a path variable now let me try to tell you how you can pass it as a request parameter so the same thing you have to do now let me try to remove this image prompt from the API and we are passing this as a request parameter so when we are passing this as a request parameter either you can set some default value here and you can also make this as a mandatory or non-mandatory field we're not going to do those things I already have covered a lot of uh like I already have covered this concept in lot of other videos so I'm not going to discuss here so we are just going to use a request parameter here and we are passing this as a request parameter like we are going to use this image prompt as a request parameter here and we passing this here in the image prompt now let me try to run this application and try to hit the API again so if you'll go to the postman here you have to remove this additional path variable and you have to give your image prompt as a request parameter so you can see that there is option of if if you'll go inside the params you will find this query params so inside the query params you have to provide your prompt here you can see that so this is the uh variable that we have set as a request parameter you have to keep this as a key and then in the value you have to provide your prompt here and this will come like this okay so just send it and then wait for a few second and here we're getting the result just copy this URL go there and then try to hit this and here you can see that we are getting the image according to the prompt that we have provided so this is how you can uh use the image generator API provided by the spring AI in your springbot application okay so this is about the video guys if you are new to the channel you can check all others video on the spring AI like we have developed the chat controller or you can say the chat board we have also seen how we can uh transform our output coming from the chat boards which are not the structured output in a structured or you can see in the Java formats using the output parsel and we also have discussed about The Prompt API and here in this video we discuss about the image generator so these are the videos I already have covered and if you're new to the Channel Please Subscribe the channel and please support the channel by sharing to the friend in need thanks
Info
Channel: EnggAdda
Views: 1,505
Rating: undefined out of 5
Keywords: Spring Boot Development, Rest API Development, MySQL, Spring Boot, docker, dockerize, docker image, what is docker, docker definition, dockerhub, docker in spring boot, docker images, how to create docker image, docker basic, beginner to docker, spring boot and docker, docker hub, containers, docker containers, deamon, docker deamon, spring ai, open ai, image generator, spring ai image generator, image generator api, chat gpt, image generator api in spring boot
Id: XzJLXXkiz4w
Channel Id: undefined
Length: 28min 44sec (1724 seconds)
Published: Thu Mar 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.