import os
import torch
from diffusers import FluxKontextPipeline
from diffusers.utils import load_image
from transformer_patches import apply_patches
apply_patches()
DEVICE = "npu"
MODEL_PATH = "black-forest-labs/FLUX.1-Kontext-dev"
OUTPUT_PATH = "./infer_result"
IMAGE = "./flux_cat.png"
OUTPUT_IMAGE_1 = "flux_kontext_1.png"
OUTPUT_IMAGE_2 = "flux_kontext_2.png"
GENERATOR = torch.Generator(device="cpu").manual_seed(42)
STEPS = 50
GUIDANCE = 2.5
RESOLUTION = 1024
os.makedirs(OUTPUT_PATH, exist_ok=True)
pipe = FluxKontextPipeline.from_pretrained(
MODEL_PATH,
torch_dtype=torch.bfloat16,
local_files_only=True,
)
pipe = pipe.to(DEVICE)
pipe.transformer.set_attention_backend("_native_npu")
image = load_image(IMAGE).convert("RGB")
prompt = "Relight the scene with a soft, diffused foggy glow emanating from the top left side"
image = pipe(
image=image,
prompt=prompt,
num_inference_steps=STEPS,
height=RESOLUTION,
width=RESOLUTION,
guidance_scale=GUIDANCE,
generator=GENERATOR,
).images[0]
saved_image = OUTPUT_PATH + "/" + OUTPUT_IMAGE_1
image.save(saved_image)
image = load_image(saved_image).convert("RGB")
prompt = "Change the sign from 'Hello World' to 'Mindspeed MM'"
image = pipe(
image=image,
prompt=prompt,
num_inference_steps=STEPS,
height=RESOLUTION,
width=RESOLUTION,
guidance_scale=GUIDANCE,
generator=GENERATOR,
).images[0]
image.save(OUTPUT_PATH + "/" + OUTPUT_IMAGE_2)