linoyts's picture
linoyts HF Staff
Create app.py
8fc365a verified
raw
history blame
4.43 kB
import gradio as gr
import numpy as np
import spaces
import torch
import random
import os
from diffusers import QwenImageEditInpaintPipeline
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")
@spaces.GPU
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.Markdown(f"""# Qwen Image Edit Inpainting
Advanced image inpainting using Qwen's Image Edit model
[[model](https://huggingface.co/Qwen/Qwen-Image-Edit)] [[paper](https://arxiv.org/abs/2412.20710)]
""")
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()