Skip to content

Commit eea7689

Browse files
authored
Flux Kontext (#11812)
* support flux kontext * make fix-copies * add example * add tests * update docs * update * add note on integrity checker * make fix-copies issue * add copied froms * make style * update repository ids * more copied froms
1 parent 27bf7fc commit eea7689

File tree

7 files changed

+1368
-0
lines changed

7 files changed

+1368
-0
lines changed

docs/source/en/api/pipelines/flux.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Flux comes in the following variants:
3939
| Canny Control (LoRA) | [`black-forest-labs/FLUX.1-Canny-dev-lora`](https://huggingface.co/black-forest-labs/FLUX.1-Canny-dev-lora) |
4040
| Depth Control (LoRA) | [`black-forest-labs/FLUX.1-Depth-dev-lora`](https://huggingface.co/black-forest-labs/FLUX.1-Depth-dev-lora) |
4141
| Redux (Adapter) | [`black-forest-labs/FLUX.1-Redux-dev`](https://huggingface.co/black-forest-labs/FLUX.1-Redux-dev) |
42+
| Kontext | [`black-forest-labs/FLUX.1-kontext`](https://huggingface.co/black-forest-labs/FLUX.1-kontext) |
4243

4344
All checkpoints have different usage which we detail below.
4445

@@ -273,6 +274,46 @@ images = pipe(
273274
images[0].save("flux-redux.png")
274275
```
275276

277+
### Kontext
278+
279+
Flux Kontext is a model that allows in-context control of the image generation process, allowing for editing, refinement, relighting, style transfer, character customization, and more.
280+
281+
```python
282+
import torch
283+
from diffusers import FluxKontextPipeline
284+
from diffusers.utils import load_image
285+
286+
pipe = FluxKontextPipeline.from_pretrained(
287+
"black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16
288+
)
289+
pipe.to("cuda")
290+
291+
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yarn-art-pikachu.png").convert("RGB")
292+
prompt = "Make Pikachu hold a sign that says 'Black Forest Labs is awesome', yarn art style, detailed, vibrant colors"
293+
image = pipe(
294+
image=image,
295+
prompt=prompt,
296+
guidance_scale=2.5,
297+
generator=torch.Generator().manual_seed(42),
298+
).images[0]
299+
image.save("flux-kontext.png")
300+
```
301+
302+
Flux Kontext comes with an integrity safety checker, which should be run after the image generation step. To run the safety checker, install the official repository from [black-forest-labs/flux](https://github.com/black-forest-labs/flux) and add the following code:
303+
304+
```python
305+
from flux.safety import PixtralIntegrity
306+
307+
# ... pipeline invocation to generate images
308+
309+
integrity_checker = PixtralIntegrity(torch.device("cuda"))
310+
image_ = np.array(image) / 255.0
311+
image_ = 2 * image_ - 1
312+
image_ = torch.from_numpy(image_).to("cuda", dtype=torch.float32).unsqueeze(0).permute(0, 3, 1, 2)
313+
if integrity_checker.test_image(image_):
314+
raise ValueError("Your image has been flagged. Choose another prompt/image or try again.")
315+
```
316+
276317
## Combining Flux Turbo LoRAs with Flux Control, Fill, and Redux
277318

278319
We can combine Flux Turbo LoRAs with Flux Control and other pipelines like Fill and Redux to enable few-steps' inference. The example below shows how to do that for Flux Control LoRA for depth and turbo LoRA from [`ByteDance/Hyper-SD`](https://hf.co/ByteDance/Hyper-SD).

src/diffusers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
"FluxFillPipeline",
382382
"FluxImg2ImgPipeline",
383383
"FluxInpaintPipeline",
384+
"FluxKontextPipeline",
384385
"FluxPipeline",
385386
"FluxPriorReduxPipeline",
386387
"HiDreamImagePipeline",
@@ -974,6 +975,7 @@
974975
FluxFillPipeline,
975976
FluxImg2ImgPipeline,
976977
FluxInpaintPipeline,
978+
FluxKontextPipeline,
977979
FluxPipeline,
978980
FluxPriorReduxPipeline,
979981
HiDreamImagePipeline,

src/diffusers/pipelines/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
"FluxFillPipeline",
141141
"FluxPriorReduxPipeline",
142142
"ReduxImageEncoder",
143+
"FluxKontextPipeline",
143144
]
144145
_import_structure["audioldm"] = ["AudioLDMPipeline"]
145146
_import_structure["audioldm2"] = [
@@ -609,6 +610,7 @@
609610
FluxFillPipeline,
610611
FluxImg2ImgPipeline,
611612
FluxInpaintPipeline,
613+
FluxKontextPipeline,
612614
FluxPipeline,
613615
FluxPriorReduxPipeline,
614616
ReduxImageEncoder,

src/diffusers/pipelines/flux/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
_import_structure["pipeline_flux_fill"] = ["FluxFillPipeline"]
3434
_import_structure["pipeline_flux_img2img"] = ["FluxImg2ImgPipeline"]
3535
_import_structure["pipeline_flux_inpaint"] = ["FluxInpaintPipeline"]
36+
_import_structure["pipeline_flux_kontext"] = ["FluxKontextPipeline"]
3637
_import_structure["pipeline_flux_prior_redux"] = ["FluxPriorReduxPipeline"]
3738
if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT:
3839
try:
@@ -52,6 +53,7 @@
5253
from .pipeline_flux_fill import FluxFillPipeline
5354
from .pipeline_flux_img2img import FluxImg2ImgPipeline
5455
from .pipeline_flux_inpaint import FluxInpaintPipeline
56+
from .pipeline_flux_kontext import FluxKontextPipeline
5557
from .pipeline_flux_prior_redux import FluxPriorReduxPipeline
5658
else:
5759
import sys

0 commit comments

Comments
 (0)