This repository contains a FLUX.1-dev text-to-image diffusion model stored in Orbax/JAX format, optimized for use with JAX/Flax frameworks.
FLUX.1-dev is a powerful text-to-image generation model that uses a transformer-based architecture with dual text encoders (CLIP and T5) for enhanced text understanding and image generation capabilities.
For a comprehensive tutorial on using FLUX models with Diffuse, please refer to:
FLUX Tutorial Documentation
1import jax
2from pathlib import Path
3from huggingface_hub import snapshot_download
4from diffuse import FluxModelLoader, FluxTimer, Flow, Predictor, Denoiser
5from diffuse.integrators import EulerIntegrator
6from diffuse.utils import _latent_shapes
7
8# ===========================
9# 1. Download Model
10# ===========================
11HF_REPO_ID = "jcopo/flux_jax"
12
13checkpoint_dir = Path(snapshot_download(repo_id=HF_REPO_ID, repo_type="model"))
14
15# ===========================
16# 2. Set Generation Parameters
17# ===========================
18PROMPT = "A serene landscape with mountains at sunset, highly detailed, photorealistic"
19HEIGHT = 512
20WIDTH = 512
21NUM_STEPS = 20
22GUIDANCE_SCALE = 4.0
23SEED = 42
24
25# ===========================
26# 3. Load Model and Prepare Network
27# ===========================
28loader = FluxModelLoader(checkpoint_dir=checkpoint_dir, verbose=True)
29
30conditioned = loader.prepare_conditioned_network(
31 prompt=PROMPT,
32 negative_prompt=None,
33 guidance_scale=GUIDANCE_SCALE,
34 height=HEIGHT,
35 width=WIDTH,
36)
37
38# ===========================
39# 4. Setup Diffusion Components
40# ===========================
41_, transformer_hw = _latent_shapes(HEIGHT, WIDTH)
42image_seq_len = transformer_hw[0] * transformer_hw[1]
43
44# Initialize timer with dynamic shift
45timer = FluxTimer(num_steps=NUM_STEPS, use_dynamic_shift=True)
46timer.set_image_seq_len(image_seq_len)
47
48# Create flow model and predictor
49flow = Flow(tf=1.0)
50predictor = Predictor(
51 model=flow,
52 network=conditioned.network_fn,
53 prediction_type="velocity",
54)
55
56# Create integrator and denoiser
57integrator = EulerIntegrator(model=flow, timer=timer)
58denoiser = Denoiser(
59 integrator=integrator,
60 model=flow,
61 predictor=predictor,
62 x0_shape=(transformer_hw[0], transformer_hw[1], conditioned.in_channels),
63)
64
65# ===========================
66# 5. Generate Image
67# ===========================
68key = jax.random.PRNGKey(SEED)
69state, _ = denoiser.generate(
70 rng_key=key,
71 n_steps=NUM_STEPS,
72 n_particles=1,
73 keep_history=False,
74)
75
76# Get latent from generation
77latent = state.integrator_state.position
78
79# ===========================
80# 6. Decode to Image
81# ===========================
82image = loader.decode_latent(latent)
83print(f"Generated image shape: {image.shape}")
84
85# Save image (image is a numpy array in [0, 1] range)
86from PIL import Image
87img = Image.fromarray((image * 255).astype('uint8'))
88img.save("output.png")
Please refer to the original FLUX.1-dev license terms for usage restrictions and guidelines.
If you use this model in your research, please cite the original FLUX paper and the Diffuse library.
1@software{diffuse2024,
2 title = {Diffuse: A modular diffusion model library},
3 author = {Iollo, J., Oudoumanessah G.},
4 year = {2025},
5 url = {https://github.com/jcopo/diffuse}
6}