Fine-tune FLUX.1 with an API
Captured source
source ↗Fine-tune FLUX.1 with an API – Replicate blog
Replicate Blog
Fine-tune FLUX.1 with an API
Posted September 9, 2024 by zeke
Info
You can now fine-tune models with the fast FLUX trainer on Replicate. It’s fast (under 2 minutes), cheap (under $2), and gives you a warm, runnable model plus LoRA weights to download.
FLUX.1 is all the rage these days, and for good reason. It’s a fast, powerful image generation model that’s easy to use and fine-tune, and it generates stunning images . Last week we brought you a guide to fine-tuning Flux with faces . That guide used an entirely web-based flow to create a fine-tuned Flux model, without writing a single line of code. We heard from some users that they would like to fine-tune Flux with an API, so we’re back this week with another tutorial that shows you how to do just that. In this guide, you’ll create and run your own fine-tuned Flux models programmatically using Replicate’s HTTP API. Kick off your Flux trainings programmatically with Replicate’s HTTP API. Step 0: Prerequisites Here’s what you’ll need to get started:
A Replicate account
A handful of training images
A small budget of 2-3 US dollars for training costs
cURL , the beloved command-line tool for making HTTP requests that’s been around since the 1990s
Step 1: Gather your training images You’ll need a few images of yourself to get started. You can fine-tune Flux with as few as two training images, but for best results you’ll want to use at least 10 images or more. In theory you’ll get continually better results as you include more images in the training data, but the training process can take longer the more images you add. Consider the following when gathering your training images:
WebP, JPG, and PNG formats are all supported.
Use 1024x1024 or higher resolution if possible.
Filenames don’t matter. Name your files whatever you like.
Images can have different aspect ratios; they don’t all need to be square, landscape or portrait.
10 images is a good minimum.
Once you’ve gathered your images, put them in a zip file. Assuming you put them all in a folder called data , run this command to generate a file called data.zip : Copy
zip -r data.zip data
Step 2: Set an API token in your environment You’ll need an API token to make requests to the Replicate API. Visit replicate.com/account/api-tokens to create a new API token, then copy it to your clipboard. Most Replicate tools like the client libraries and the Replicate CLI follow the convention of looking for an API token in an environment variable called REPLICATE_API_TOKEN . Set the REPLICATE_API_TOKEN environment variable by running this command in your terminal: Copy
export REPLICATE_API_TOKEN = "r8_..."
Tip: If you’re going to be making a lot of API requests using cURL commands or code on your own computer, you might want to set the REPLICATE_API_TOKEN environment variable in your shell profile or dotfiles so you don’t have to type it out every time you open a new terminal window. Step 3: Create the destination model Next, you’ll create an empty model on Replicate for your trained model. When your training finishes, it will be pushed as a new version to this model. You can create models under your own personal account or in an organization if you want to share access with your team or other collaborators. There are several ways to create models on Replicate, like using the Replicate web UI or the Replicate CLI , but in this guide we’ll create the model by making a cURL request to the models.create API endpoint . Choose a descriptive name for your model, like flux-my-cool-finetune or flux-my-dog-fluffy . A popular convention for Flux models is to include flux somewhere in the name, but that’s not required. Run this command in your terminal to create the model, replacing your-username and your-model-name with the correct values: Copy
curl -s -X POST \ -H "Authorization: Bearer $REPLICATE_API_TOKEN " \ -H 'Content-Type: application/json' \ -d '{"owner": "your-username", "name": "your-model-name", "description": "An example model", "visibility": "public", "hardware": "gpu-a40-large"}' \ https://api.replicate.com/v1/models
Step 4: Upload your training data Next you’ll need to upload your zip file somewhere on the internet that is publicly accessible, like an S3 bucket or a GitHub Pages site. You can also use Replicate’s Files API to upload your training data. Here’s an example of how to do that with cURL, assuming you named your training data file data.zip : Copy
curl -s -X POST "https://api.replicate.com/v1/files" \ -H "Authorization: Bearer $REPLICATE_API_TOKEN " \ -H "Content-Type: multipart/form-data" \ -F "content=@data.zip;type=application/zip;filename=data.zip"
The output will be a JSON response containing the URL of the uploaded file: Copy
{"id":"MThjNTQwOTEtNDJmNS00Mjc2LWIzMTUtMzczMTNmNzYyYTEw","name":"data.zip","content_type":"application/zip","size":1431986,"etag":"9c5b5aa1178bd843722a8cce85ba778b","checksums":{"sha256":"9d0efe1c32d02fd8a0b01af67bea357d7279522aff8b4158a37529abe4713103","md5":"9c5b5aa1178bd843722a8cce85ba778b"},"metadata":{},"created_at":"2024-09-09T20:17:37.031Z","expires_at":"2024-09-10T20:17:37.031Z","urls":{"get":"https://api.replicate.com/v1/files/MThjNTQwOTEtNDJmNS00Mjc2LWIzMTUtMzczMTNmNzYyYTEw"}}
Find the URL in that output that starts with https://api.replicate.com/v1... and copy it to your clipboard. You’ll use it as an input to the training process in the next step. Tip: If you have the jq command-line JSON processor installed, you can upload the file and output the URL in one step like this: Copy
curl -s -X POST "https://api.replicate.com/v1/files" \ -H "Authorization: Bearer $REPLICATE_API_TOKEN " \ -H "Content-Type: multipart/form-data" \ -F "content=@data.zip;type=application/zip;filename=data.zip" | jq -r '.urls.get'
Step 5: Start the training process Now that you’ve got your training data uploaded to a publicly accessible URL, the next step is to start the training process using the API. You’ll be billed per second for the time the training process takes to run. Trainings for the Flux model run on Nvidia H100 GPU hardware, which costs $0.001528 per second at the time of this writing. For a 20-minute training (which is typical when using about 20 training images and 1000 steps), you can expect to pay about $1.85 USD. Once your model is trained, you can run it with an API just like any other Replicate model, and you’ll only be billed for the compute time it takes to…
Excerpt shown — open the source for the full document.
Notability
notability 7.0/10Enables fine-tuning of state-of-the-art image model via API