FLUX Inference Codebase

repository·main·Indexed 12 days ago

https://github.com/black-forest-labs/flux

Minimal inference code for running Black Forest Labs' open-weight image generation and editing models locally. Supports FLUX.1 [schnell] and [dev], as well as specialized tools like Kontext, Fill, Redux, Canny, and Depth. Includes support for TensorRT optimization, CLI-based sampling, and integration with Hugging Face diffusers.

Tokens
15K
Snippets
45
Records
69
Agent score
98%

What's inside FLUX

  1. Overview of FLUX.1 [schnell]

    main
    FLUX.1 [schnell] is a 12 billion parameter rectified flow transformer designed for high-quality image generation from text descriptions. It is optimized for speed using latent adversarial diffusion distillation, allowing for high-quality image generation in as few as 1 to 4 steps. It is released under the apache-2.0 license, making it suitable for personal, scientific, and commercial use.
  2. Use FLUX.1 Kontext [dev] for image editing and in-context generation

    main

    FLUX.1 Kontext [dev] is a 12 billion parameter rectified flow transformer designed for instruction-based image editing.

    Core Capabilities:

    • Image Editing: Modify existing images using text instructions.
    • In-Context Reference: Use characters, styles, or objects as references without requiring fine-tuning.
    • Iterative Refinement: High consistency allows for multiple successive edits with minimal visual drift.
    • Efficiency: Trained using guidance distillation for improved performance.

    Implementation Options:

  3. License and usage terms for FLUX.1 Krea [dev]

    main

    FLUX.1 Krea [dev] is released under the FLUX.1 [dev] Non-Commercial License.

    Key Licensing Details:

    • Permitted Use: Generated outputs can be used for personal, scientific, and commercial purposes only as described in the specific license terms (refer to the FLUX.1 [dev] Non-Commercial License).
    • Prohibited Use: The license prohibits the generation of unlawful content or the use of generated content for unlawful, defamatory, or abusive purposes.
    • Compliance: Developers using the open-weight model are expected to implement filters or manual reviews for illegal or infringing content to comply with the license terms.
  4. Understand the FLUX.1 [dev] license and usage restrictions

    main

    Licensing

    FLUX.1 [dev] is released under the FLUX.1 [dev] Non-Commercial License. While outputs can be used for personal, scientific, and commercial purposes as defined by the license, users must adhere to strict usage policies.

    Out-of-Scope Use

    The model and its derivatives may not be used for:

    • Violating any laws or regulations.
    • Exploiting or harming minors.
    • Generating verifiably false information to harm others.
    • Generating personal identifiable information (PII) to harm individuals.
    • Harassment, abuse, threats, stalking, or bullying.
    • Creating non-consensual nudity or illegal pornographic content.
    • Fully automated decision making that impacts legal rights.
    • Facilitating large-scale disinformation campaigns.
  5. Configure FLUX.1 Fill model paths via environment variables

    main

    By default, weights are downloaded automatically to the checkpoints/ directory from HuggingFace when running demos. To use manually downloaded weights, you can place them in checkpoints/ or point to them using the following environment variables:

    • FLUX_MODEL: Path to your model file.
    • FLUX_AE: Path to your autoencoder file.
    export FLUX_MODEL=<your model path here>
    export FLUX_AE=<your autoencoder path here>
  6. Install FLUX with TensorRT support

    main

    For TensorRT support, you must use an NVIDIA PyTorch image via enroot. Follow these steps to import the image, create a container, and install the repository with the [tensorrt] extra and the NVIDIA PyPI index.

    cd $HOME && git clone https://github.com/black-forest-labs/flux
    enroot import 'docker://$oauthtoken@nvcr.io#nvidia/pytorch:25.01-py3'
    enroot create -n pti2501 nvidia+pytorch+25.01-py3.sqsh
    enroot start --rw -m ${PWD}/flux:/workspace/flux -r pti2501
    cd flux
    pip install -e ".[tensorrt]" --extra-index-url https://pypi.nvidia.com
  7. Run FLUX interactive demos (Streamlit and Gradio)

    main

    Streamlit Demo

    Provides both text-to-image and image-to-image capabilities.

    streamlit run demo_st.py

    Gradio Demo

    An interactive experience with the following options:

    • --name: Model to use (flux-schnell or flux-dev)
    • --device: 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
    # Run Gradio with dev model and a public link
    python demo_gr.py --name flux-dev --share
    python demo_gr.py --name flux-schnell --device cuda
  8. Integrate FLUX with HuggingFace Diffusers

    main

    FLUX models are compatible with the diffusers library.

    1. Install diffusers: pip install git+https://github.com/huggingface/diffusers.git
    2. Use FluxPipeline to load and run the model.

    Tip: Use pipe.enable_model_cpu_offload() to save VRAM if you have limited GPU power. For FLUX.1 [dev], use a larger number of num_inference_steps than for schnell.

    import torch
    from diffusers import FluxPipeline
    
    model_id = "black-forest-labs/FLUX.1-schnell"
    
    pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16)
    pipe.enable_model_cpu_offload() 
    
    prompt = "A cat holding a sign that says hello world"
    seed = 42
    image = pipe(
        prompt,
        output_type="pil",
        num_inference_steps=4, 
        generator=torch.Generator("cpu").manual_seed(seed)
    ).images[0]
    image.save("flux-schnell.png")
  9. Enable usage tracking for commercial use

    main

    When using open-weight models commercially, you must track usage via the BFL API. To do this, set your BFL_API_KEY environment variable and use the --track_usage flag in the CLI. This applies to FLUX.1 Kontext [dev], FLUX.1 [dev], and FLUX.1 Tools [dev].

    export BFL_API_KEY="your_api_key_here"
    
    # For a single generation with tracking
    python -m flux kontext --track_usage --prompt "replace the logo with the text 'Black Forest Labs'"
    
    # For continuous loop with tracking
    python -m flux kontext --track_usage --loop