Welcome back, everyone, to this part of the project we're working with the dolly to prompt.
So it's time to explore the dolly to API.
I should point out it's technically called the Image API in the same way that dealing with GPT and text
Da Vinci is technically the completion API.
Now the image API is a lot simpler than the text completion app because it has less parameters.
So the image API simply takes in a text prompt and returns an image that will attempt its best to match
up what is in the text prompt.
So you ask for a dog and a cat in a string.
Maybe you want to be a little more detailed like high definition photo of a dog and a cat and it will
send you back an image that hopefully looks like a photo of a dog and a cat.
Make sure to review our crash course lecture on how Dolly works to get the gist of it.
Something I'm going to point out here is we're actually not going to have a presentation on guides for
the best string image prompt combo.
So there's already tons of existing guides online.
Just search Dolly Prompt guide and you will see tons of free ebooks.
Some of them will link here in the resources for you to check out.
But again, there's already so many guides online and there's many guides for specific styles.
So if you are into anime or maybe you're into oil painting and you want to generate photos that look
like that, you can check out those specific Dolly Guide prompt books.
Now let's talk about the more technical parameters when you're actually calling the image API.
There are image sizes.
You can request the following size images.
At this time, it's 256 by two, five, six pixels.
You can do five, 12 by 512 or the largest available is 1024 by 1024.
Take a look at the pricing page for the latest pricing information.
As you may have expected, the larger the image, the more it costs.
And as of this filming, I believe every 1024 by 1024 image costs about $0.02.
Now, you don't just have to get one image back.
There is actually an end parameter, just like there was in the completion API.
And you can actually ask for ten images back if you've used only the Dolly guy that is the graphical
user interface online, you would have noticed it gives you only four images.
Back with the API, you can actually call for ten images.
Keep in mind, the more images you request, the longer the response time will take.
And if there is a time out, you'll have to try again.
There are sometimes bandwidth issues, but Openai does get better and better with that every day, especially
if there new funding from Microsoft.
Now, here's something that's interesting that I don't think sometimes people realize you can do with
the API instead of a text prompt.
There's actually a parameter called image where you can open up an image on your computer.
So you would just say open, that's a base python command pass in the file and then say read it as bytes
and you can pass in the image and open eyes.
Image API via Dolly will give you an output or return image that attempts to be a variation.
So essentially you can see here we have a cat and a dog and we and it's a photo of them and we want
some sort of variation on this.
If you look closely, it's definitely not the same cat and definitely not the same dog, but you can
see that it's basically the same kind of idea or flavor of the image.
So in case you ever find yourself needing that sort of particular service that's available as well.
All right.
Let's head over to the Notebook to get started.
All right.
Here we are at the Jupyter Notebook.
Remember, previously we were already able to extract the title, so we just got ham, turkey, egg,
breakfast sandwich.
Let's see what happens when we just pass that text directly into Dolly that may actually be descriptive
enough to get an image that kind of makes sense.
So we're going to say response is equal to open i dot image and something to be careful of.
You'll notice I'm using response here.
Technically speaking, if we come back, that is also what I used here.
So be careful.
Maybe you want to say like text response and image response.
I'm being a little sloppier right now just because we're in a notebook and learning, but I just wanted
to point that out.
Try not to use the same variable for both the completion API and the image API.
In our case, it won't really matter because I've already extracted the resulting text, so I'm going
to say now open I image create and we are going to pass in the prompt.
I will say prompt is equal to extract title from the resulting text, which remember is ham, turkey
and egg breakfast sandwich.
I'm going to say comma here.
I'm going to specify that I just want one image and is equal to one comma.
And then my last parameter or argument, I'm going to ask for the larger size.
So we'll say 1024 x 1024, So 1024 by ten, 24 pixels, I'm going to go ahead and run this and then
let's check out the response.
What's kind of interesting is the response actually links you to a URL of your photo, which I also
think sometimes confuses students.
They actually expect the direct file download, but that's not actually what happens.
Dolly is essentially now hosting this image and it's linked to your account, so you can go ahead and
copy.
The link pasted there.
Open up a new tab.
You should then be able to go to that image and I can see here that ham turkey egg sandwich.
And honestly, it's looking pretty good.
This is much better than I thought it was going to be.
Now, can we do better?
Can we make this image actually better?
And also, how do I download this image?
Well, you could go to that link and then right click and do save image as, but you can actually do
the python.
So let's have Python.
Take care of that for us.
I'm going to create a simple function that actually given an image URL, we'll download it and save
it as an image.
So we're going to say save image and let's get the image URL from the response.
So basically you're going to call data.
And that gives you this list.
You'll get the first item in that list.
And then from there you are going to grab the URL key.
And now you have the image URL as a string.
So let's call this image underscore URL.
And then what I'm going to do here is simply pass in.
An image URL and the file name I want.
For that image.
And essentially what I'm going to do is I'm going to go from the URL to some local PNG file and how
do I do this?
It's actually not too bad.
You will need to import requests though, because we have to connect to the internet.
So import requests and then you're going to say my image result is equal to requests.
You're going to request the information that is in that URL.
So you're going to get that.
And you can say stream is equal to true here.
And then we are going to say if the image results.
And a status code.
Of 200.
So this is equal to those HTML codes, like a 404 HTML code 200 means that it actually found the image
there.
I'm going to say with open.
File name.
And since it's an image file, we write to bytes.
Make sure I put a comma there.
So those are just your typical arguments.
And I'm going to say that as f then I actually to do this, let's go ahead and use some utilities.
So we're going to say import.
These utilities that are useful for us because that actually allows us to, in just one line, copy
that file object from the URL and save it as the file.
So we're going to say image results dot raw because that's the actual image.
And they're going to write that to the file.
Otherwise, along with this statement, if we did get a 404, I'm going to say print.
Error loading image.
The reason we ended up writing the full statement here is because the Dolly API.
Sometimes it gets overloaded, especially during the daytime and East coast West coast, so there's
sometimes an error that makes no image appear.
So I just want to leave that for you.
Except the they're really getting much faster and much better, almost day by day or week by week.
So by the time you're watching this, it may never be down.
Who knows?
But we're just going to return the image result status code, which if it's a 200, then we know everything
should have worked.
Okay, so that's our simple function.
To save the image, let's actually make sure this works.
So I'm going to say save image.
I'm going to pass in the image URL and the file name I want can be example download dot PNG save image
and we got exit code 200, which means it was able to successfully go to this API with the request.
So that is what this line did.
Then it checks.
Okay, is it a 200 code?
So this isn't a four or four page.
It's good there.
Then it opens up the file name, essentially creating that file.
So example download PNG and then from the image result object.
This actually is a request object.
So it has things like status codes, but the raw information, that's the actual image.
So then we take that raw image and save it and copy that file object into this file name, location,
and then we're done.
If there was an error along the way, it would have said print error loading image.
So that is basically it.
You can see with just very little code.
Really.
I is doing the heavy lifting here.
I was able to create a recipe and also generate image from it.
That's really just the two key components you need in order to kind of create an automatic recipe.
Blog posts generator.
We will show you in the next lecture a walkthrough of a Python script that does kind of bring it all
together.
But in the next project, we're actually going to show you how you can automatically push a blog post
to online for free via GitHub pages.
The last thing I want to touch on is prompt engineering for Dolly.
I did say that you should Google search this instead of depending on us because there's so many different
guides out there.
But we did find a couple of guides on food photography.
As you know, that's kind of a special field and making food look really good and delicious.
Although I will admit this one's already pretty darn good, but let's try a couple of additions to our
prompt.
So we have found that adding things with commas basically styles is usually a good idea for prompt creation.
So I'm going to create a separate function called Dolly to prompt, which is kind of a nicer version
of the prompt we have here of ham, turkey, egg breakfast sandwich.
And we are going to call this let's call this our recipe title.
So if you take a look, recipe title is just a string ham turkey egg sandwich.
So I'm going to grab that recipe title.
And then I'm going to return a prompt equal to f string literal where I just say.
The recipe title.
Comma.
And I'm going to add a couple of more things that we found worked really well.
So something that works really well for food is the term professional food.
Photography.
And then because it's photography, a lot of times images will say the millimeter of lens they used.
So adding 50 millimetres makes it look good, as well as adding the phrase studio light, not lightning,
but lighting.
And then I'm going to return this prompt.
So what does this actually mean?
It means that if I grab the dolly to prompt and pass in my recipe title, I get back this as my full
text prompt instead of ham, turkey, egg breakfast sandwich.
So let's actually try passing in this as our prompt and seeing how much better the image is.
And we'll also then grab that new image URL.
And there's no guarantee that this will work better.
But given our results, we've found that it typically is a lot nicer.
So I'm going to copy that.
And let's go ahead and paste it in.
Okay.
You can definitely tell what's going on here if this is better or worse.
I'm actually now I'm not so sure this one at least looks more real.
This one, maybe it looks almost a little too fake.
Hopefully you enjoyed that project and mainly enjoyed understanding how the image completion API works
in the next lecture.
What we're going to do is give you a walkthrough of a more kind of built out, completed code that allows
a user to directly input a list of items in their fridge.
Keep in mind, under the hood, it's really just a couple of functions we created, spliced in and glued
together.
So let's check out that code in the next lecture.