Spaces:
Runtime error
Runtime error
feat: swap Image Edit tab to Qwen-Image-Edit 2511 (50 steps, proper instruction following)
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ import zipfile
|
|
| 10 |
from io import BytesIO
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
-
from diffusers import Flux2KleinPipeline, ZImagePipeline
|
| 14 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 15 |
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -115,6 +115,12 @@ zimage_pipe = ZImagePipeline.from_pretrained(
|
|
| 115 |
).to(device)
|
| 116 |
print("Z-Image Turbo loaded!")
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
print("Loading Florence-2 captioner...")
|
| 119 |
florence_processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
| 120 |
florence_model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -260,11 +266,10 @@ def face_swap(body_img, face_img, custom_prompt, nsfw_on, nsfw_str, swap_str,
|
|
| 260 |
|
| 261 |
|
| 262 |
# ===========================================================
|
| 263 |
-
# Tab 2: Image Edit
|
| 264 |
# ===========================================================
|
| 265 |
-
@spaces.GPU
|
| 266 |
-
def image_edit(ref_images, prompt,
|
| 267 |
-
seed, randomize_seed, steps, guidance,
|
| 268 |
progress=gr.Progress(track_tqdm=True)):
|
| 269 |
gc.collect(); torch.cuda.empty_cache()
|
| 270 |
try:
|
|
@@ -274,15 +279,18 @@ def image_edit(ref_images, prompt, nsfw_on, nsfw_str,
|
|
| 274 |
if not prompt or not prompt.strip():
|
| 275 |
raise gr.Error("Enter an edit prompt!")
|
| 276 |
|
| 277 |
-
loras = []
|
| 278 |
-
if nsfw_on:
|
| 279 |
-
loras.append(("nsfw", nsfw_str))
|
| 280 |
-
activate_loras(loras)
|
| 281 |
-
|
| 282 |
if randomize_seed:
|
| 283 |
seed = random.randint(0, MAX_SEED)
|
| 284 |
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
return result, seed
|
| 287 |
finally:
|
| 288 |
gc.collect(); torch.cuda.empty_cache()
|
|
@@ -443,18 +451,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 443 |
|
| 444 |
# ==================== IMAGE EDIT ====================
|
| 445 |
with gr.TabItem("Image Edit"):
|
| 446 |
-
gr.Markdown("
|
| 447 |
with gr.Row():
|
| 448 |
with gr.Column():
|
| 449 |
-
ie_images = gr.Gallery(label="Input Images", type="filepath", columns=2, rows=1, height=280)
|
| 450 |
ie_template = gr.Dropdown(list(EDIT_TEMPLATES.keys()), value="Custom", label="Preset")
|
| 451 |
-
ie_prompt = gr.Textbox(label="Edit
|
| 452 |
-
|
| 453 |
-
ie_nsfw = gr.Checkbox(value=True, label="NSFW LoRA")
|
| 454 |
-
ie_nsfw_str = gr.Slider(0.0, 1.5, value=0.6, step=0.05, label="NSFW strength")
|
| 455 |
-
with gr.Row():
|
| 456 |
-
ie_steps = gr.Slider(1, 50, value=4, step=1, label="Steps")
|
| 457 |
-
ie_guidance = gr.Slider(0.0, 10.0, value=1.0, step=0.1, label="Guidance")
|
| 458 |
ie_seed = gr.Slider(0, MAX_SEED, value=42, step=1, label="Seed")
|
| 459 |
ie_rand = gr.Checkbox(value=True, label="Randomize seed")
|
| 460 |
ie_btn = gr.Button("Edit", variant="primary", size="lg")
|
|
@@ -464,7 +467,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 464 |
|
| 465 |
ie_template.change(fn=lambda t: EDIT_TEMPLATES.get(t, ""), inputs=[ie_template], outputs=[ie_prompt])
|
| 466 |
ie_btn.click(fn=image_edit,
|
| 467 |
-
inputs=[ie_images, ie_prompt,
|
| 468 |
outputs=[ie_out, ie_seed_out])
|
| 469 |
|
| 470 |
# ==================== POSE VARIATIONS ====================
|
|
|
|
| 10 |
from io import BytesIO
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
+
from diffusers import Flux2KleinPipeline, ZImagePipeline, QwenImageEditPlusPipeline
|
| 14 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 15 |
|
| 16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 115 |
).to(device)
|
| 116 |
print("Z-Image Turbo loaded!")
|
| 117 |
|
| 118 |
+
print("Loading Qwen-Image-Edit 2511...")
|
| 119 |
+
qwen_pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 120 |
+
"Qwen/Qwen-Image-Edit-2511", torch_dtype=torch.bfloat16,
|
| 121 |
+
).to(device)
|
| 122 |
+
print("Qwen loaded!")
|
| 123 |
+
|
| 124 |
print("Loading Florence-2 captioner...")
|
| 125 |
florence_processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
| 126 |
florence_model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 266 |
|
| 267 |
|
| 268 |
# ===========================================================
|
| 269 |
+
# Tab 2: Image Edit (Qwen-Image-Edit 2511)
|
| 270 |
# ===========================================================
|
| 271 |
+
@spaces.GPU(duration=120)
|
| 272 |
+
def image_edit(ref_images, prompt, seed, randomize_seed, steps,
|
|
|
|
| 273 |
progress=gr.Progress(track_tqdm=True)):
|
| 274 |
gc.collect(); torch.cuda.empty_cache()
|
| 275 |
try:
|
|
|
|
| 279 |
if not prompt or not prompt.strip():
|
| 280 |
raise gr.Error("Enter an edit prompt!")
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
if randomize_seed:
|
| 283 |
seed = random.randint(0, MAX_SEED)
|
| 284 |
|
| 285 |
+
w, h = update_dimensions(images[0])
|
| 286 |
+
processed = [img.resize((w, h), Image.LANCZOS).convert("RGB") for img in images]
|
| 287 |
+
|
| 288 |
+
result = qwen_pipe(
|
| 289 |
+
image=processed,
|
| 290 |
+
prompt=prompt.strip(),
|
| 291 |
+
num_inference_steps=steps,
|
| 292 |
+
generator=torch.Generator(device=device).manual_seed(seed),
|
| 293 |
+
).images[0]
|
| 294 |
return result, seed
|
| 295 |
finally:
|
| 296 |
gc.collect(); torch.cuda.empty_cache()
|
|
|
|
| 451 |
|
| 452 |
# ==================== IMAGE EDIT ====================
|
| 453 |
with gr.TabItem("Image Edit"):
|
| 454 |
+
gr.Markdown("Powered by **Qwen-Image-Edit 2511** β instruction-based editing at 50 steps. Supports multi-reference (upload multiple images). No LoRA needed.")
|
| 455 |
with gr.Row():
|
| 456 |
with gr.Column():
|
| 457 |
+
ie_images = gr.Gallery(label="Input Images (multi-reference supported)", type="filepath", columns=2, rows=1, height=280)
|
| 458 |
ie_template = gr.Dropdown(list(EDIT_TEMPLATES.keys()), value="Custom", label="Preset")
|
| 459 |
+
ie_prompt = gr.Textbox(label="Edit instruction", lines=3, placeholder="e.g. remove clothing, add tattoos, change background...")
|
| 460 |
+
ie_steps = gr.Slider(10, 100, value=50, step=5, label="Steps (50 recommended)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
ie_seed = gr.Slider(0, MAX_SEED, value=42, step=1, label="Seed")
|
| 462 |
ie_rand = gr.Checkbox(value=True, label="Randomize seed")
|
| 463 |
ie_btn = gr.Button("Edit", variant="primary", size="lg")
|
|
|
|
| 467 |
|
| 468 |
ie_template.change(fn=lambda t: EDIT_TEMPLATES.get(t, ""), inputs=[ie_template], outputs=[ie_prompt])
|
| 469 |
ie_btn.click(fn=image_edit,
|
| 470 |
+
inputs=[ie_images, ie_prompt, ie_seed, ie_rand, ie_steps],
|
| 471 |
outputs=[ie_out, ie_seed_out])
|
| 472 |
|
| 473 |
# ==================== POSE VARIATIONS ====================
|