FLUX.1-dev

Black Forest Labs

FLUX.1 [dev] is a 12 billion parameter rectified flow transformer that generates images from text descriptions. Use this model for the fastest inference with the TheStage AI framework "Elastic Models."

Deploy

Serving with Docker Image

For serving with Nvidia GPUs, we provide ready-to-go Docker containers with OpenAI-compatible API endpoints.
Using our containers you can set up an inference endpoint on any desired cloud/serverless providers as well as on-premise servers.
You can also use this container to run inference through TheStage AI platform.

Prebuilt image from ECR

Pull docker image and start inference container:

docker pull public.ecr.aws/i3f7g5s7/thestage/elastic-models:0.2.0-diffusers-24.09c
docker run --rm -ti \
  --name serving_thestage_model \
  -p 8000:80 \
  -e AUTH_TOKEN=<AUTH_TOKEN> \
  -e MODEL_REPO=black-forest-labs/FLUX.1-dev \
  -e MODEL_SIZE=<MODEL_SIZE> \
  -e MODEL_BATCH=<MAX_BATCH_SIZE> \
  -e HUGGINGFACE_ACCESS_TOKEN=<HUGGINGFACE_ACCESS_TOKEN> \
  -e THESTAGE_AUTH_TOKEN=<THESTAGE_ACCESS_TOKEN> \
  -v /mnt/hf_cache:/root/.cache/huggingface \
  public.ecr.aws/i3f7g5s7/thestage/elastic-models:0.2.0-diffusers-24.09c
ParameterDescription
Available: S, M, L, XL.
Maximum batch size to process in parallel.
Hugging Face access token.
TheStage token generated on the platform (Profile -> Access tokens).
Token for endpoint authentication. You can set it to any random string; it must match the value used by the client.

Invocation

You can invoke the endpoint using CURL as follows:

curl -X POST <http://127.0.0.1:8000/v1/images/generations>  \
    -H "Authorization: Bearer <AUTH_TOKEN>"  \
    -H "Content-Type: application/json" \
    -H "X-Model-Name: flux-1-dev-<MODEL_SIZE>-bs<MAX_BATCH_SIZE>" \
    -d '{
          "prompt": "Cat eating banana",
          "seed": 12,
          "aspect_ratio": "1:1",
          "guidance_scale": 6.5,
          "num_inference_steps": 28
        }' \
    --output sunset.webp -D -

Or using Python requests:

import requests
import json
url = "http://127.0.0.1:8000/v1/images/generations"
payload = json.dumps({
  "prompt": "sunset",
  "seed": 12,
  "aspect_ratio": "1:1",
  "guidance_scale": 6.5,
  "num_inference_steps": 28
})
headers = {
  'Authorization': 'Bearer <AUTH_TOKEN>',
  'Content-Type': 'application/json',
  'X-Model-Name': 'flux-1-dev-<MODEL_SIZE>-bs<MAX_BATCH_SIZE>'
}
response = requests.request("POST", url, headers=headers, data=payload)
with open("sunset.webp", "wb") as f:
    f.write(response.content)

Or using OpenAI python client:

import os, base64, pathlib, json
from openai import OpenAI

BASE_URL = "http://<your_ip>/v1"
API_KEY  = ""
MODEL    = "flux-1-dev-<MODEL_SIZE>-bs<MAX_BATCH_SIZE>"

client = OpenAI(
    api_key=API_KEY,
    base_url=BASE_URL,
    default_headers={"X-Model-Name": MODEL}
)

response = client.with_raw_response.images.generate(
    model=MODEL,
    prompt="Cat eating banana",
    n=1,
    extra_body={
        "seed": 111,
        "aspect_ratio": "1:1",
        "guidance_scale": 3.5,
        "num_inference_steps": 28
    },
)

with open("thestage_image.webp", "wb") as f:
    f.write(response.content)

Endpoint Parameters

Method

POST /v1/images/generations

Header Parameters

Authorization: string
Bearer token for authentication. Should match the AUTH_TOKEN set during container startup.

Content-Type: string
Must be set to application/json.

X-Model-Name: string
Specifies the model to use for generation. Format: flux-1-dev- -bs , where is one of S, M, L, XL, original and is the maximum batch size configured during container startup.

Input Body

prompt : string
The text prompt to generate an image for.

seed: int32
Random seed for generation.

num_inference_steps: int32
Number of diffusion steps to use for generation. Higher values yield better quality but take longer. Default is 28

aspect_ratio: string
Aspect ratio of the generated image. Supported values:
```
"1:1": (1024, 1024),
"16:9": (1280, 736),
"21:9": (1280, 544),
"3:2": (1248, 832),
"2:3": (832, 1248),
"4:3": (1184, 896),
"3:4": (896, 1184),
"5:4": (1152, 928),
"4:5": (928, 1152),
"9:16": (736, 1280),
"9:21": (544, 1280)
```

guidance_scale: float32
Guidance scale for classifier-free guidance. Higher values increase adherence to the prompt.

Deploy on Modal

For more details please use the tutorial Modal deployment

Clone modal serving code

git clone https://github.com/TheStageAI/ElasticModels.git
cd ElasticModels/examples/modal

Configuration of environment variables

Set your environment variables in modal_serving.py:

# modal_serving.py

ENVS = {
    "MODEL_REPO": "black-forest-labs/FLUX.1-dev",
    "MODEL_BATCH": "4",
    "THESTAGE_AUTH_TOKEN": "",
    "HUGGINGFACE_ACCESS_TOKEN": "",
    "PORT": "80",
    "PORT_HEALTH": "80",
    "HF_HOME": "/cache/huggingface",
}

Configuration of GPUs

Set your desired GPU type and autoscaling variables in modal_serving.py:

# modal_serving.py

@app.function(
    image=image,
    gpu="B200",
    min_containers=8,
    max_containers=8,
    timeout=10000,
    ephemeral_disk=600 * 1024,
    volumes={"/opt/project/.cache": HF_CACHE},
    startup_timeout=60*20
)
@modal.web_server(
    80,
    label="black-forest-labs/FLUX.1-dev-test",
    startup_timeout=60*20
)
def serve():
    pass

Run serving

modal serve modal_serving.py