All right.
We're going to create XYZ prompt grids with flux and fallai.
So this is what a prompt grid looks like.
Typically what you're going to do is try a few different variables or parameters on one side, and on
the other side, you combine them together to see how that combination affects the final image output.
So in this case on the left hand side here we have inference steps.
So that's how much compute we put towards the image.
And you can see at the top here with only six steps it doesn't make a very good image.
It's fuzzy and messed up.
But then down at 50 steps here it's very crisp and clean but doesn't actually look that good in my opinion.
Like I prefer the softer edge here, like around 28 steps.
And then this is guidance scale.
So this is how much the prompt adheres to or how much the image adheres to the prompt.
And you can see that there are some kind of sweet spots here.
I really like 28 steps and 3.5 guidance scale.
And it really gives you a good feel for what you want out of an image and if you can make all those
combinations together, then you can pick where on the map you want to be.
That's what we're trying to do today.
Let's go through and run it from the beginning so you can understand how this works.
So I'm just going to run this to create the file client.
And that's what we're going to use to to run this.
We also want to get us numpy and matplotlib.
This is going to help us do some of the plotting of the images.
And then we also need here the environment.
So if you don't have a file key in your env file locally you just go to file.
I get the keys, create an account, make sure that you've got money in there, and then load the API
key here.
One other thing in terms of setup we need to have here is nest async here.
That's because we're going to run this asynchronously.
And that's just needed for Jupyter Notebook specifically so that it doesn't get confused and stuck in
the loop.
All right.
So that's all the setup we need.
Now let's actually start creating it.
We need to get async here and file client.
Those are the two things we need right now.
And then we're going to create a function.
So we're going to say async def generate images.
And it's going to take the model and then the prompt.
And then that the model here is just a string.
The prompt is a string as well.
All right then we want to set some guidance scales.
You could actually pass these step pass these through if you want.
Here I'm just going to copy the values that I used.
And you can set whatever you want in here.
You can set more of them.
But obviously that's just going to run for longer.
And then we want to create the handlers.
So let me just get rid of this.
So I'm going to say handlers equals await.
Listen here.
Gather.
So this is going to create all of the different API requests.
And then I'm going to say file client submit async and then submit the model.
Submit the prompt.
And then we've got the guidance scale and the number of inference steps.
And then we want to set a seed just so it's reproducible.
So when we run this again it's going to it's going to go again right.
Yeah.
So we've got it's going to go through and it's going to loop through every guidance scale.
And then it's going to also loop through every inference step and kind of multiply them together this
way.
So hopefully that makes sense to you.
We're basically just creating all of the different requests.
And then with the async API I would just want to now gather the actual results.
So that's result async here.
And we're just passing in the handler request ID and that's for handler and handlers.
So I got the handlers here that's creating all the requests.
And then we want to wait for the results to come back.
And then we're going to pass through results guidance scales and inference steps.
All right.
So let's see what this looks like.
If we pass flux dev and we pass this prompt of a rhino and a bar.
And then we're just going to say results guidance scales inference steps.
We're going to come out of doing this here.
We can just do like wait generate images and then see if this runs.
So this should take five seconds to run all those images, which is the benefit of doing an asynchronous
or to take taking a lot longer otherwise.
And now we just want to I guess we can see what these results look like.
See you can see we've got all the different images here.
If we open one of them, this is like a fuzzy one with a low inference step.
Cool.
All right.
So now let's display them in an image grid.
So to do that we're going to use Pil.
We just need to get image draw image font.
We also need requests to download the images.
And then we're going to start creating this function here display image grid.
We're going to take results scales and inference steps.
And we're going to do something with them.
First we need to create the image dimensions which is 400 by 300.
And the margin we want between the images.
And then we can use that to figure out the total width and height.
And then we want to do scale factor.
This is basically to create a higher resolution image and scale it down to make it not look really crappy.
And then we have this combined image.
So image new.
And then it's it's creating like a white image essentially.
Okay.
And then we're going to draw that we're going to get the fonts.
This is like a bit of a pain.
I'm just going to copy this font names in.
You don't need to worry too much about this.
It's just for for show.
And then this is just some kind of boilerplate to load the fonts.
I don't think it's that important, but it's just what happened when I was coding it.
There we go.
So that should work.
Hopefully, even if you're on a PC.
And now we need this function I'm just going to copy this function in.
And what this function does is it just draws text on on the on the side.
This is actually the hardest part.
And we're going to use that later on.
All right.
Now we just need to add the images to the grid right.
Yeah.
Let's call this grid.
All right.
So to do that we're just going to say for index result in numeric results.
And then we're going to get the row.
Five by four.
And then the column.
And then we're going to get the URL for this one.
Download the image.
And then let me just copy this across because this is like a specific way I found it works.
So this is getting the URL.
It's going to get the image from file.
It's going to download that image using the request library.
And then we're going to get the content of that image and then scale it and do resampling.
So again don't worry if you don't know what any of this does because it's not super important.
This is really just to make it display nicely.
And then this bit here is just going to calculate the position.
So it's going to take the column multiplied by the image width by the scale factor plus the margin multiplied
by the scale factor again.
And then for the same thing for the Y as well.
And now we're going to add the labels as well.
So from the from the bottom row then we just want to add the text.
And then if we're on the leftmost column we're going to add the text as well.
Don't ask me what this does because this is.
Yeah.
Should work.
Yeah.
Because this is just basically adding the text on the left hand side.
This was like a lot of messing around to get it working.
But I'm sure that you could could do a better job here.
All right.
And then I'm going to go final image is going to just resize and resample.
And then we want to just rather return and just display that final image.
Okay.
And then display image grid.
These needs to be here I believe.
Yeah.
Cool.
So just to summarize there's a lot going on here.
But you don't.
None of it's important.
All this is doing is just creating the grid.
And there are lots of different ways to create a grid.
So there's no secret sauce in here.
It's just overly complicated.
It's just figuring out where the images should go, How to scale them to the right size and then how
to add text onto them.
So if we run this, hopefully it should work.
So it's just getting all those images.
The reason it takes some time is it's got to download the image from file.
And here we go.
Now we have the grid.
And because we used the same seed it's the exact same image.
And that's the nice thing about reproducibility here.
So we could actually change.
Now we have this.
We could go and change the different steps that we put in here the guidance scales.
Or we could use different parameters as well.
Like for example we could change something about the prompt.
Maybe we want like a lion and a rhino and a chicken and just kind of see how they how they change things
over time.
This is a technique I use all the time to figure out how much to tune one way or another, a parameter,
or what a prompt looks like with different variables in in the template.