Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,9 @@ import os
|
|
| 8 |
# from diffusers import QwenImageEditInpaintPipeline
|
| 9 |
from optimization import optimize_pipeline_
|
| 10 |
from diffusers.utils import load_image
|
| 11 |
-
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
|
| 15 |
import math
|
| 16 |
from huggingface_hub import InferenceClient
|
| 17 |
|
|
@@ -148,54 +147,27 @@ def use_output_as_input(output_image):
|
|
| 148 |
return gr.update(value=output_image[1])
|
| 149 |
return gr.update()
|
| 150 |
|
| 151 |
-
# Initialize Qwen Image Edit pipeline
|
| 152 |
-
# Scheduler configuration for Lightning
|
| 153 |
-
scheduler_config = {
|
| 154 |
-
"base_image_seq_len": 256,
|
| 155 |
-
"base_shift": math.log(3),
|
| 156 |
-
"invert_sigmas": False,
|
| 157 |
-
"max_image_seq_len": 8192,
|
| 158 |
-
"max_shift": math.log(3),
|
| 159 |
-
"num_train_timesteps": 1000,
|
| 160 |
-
"shift": 1.0,
|
| 161 |
-
"shift_terminal": None,
|
| 162 |
-
"stochastic_sampling": False,
|
| 163 |
-
"time_shift_type": "exponential",
|
| 164 |
-
"use_beta_sigmas": False,
|
| 165 |
-
"use_dynamic_shifting": True,
|
| 166 |
-
"use_exponential_sigmas": False,
|
| 167 |
-
"use_karras_sigmas": False,
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
# Initialize scheduler with Lightning config
|
| 171 |
-
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
pipe = QwenImageEditInpaintPipeline.from_pretrained("Qwen/Qwen-Image-Edit", scheduler=scheduler, torch_dtype=torch.bfloat16).to("cuda")
|
| 175 |
-
pipe.load_lora_weights(
|
| 176 |
-
"lightx2v/Qwen-Image-Lightning",
|
| 177 |
-
weight_name="Qwen-Image-Lightning-8steps-V1.1.safetensors"
|
| 178 |
-
)
|
| 179 |
-
pipe.fuse_lora()
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
|
|
|
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
# # --- Ahead-of-time compilation ---
|
| 188 |
-
# optimize_pipeline_(pipe, image=Image.new("RGB", (1328, 1328)), prompt="prompt", mask_image=dummy_mask)
|
| 189 |
|
| 190 |
@spaces.GPU(duration=120)
|
| 191 |
def infer(edit_images,
|
| 192 |
prompt,
|
| 193 |
-
negative_prompt="",
|
| 194 |
seed=42,
|
| 195 |
randomize_seed=False,
|
| 196 |
strength=1.0,
|
| 197 |
-
num_inference_steps=
|
| 198 |
-
true_cfg_scale=
|
| 199 |
rewrite_prompt=True,
|
| 200 |
progress=gr.Progress(track_tqdm=True)):
|
| 201 |
|
|
@@ -213,9 +185,9 @@ def infer(edit_images,
|
|
| 213 |
result_image = pipe(
|
| 214 |
prompt=prompt,
|
| 215 |
negative_prompt=negative_prompt,
|
| 216 |
-
|
| 217 |
-
mask_image
|
| 218 |
-
|
| 219 |
num_inference_steps=num_inference_steps,
|
| 220 |
true_cfg_scale=true_cfg_scale,
|
| 221 |
generator=torch.Generator(device="cuda").manual_seed(seed)
|
|
@@ -244,22 +216,11 @@ css = """
|
|
| 244 |
"""
|
| 245 |
|
| 246 |
|
| 247 |
-
with gr.Blocks(css=css) as demo:
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
<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;">
|
| 253 |
-
<h2 style="font-style: italic;color: #5b47d1;margin-top: -27px !important;margin-left: 133px;">Inpaint</h2>
|
| 254 |
-
</div>
|
| 255 |
-
""")
|
| 256 |
-
gr.Markdown("""
|
| 257 |
-
|
| 258 |
-
Inpaint images with Qwen Image Edit. [Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series.
|
| 259 |
-
|
| 260 |
-
This demo uses the [Qwen-Image-Lightning](https://huggingface.co/lightx2v/Qwen-Image-Lightning) LoRA with FA3 for accelerated 8-step inference.
|
| 261 |
-
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.
|
| 262 |
-
""")
|
| 263 |
with gr.Row():
|
| 264 |
with gr.Column():
|
| 265 |
edit_image = gr.ImageEditor(
|
|
@@ -309,7 +270,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 309 |
|
| 310 |
with gr.Row():
|
| 311 |
strength = gr.Slider(
|
| 312 |
-
label="
|
| 313 |
minimum=0.0,
|
| 314 |
maximum=1.0,
|
| 315 |
step=0.1,
|
|
@@ -322,7 +283,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 322 |
minimum=1.0,
|
| 323 |
maximum=10.0,
|
| 324 |
step=0.5,
|
| 325 |
-
value=
|
| 326 |
info="Classifier-free guidance scale"
|
| 327 |
)
|
| 328 |
|
|
@@ -331,7 +292,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 331 |
minimum=1,
|
| 332 |
maximum=50,
|
| 333 |
step=1,
|
| 334 |
-
value=
|
| 335 |
)
|
| 336 |
rewrite_prompt = gr.Checkbox(
|
| 337 |
label="Enhance prompt (using HF Inference)",
|
|
|
|
| 8 |
# from diffusers import QwenImageEditInpaintPipeline
|
| 9 |
from optimization import optimize_pipeline_
|
| 10 |
from diffusers.utils import load_image
|
| 11 |
+
|
| 12 |
+
from diffusers import QwenImageControlNetModel, QwenImageControlNetInpaintPipeline
|
| 13 |
+
|
|
|
|
| 14 |
import math
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
|
|
|
|
| 147 |
return gr.update(value=output_image[1])
|
| 148 |
return gr.update()
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
base_model = "Qwen/Qwen-Image"
|
| 152 |
+
controlnet_model = "InstantX/Qwen-Image-ControlNet-Inpainting"
|
| 153 |
|
| 154 |
+
controlnet = QwenImageControlNetModel.from_pretrained(controlnet_model, torch_dtype=torch.bfloat16)
|
| 155 |
|
| 156 |
+
pipe = QwenImageControlNetInpaintPipeline.from_pretrained(
|
| 157 |
+
base_model, controlnet=controlnet, torch_dtype=torch.bfloat16
|
| 158 |
+
)
|
| 159 |
+
pipe.to("cuda")
|
| 160 |
|
|
|
|
|
|
|
| 161 |
|
| 162 |
@spaces.GPU(duration=120)
|
| 163 |
def infer(edit_images,
|
| 164 |
prompt,
|
| 165 |
+
negative_prompt=" ",
|
| 166 |
seed=42,
|
| 167 |
randomize_seed=False,
|
| 168 |
strength=1.0,
|
| 169 |
+
num_inference_steps=30,
|
| 170 |
+
true_cfg_scale=4.0,
|
| 171 |
rewrite_prompt=True,
|
| 172 |
progress=gr.Progress(track_tqdm=True)):
|
| 173 |
|
|
|
|
| 185 |
result_image = pipe(
|
| 186 |
prompt=prompt,
|
| 187 |
negative_prompt=negative_prompt,
|
| 188 |
+
control_image=image,
|
| 189 |
+
control_mask=mask_image,
|
| 190 |
+
controlnet_conditioning_scale=strength,
|
| 191 |
num_inference_steps=num_inference_steps,
|
| 192 |
true_cfg_scale=true_cfg_scale,
|
| 193 |
generator=torch.Generator(device="cuda").manual_seed(seed)
|
|
|
|
| 216 |
"""
|
| 217 |
|
| 218 |
|
| 219 |
+
with gr.Blocks(css=css, theme=gr.themes.Citrus()) as demo:
|
| 220 |
+
gr.HTML("<h1 style='text-align: center'>Qwen-Image with InstantX Inpainting ControlNet</style>")
|
| 221 |
+
gr.Markdown(
|
| 222 |
+
"Generate images with the [InstantX/Qwen-Image-ControlNet-Inpainting](https://huggingface.co/InstantX/Qwen-Image-ControlNet-Inpainting) that takes depth, pose and canny conditionings"
|
| 223 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
with gr.Row():
|
| 225 |
with gr.Column():
|
| 226 |
edit_image = gr.ImageEditor(
|
|
|
|
| 270 |
|
| 271 |
with gr.Row():
|
| 272 |
strength = gr.Slider(
|
| 273 |
+
label="Conditioning Scale",
|
| 274 |
minimum=0.0,
|
| 275 |
maximum=1.0,
|
| 276 |
step=0.1,
|
|
|
|
| 283 |
minimum=1.0,
|
| 284 |
maximum=10.0,
|
| 285 |
step=0.5,
|
| 286 |
+
value=4.0,
|
| 287 |
info="Classifier-free guidance scale"
|
| 288 |
)
|
| 289 |
|
|
|
|
| 292 |
minimum=1,
|
| 293 |
maximum=50,
|
| 294 |
step=1,
|
| 295 |
+
value=30,
|
| 296 |
)
|
| 297 |
rewrite_prompt = gr.Checkbox(
|
| 298 |
label="Enhance prompt (using HF Inference)",
|