Run Flux locally

The internet is full of code and samples giving you ways to run Flux.1-dev locally but if you are coding an application here is the bare minimum that will get you started.

import torch
from diffusers import FluxPipeline

pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev",
torch_dtype=torch.bfloat16, device_map='balanced')

prompt = '''A rabbit holding a pancart with THAT WORKS written on it'''
image = pipe(
    prompt,
    height=512,
    width=512,
    guidance_scale=3.5,
    output_type="pil",
    num_inference_steps=25,
    max_sequence_length=512,
    generator=torch.Generator("cuda")
).images[0]
image.save("flux-dev.png")

This uses CUDA so you need a decent RTX 3090 or 4090. If you don't have one but a lot of RAM you can just remove the torch.Generator("cuda") and use torch.Generator("cpu")

That's it.