Skip to content

black-forest-labs/flux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FLUX

by Black Forest Labs: https://blackforestlabs.ai. Documentation for our API can be found here: docs.bfl.ml.

grid

This repo contains minimal inference code to run text-to-image and image-to-image with our Flux latent rectified flow transformers.

Inference partners

We are happy to partner with Replicate, FAL, Mystic, and Together. You can sample our models using their services. Below we list relevant links.

Replicate:

FAL:

Mystic:

Together:

Local installation

cd $HOME && git clone https://github.com/black-forest-labs/flux
cd $HOME/flux
python3.10 -m venv .venv
source .venv/bin/activate
pip install -e ".[all]"

Models

We are offering three models:

  • FLUX1.1 [pro] available via API only
  • FLUX.1 [pro] available via API only
  • FLUX.1 [dev] guidance-distilled variant
  • FLUX.1 [schnell] guidance and step-distilled variant
Name HuggingFace repo License md5sum
FLUX.1 [schnell] https://huggingface.co/black-forest-labs/FLUX.1-schnell apache-2.0 a9e1e277b9b16add186f38e3f5a34044
FLUX.1 [dev] https://huggingface.co/black-forest-labs/FLUX.1-dev FLUX.1-dev Non-Commercial License a6bd8c16dfc23db6aee2f63a2eba78c0
FLUX.1 [pro] Only available in our API.
FLUX1.1 [pro] Only available in our API.

The weights of the autoencoder are also released under apache-2.0 and can be found in either of the two HuggingFace repos above. They are the same for both models.

Usage

The weights will be downloaded automatically from HuggingFace once you start one of the demos. To download FLUX.1 [dev], you will need to be logged in, see here. If you have downloaded the model weights manually, you can specify the downloaded paths via environment-variables:

export FLUX_SCHNELL=<path_to_flux_schnell_sft_file>
export FLUX_DEV=<path_to_flux_dev_sft_file>
export AE=<path_to_ae_sft_file>

For interactive sampling run

python -m flux --name <name> --loop

Or to generate a single sample run

python -m flux --name <name> \
  --height <height> --width <width> \
  --prompt "<prompt>"

We also provide a streamlit demo that does both text-to-image and image-to-image. The demo can be run via

streamlit run demo_st.py

We also offer a Gradio-based demo for an interactive experience. To run the Gradio demo:

python demo_gr.py --name flux-schnell --device cuda

Options:

  • --name: Choose the model to use (options: "flux-schnell", "flux-dev")
  • --device: Specify the device to use (default: "cuda" if available, otherwise "cpu")
  • --offload: Offload model to CPU when not in use
  • --share: Create a public link to your demo

To run the demo with the dev model and create a public link:

python demo_gr.py --name flux-dev --share

Diffusers integration

FLUX.1 [schnell] and FLUX.1 [dev] are integrated with the 🧨 diffusers library. To use it with diffusers, install it:

pip install git+https://github.com/huggingface/diffusers.git

Then you can use FluxPipeline to run the model

import torch
from diffusers import FluxPipeline

model_id = "black-forest-labs/FLUX.1-schnell" #you can also use `black-forest-labs/FLUX.1-dev`

pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16)
pipe.enable_model_cpu_offload() #save some VRAM by offloading the model to CPU. Remove this if you have enough GPU power

prompt = "A cat holding a sign that says hello world"
seed = 42
image = pipe(
    prompt,
    output_type="pil",
    num_inference_steps=4, #use a larger number if you are using [dev]
    generator=torch.Generator("cpu").manual_seed(seed)
).images[0]
image.save("flux-schnell.png")

To learn more check out the diffusers documentation

API usage

Our API offers access to our models. It is documented here: docs.bfl.ml.

In this repository we also offer an easy python interface. To use this, you first need to register with the API on api.bfl.ml, and create a new API key.

To use the API key either run export BFL_API_KEY=<your_key_here> or provide it via the api_key=<your_key_here> parameter. It is also expected that you have installed the package as above.

Usage from python:

from flux.api import ImageRequest

# this will create an api request directly but not block until the generation is finished
request = ImageRequest("A beautiful beach", name="flux.1.1-pro")
# or: request = ImageRequest("A beautiful beach", name="flux.1.1-pro", api_key="your_key_here")

# any of the following will block until the generation is finished
request.url
# -> https:<...>/sample.jpg
request.bytes
# -> b"..." bytes for the generated image
request.save("outputs/api.jpg")
# saves the sample to local storage
request.image
# -> a PIL image

Usage from the command line:

$ python -m flux.api --prompt="A beautiful beach" url
https:<...>/sample.jpg

# generate and save the result
$ python -m flux.api --prompt="A beautiful beach" save outputs/api

# open the image directly
$ python -m flux.api --prompt="A beautiful beach" image show