import gradio as gr import numpy as np import spaces import torch import random import os # from diffusers import QwenImageEditInpaintPipeline from optimization import optimize_pipeline_ from diffusers.utils import load_image from diffusers import FlowMatchEulerDiscreteScheduler from qwenimage.pipeline_qwenimage_edit_inpaint import QwenImageEditInpaintPipeline from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3 from PIL import Image # Set environment variable for parallel loading os.environ["HF_ENABLE_PARALLEL_LOADING"] = "YES" MAX_SEED = np.iinfo(np.int32).max MAX_IMAGE_SIZE = 2048 # Initialize Qwen Image Edit pipeline # Scheduler configuration for Lightning scheduler_config = { "base_image_seq_len": 256, "base_shift": math.log(3), "invert_sigmas": False, "max_image_seq_len": 8192, "max_shift": math.log(3), "num_train_timesteps": 1000, "shift": 1.0, "shift_terminal": None, "stochastic_sampling": False, "time_shift_type": "exponential", "use_beta_sigmas": False, "use_dynamic_shifting": True, "use_exponential_sigmas": False, "use_karras_sigmas": False, } # Initialize scheduler with Lightning config scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config) pipe = QwenImageEditInpaintPipeline.from_pretrained("Qwen/Qwen-Image-Edit", scheduler=scheduler, torch_dtype=torch.bfloat16).to("cuda") pipe.load_lora_weights( "lightx2v/Qwen-Image-Lightning", weight_name="Qwen-Image-Lightning-8steps-V1.1.safetensors" ) pipe.fuse_lora() pipe.transformer.__class__ = QwenImageTransformer2DModel pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3()) # dummy_mask = load_image("https://github.com/Trgtuan10/Image_storage/blob/main/mask_cat.png?raw=true") # # --- Ahead-of-time compilation --- # optimize_pipeline_(pipe, image=Image.new("RGB", (1328, 1328)), prompt="prompt", mask_image=dummy_mask) @spaces.GPU(duration=120) def infer(edit_images, prompt, negative_prompt="", seed=42, randomize_seed=False, strength=1.0, num_inference_steps=35, true_cfg_scale=4.0, progress=gr.Progress(track_tqdm=True)): image = edit_images["background"] mask = edit_images["layers"][0] if randomize_seed: seed = random.randint(0, MAX_SEED) # Generate image using Qwen pipeline result_image = pipe( prompt=prompt, negative_prompt=negative_prompt, image=image, mask_image=mask, strength=strength, num_inference_steps=num_inference_steps, true_cfg_scale=true_cfg_scale, generator=torch.Generator(device="cuda").manual_seed(seed) ).images[0] return result_image, seed examples = [ "change the hat to red", "make the background a beautiful sunset", "replace the object with a flower vase", ] css=""" #col-container { margin: 0 auto; max-width: 1000px; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.HTML("""
Qwen-Image Edit Logo

Inapint

""") gr.Markdown(""" Inpaint images with Qwen Image Edit. [Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series. This demo uses the [Qwen-Image-Lightning](https://huggingface.co/lightx2v/Qwen-Image-Lightning) LoRA with AoT compilation and FA3 for accelerated 8-step inference. Try on [Qwen Chat](https://chat.qwen.ai/), or [download model](https://huggingface.co/Qwen/Qwen-Image-Edit) to run locally with ComfyUI or diffusers. """) with gr.Row(): with gr.Column(): edit_image = gr.ImageEditor( label='Upload and draw mask for inpainting', type='pil', sources=["upload", "webcam"], image_mode='RGB', layers=False, brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"), height=600 ) prompt = gr.Text( label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt (e.g., 'change the hat to red')", container=False, ) negative_prompt = gr.Text( label="Negative Prompt", show_label=True, max_lines=1, placeholder="Enter what you don't want (optional)", container=False, value="" ) run_button = gr.Button("Run") result = gr.Image(label="Result", show_label=False) with gr.Accordion("Advanced Settings", open=False): seed = gr.Slider( label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, ) randomize_seed = gr.Checkbox(label="Randomize seed", value=True) with gr.Row(): strength = gr.Slider( label="Strength", minimum=0.0, maximum=2.0, step=0.1, value=1.0, info="Controls how much the inpainted region should change" ) true_cfg_scale = gr.Slider( label="True CFG Scale", minimum=1.0, maximum=20.0, step=0.5, value=1.0, info="Classifier-free guidance scale" ) num_inference_steps = gr.Slider( label="Number of inference steps", minimum=10, maximum=50, step=1, value=8, ) gr.on( triggers=[run_button.click, prompt.submit], fn = infer, inputs = [edit_image, prompt, negative_prompt, seed, randomize_seed, strength, num_inference_steps, true_cfg_scale], outputs = [result, seed] ) demo.launch()