Okay.
We're going to use file client locally to send a request to file and run flux to image generation tool.
So first we need to pip install.
I recommend installing it in an environment if you don't already have that.
And then you need to load the environment as well.
We have load env here from dot env.
And then we just run load dot env.
And when you have a dot env file.
So just a file on your computer that's called dot env.
In this local space you would have a file underscore key in their file key equals the API key that you
have from here.
So if you run that it's going to tell you true.
This loaded the environment.
And then hopefully you should have your API key.
If you don't have an API key you just need to go to this URL here.
And then you should be able to find find your API key.
Once you create an account and once you have a paid account as well.
All right.
So now let's get into the code.
So we want to import file client.
We want to get the Pil package to get an image.
I want to import requests so we can make the request to get the image afterwards.
And then we also want to get this bytesio as well.
This is for dealing with the image.
But the main thing we do with file is we just submit a prompt and then we submit a model as well.
In this case the prompt is a photo of a rhino dressed suit and tie sitting at a table in a bar with
bar stools, award winning photography, and then this name of the photographer.
And we're going to create the handler.
We're going to go to file client and then submit.
And there's a few different methods that I'll show you.
But then this is the simple way to do it.
We just want to get the result and to do the result we just do handler get.
And then we can print the result.
And let me just show you what that comes back with.
You can see we have an image here.
And there's a width or height.
how much time it took, for example, and the prompt and if we click on this should be able to see the
image.
Here we go.
We have a rhino in a suit.
So in order to get at that image we want to do image URL equals result images.
And then you get the first image because it could come back with multiple if you add that argument in
and then you get the URL of that image, and then you just need to make a request to get the image to
download it as bytes.
And then we can open the image like this.
So if I run that again it's going to run the prompt again.
And then it'll just display the image for us.
Here we go.
So we can see it right here.
So this is a new image.
Every time you run it it's something different unless you use the same seed okay.
Now that we have that we want to function for generating images in general, let's just wrap that and
we'll say call this generate image.
We want to take in a prompt and a model.
And then we're going to do the handler.
We're going to take input the model there the arguments will be the prompt.
And then in this case we wanted to change the image size.
We're going to do a portrait image.
We get the result.
And then when I get the URL, get that image, and then we'll display the image and return the URL.
So now if we want to use that we would just say generate image.
And then we just pass in that prompt and we pass in the model as well.
In this case flux dev.
But actually we could change that.
So maybe just try a different model.
Flux now is another model we can use.
Cool.
Now we have a portrait image and we're using the Chanel model which is a little bit faster but less
detailed than the dev model.
So it just depends on which one you use.
It's also much cheaper, so you can try out all the different models on replicate.
They give you this code, but as you can see it's a relatively simple API request.
So it really makes things a lot easier.
I prefer to do this.
I used to run stable diffusion on my local computer, and there's stuff that always seems to go wrong
in the setup with that.
And yeah, I've switched everything over just to this because it's much, much simpler.