From the course: OpenAI API for Python Developers
Generate creative art with DALL·E
- [Instructor] Now let's get to work. And we want to create an image gallery driven by AI. Here we're going to allow to send a text input whenever we click on the send button. We're going to see how the application looks like. My app is already up and running under here, this URL. We're going to go check this one out and see how this looks. And here we have our image generation gallery. And so in order to generate new original image, we're going to find the documentation here on OpenAI. And you're going to find here under Generations, this code snippets, we're going to use the same actually. We're going to copy from line four to all the way down to 12. Let's go back to our project. And what we want is to call this function generate_image that we're going to call from handlers. That's going to be right here. And so you're going to paste this And after you get a response from AI, you want to return the URL, so you're going to access the data, then return the link to the image, the AI generated image. And after that we want to display it in our gallery. This is already added to the scope of my module right here, so I'm going to do here image, then call generate_image, and pass the user input, so whatever we type in the text input, we're going to use that as a text prompt. For now, I'm just going to print, so, whatever URL is generated by the AI, I'm just going to print it and we're going to see the results. We're going to test it right away. Let's go here. For now, it's just going to be a test. I'm just going to put cat. You see that this is running. Let's go back to the project and we're going to wait for the response. Here we go, so here we've got this URL, and if we copy it, let's see how the result of this AI generated image looks like. Here we go, so this is a nice-looking cat, which actually is completely fake, which is the result of AI generation. Now let's go back, because what I'd like is to actually display this new image here in my gallery. For that I'm going to do a few adjustments. This is a feature provided by Streamlit, which is st.image. So we're going to pass image, then you can specify here parameters, settings. Here we go. And also what we could do is to add, if you'd like, success, to say that your image was downloaded successfully. Actually, this is the next step that we're going to do. I'm a bit ahead. What I'm going to do is to put that right below, because that's going to be the next step. We want to also be able to download and have access to the images physically. What I'm going to do also is to add another feature, which is with a spinner, I'm going to show the user that this is working so that they can wait while the image is being generated so you don't just look at a blank screen. I'm going to put that below a spinner, and the next step will be to download this file. You'll see that we have added also this function, which is downloadFile and get_files, which will be for the next part of the project. So let's go check out downloadFile. Basically what it's going to do, so this is just Python code here, we're going to then get the URL to download from this URL and save it on a file. And then within a directory, which will be media. And I have specified here the directory, which is media. We're going to use it, so that's going to be another extra step. After we have successfully generated an image, we're going to take the output from the generative model and then run. I'm going to do saved_image like this. And then I'm going to call, downloadFile, and pass the user input. And the image, actually, this is going to be first, and this is only after we display the image that we're going to run this function, which is to download the file. And after that we're going to read, image successfully downloaded, saved to the media folder. Okay, so it got it. Okay, let's try that again, but this time I want to be able to generate based on my text inputs, because so far we've been using this example, which is a white Siamese cat. Let's replace this with my user inputs and let's try that again. And this time I'm going to try something like, I want to generate an image with flowers. You see that we've got here the spinner, so we know that this is working. Here we go, so we've got our nice looking generated image, all good. So it looks like it was also successfully saved in the media folder, and we can actually find it here. Excellent, so the next step will be to download and save as many images as we want. And in order to display them in our gallery. That's going to be right here.
Contents
-
-
-
-
-
-
Introducing the Moderation API1m 52s
-
Add a moderation layer4m 56s
-
Text to image: Introducing the DALL·E model2m 24s
-
Generate creative art with DALL·E4m 46s
-
Create an image gallery with DALL·E4m 35s
-
Whisper Audio API: Speech-to-text2m 40s
-
Whisper Audio API: Transcribe audio samples5m 5s
-
Whisper Audio API: Translate audio sample4m 46s
-
-
-
-