Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	| 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 qwenimage.pipeline_qwen_image_edit 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 | |
| pipe = QwenImageEditInpaintPipeline.from_pretrained("Qwen/Qwen-Image-Edit", torch_dtype=torch.bfloat16).to("cuda") | |
| pipe.transformer.__class__ = QwenImageTransformer2DModel | |
| pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3()) | |
| # --- Ahead-of-time compilation --- | |
| optimize_pipeline_(pipe, image=Image.new("RGB", (1024, 1024)), prompt="prompt") | |
| 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(""" | |
| <div id="logo-title"> | |
| <img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_edit_logo.png" alt="Qwen-Image Edit Logo" width="400" style="display: block; margin: 0 auto;"> | |
| <h2 style="font-style: italic;color: #5b47d1;margin-top: -27px !important;margin-left: 133px;">Inapint</h2> | |
| </div> | |
| """) | |
| 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=4.0, | |
| info="Classifier-free guidance scale" | |
| ) | |
| num_inference_steps = gr.Slider( | |
| label="Number of inference steps", | |
| minimum=10, | |
| maximum=100, | |
| step=1, | |
| value=35, | |
| ) | |
| 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() | 
 
			
