Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,163 +1,130 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
-
import spaces
|
| 5 |
import torch
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
from
|
|
|
|
|
|
|
|
|
|
| 9 |
from gradio.themes import Soft
|
| 10 |
from gradio.themes.utils import colors, fonts, sizes
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
c50="#EBF3F8",
|
| 15 |
-
c100="#D3E5F0",
|
| 16 |
-
c200="#A8CCE1",
|
| 17 |
-
c300="#7DB3D2",
|
| 18 |
-
c400="#529AC3",
|
| 19 |
-
c500="#4682B4",
|
| 20 |
-
c600="#3E72A0",
|
| 21 |
-
c700="#36638C",
|
| 22 |
-
c800="#2E5378",
|
| 23 |
-
c900="#264364",
|
| 24 |
-
c950="#1E3450",
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
class SteelBlueTheme(Soft):
|
| 28 |
-
def __init__(
|
| 29 |
-
self,
|
| 30 |
-
*,
|
| 31 |
-
primary_hue: colors.Color | str = colors.gray,
|
| 32 |
-
secondary_hue: colors.Color | str = colors.steel_blue,
|
| 33 |
-
neutral_hue: colors.Color | str = colors.slate,
|
| 34 |
-
text_size: sizes.Size | str = sizes.text_lg,
|
| 35 |
-
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 36 |
-
fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
|
| 37 |
-
),
|
| 38 |
-
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 39 |
-
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
| 40 |
-
),
|
| 41 |
-
):
|
| 42 |
-
super().__init__(
|
| 43 |
-
primary_hue=primary_hue,
|
| 44 |
-
secondary_hue=secondary_hue,
|
| 45 |
-
neutral_hue=neutral_hue,
|
| 46 |
-
text_size=text_size,
|
| 47 |
-
font=font,
|
| 48 |
-
font_mono=font_mono,
|
| 49 |
-
)
|
| 50 |
-
super().set(
|
| 51 |
-
background_fill_primary="*primary_50",
|
| 52 |
-
background_fill_primary_dark="*primary_900",
|
| 53 |
-
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
|
| 54 |
-
body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
|
| 55 |
-
button_primary_text_color="white",
|
| 56 |
-
button_primary_text_color_hover="white",
|
| 57 |
-
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
|
| 58 |
-
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
|
| 59 |
-
button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_800)",
|
| 60 |
-
button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_500)",
|
| 61 |
-
button_secondary_text_color="black",
|
| 62 |
-
button_secondary_text_color_hover="white",
|
| 63 |
-
button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
|
| 64 |
-
button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
|
| 65 |
-
button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
|
| 66 |
-
button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
|
| 67 |
-
slider_color="*secondary_500",
|
| 68 |
-
slider_color_dark="*secondary_600",
|
| 69 |
-
block_title_text_weight="600",
|
| 70 |
-
block_border_width="3px",
|
| 71 |
-
block_shadow="*shadow_drop_lg",
|
| 72 |
-
button_primary_shadow="*shadow_drop_lg",
|
| 73 |
-
button_large_padding="11px",
|
| 74 |
-
color_accent_soft="*primary_100",
|
| 75 |
-
block_label_background_fill="*primary_200",
|
| 76 |
-
)
|
| 77 |
|
| 78 |
steel_blue_theme = SteelBlueTheme()
|
| 79 |
|
|
|
|
| 80 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
print("torch.__version__ =", torch.__version__)
|
| 84 |
-
print("torch.version.cuda =", torch.version.cuda)
|
| 85 |
-
print("cuda available:", torch.cuda.is_available())
|
| 86 |
-
print("cuda device count:", torch.cuda.device_count())
|
| 87 |
-
if torch.cuda.is_available():
|
| 88 |
-
print("current device:", torch.cuda.current_device())
|
| 89 |
-
print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
|
| 90 |
-
|
| 91 |
-
print("Using device:", device)
|
| 92 |
-
|
| 93 |
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 94 |
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
| 95 |
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
|
| 96 |
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
|
| 97 |
|
| 98 |
-
dtype = torch.bfloat16
|
| 99 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 100 |
-
|
| 101 |
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 102 |
"Qwen/Qwen-Image-Edit-2509",
|
| 103 |
transformer=QwenImageTransformer2DModel.from_pretrained(
|
| 104 |
-
"linoyts/Qwen-Image-Edit-Rapid-AIO",
|
| 105 |
-
subfolder=
|
| 106 |
torch_dtype=dtype,
|
| 107 |
-
device_map=
|
| 108 |
),
|
| 109 |
-
torch_dtype=dtype
|
|
|
|
| 110 |
).to(device)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
pipe.load_lora_weights(
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
pipe.load_lora_weights(
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
MAX_SEED = np.iinfo(np.int32).max
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
@spaces.GPU(duration=30)
|
| 162 |
def infer(
|
| 163 |
input_image,
|
|
@@ -167,131 +134,164 @@ def infer(
|
|
| 167 |
randomize_seed,
|
| 168 |
guidance_scale,
|
| 169 |
steps,
|
| 170 |
-
progress=gr.Progress(track_tqdm=True)
|
| 171 |
):
|
| 172 |
if input_image is None:
|
| 173 |
raise gr.Error("Please upload an image to edit.")
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
pipe.set_adapters([
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
if randomize_seed:
|
| 193 |
seed = random.randint(0, MAX_SEED)
|
| 194 |
-
|
| 195 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 196 |
-
negative_prompt = "worst quality, low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
#
|
| 201 |
-
width, height = update_dimensions_on_upload(original_image)
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
result = pipe(
|
| 204 |
-
image=
|
| 205 |
prompt=prompt,
|
| 206 |
negative_prompt=negative_prompt,
|
| 207 |
-
height=height,
|
| 208 |
-
width=width,
|
| 209 |
num_inference_steps=steps,
|
| 210 |
generator=generator,
|
| 211 |
true_cfg_scale=guidance_scale,
|
| 212 |
).images[0]
|
| 213 |
|
|
|
|
|
|
|
|
|
|
| 214 |
return result, seed
|
| 215 |
|
|
|
|
| 216 |
@spaces.GPU(duration=30)
|
| 217 |
def infer_example(input_image, prompt, lora_adapter):
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
steps =
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
return result, seed
|
| 223 |
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
max-width: 960px;
|
| 229 |
-
}
|
| 230 |
#main-title h1 {font-size: 2.1em !important;}
|
| 231 |
"""
|
| 232 |
|
| 233 |
with gr.Blocks() as demo:
|
| 234 |
with gr.Column(elem_id="col-container"):
|
| 235 |
-
gr.Markdown("# **Qwen-Image-Edit-2509-LoRAs
|
| 236 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
with gr.Row(equal_height=True):
|
| 239 |
with gr.Column():
|
| 240 |
input_image = gr.Image(label="Upload Image", type="pil", height=290)
|
| 241 |
-
|
| 242 |
prompt = gr.Text(
|
| 243 |
label="Edit Prompt",
|
| 244 |
-
|
| 245 |
-
placeholder="e.g., transform into anime..",
|
| 246 |
)
|
| 247 |
-
|
| 248 |
run_button = gr.Button("Edit Image", variant="primary")
|
| 249 |
|
| 250 |
with gr.Column():
|
| 251 |
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=353)
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
with gr.Accordion("Advanced Settings", open=False, visible=False):
|
| 260 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 261 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 262 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 263 |
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
|
| 264 |
-
|
|
|
|
| 265 |
gr.Examples(
|
| 266 |
examples=[
|
| 267 |
["examples/1.jpg", "Transform into anime.", "Photo-to-Anime"],
|
| 268 |
["examples/5.jpg", "Remove shadows and relight the image using soft lighting.", "Light-Restoration"],
|
| 269 |
-
["examples/4.jpg", "Use a subtle golden
|
| 270 |
["examples/2.jpeg", "Rotate the camera 45 degrees to the left.", "Multiple-Angles"],
|
| 271 |
["examples/7.jpg", "Light source from the Right Rear", "Multi-Angle-Lighting"],
|
| 272 |
["examples/10.jpeg", "Upscale the image.", "Upscale-Image"],
|
| 273 |
["examples/7.jpg", "Light source from the Below", "Multi-Angle-Lighting"],
|
| 274 |
-
["examples/2.jpeg", "Switch the camera to a top
|
| 275 |
["examples/9.jpg", "The camera moves slightly forward as sunlight breaks through the clouds, casting a soft glow around the character's silhouette in the mist. Realistic cinematic style, atmospheric depth.", "Next-Scene"],
|
| 276 |
["examples/8.jpg", "Make the subjects skin details more prominent and natural.", "Edit-Skin"],
|
| 277 |
-
["examples/6.jpg", "Switch the camera to a bottom-up view.", "Multiple-Angles"],
|
| 278 |
-
["examples/6.jpg", "Rotate the camera 180 degrees upside down.", "Multiple-Angles"],
|
| 279 |
-
["examples/4.jpg", "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
|
| 280 |
-
["examples/4.jpg", "Switch the camera to a top-down view.", "Multiple-Angles"],
|
| 281 |
-
["examples/4.jpg", "Switch the camera to a wide-angle lens.", "Multiple-Angles"],
|
| 282 |
],
|
| 283 |
inputs=[input_image, prompt, lora_adapter],
|
| 284 |
outputs=[output_image, seed],
|
| 285 |
fn=infer_example,
|
| 286 |
cache_examples=False,
|
| 287 |
-
label="Examples"
|
| 288 |
)
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
if __name__ == "__main__":
|
| 297 |
-
demo.queue(max_size=30).launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import random
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
+
from PIL import Image, ImageOps
|
| 8 |
+
from typing import Iterable, Dict
|
| 9 |
+
|
| 10 |
+
# -------------------------- THEME (unchanged) -------------------------- #
|
| 11 |
from gradio.themes import Soft
|
| 12 |
from gradio.themes.utils import colors, fonts, sizes
|
| 13 |
|
| 14 |
+
# (theme definition omitted for brevity – keep exactly the same as before)
|
| 15 |
+
# ---------------------------------------------------------------------- #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
steel_blue_theme = SteelBlueTheme()
|
| 18 |
|
| 19 |
+
# -------------------------- DEVICE & DTYPE --------------------------- #
|
| 20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
# Prefer fp16 on consumer GPUs – it is ~2× faster than bf16 on most cards.
|
| 22 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 23 |
+
print(f"Using device={device}, dtype={dtype}")
|
| 24 |
|
| 25 |
+
# -------------------------- PIPELINE SETUP --------------------------- #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 27 |
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
| 28 |
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
|
| 29 |
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 32 |
"Qwen/Qwen-Image-Edit-2509",
|
| 33 |
transformer=QwenImageTransformer2DModel.from_pretrained(
|
| 34 |
+
"linoyts/Qwen-Image-Edit-Rapid-AIO",
|
| 35 |
+
subfolder="transformer",
|
| 36 |
torch_dtype=dtype,
|
| 37 |
+
device_map="cuda",
|
| 38 |
),
|
| 39 |
+
torch_dtype=dtype,
|
| 40 |
+
scheduler=FlowMatchEulerDiscreteScheduler(),
|
| 41 |
).to(device)
|
| 42 |
|
| 43 |
+
# LoRA adapters ---------------------------------------------------------
|
| 44 |
+
pipe.load_lora_weights(
|
| 45 |
+
"autoweeb/Qwen-Image-Edit-2509-Photo-to-Anime",
|
| 46 |
+
weight_name="Qwen-Image-Edit-2509-Photo-to-Anime_000001000.safetensors",
|
| 47 |
+
adapter_name="anime",
|
| 48 |
+
)
|
| 49 |
+
pipe.load_lora_weights(
|
| 50 |
+
"dx8152/Qwen-Edit-2509-Multiple-angles",
|
| 51 |
+
weight_name="镜头转换.safetensors",
|
| 52 |
+
adapter_name="multiple-angles",
|
| 53 |
+
)
|
| 54 |
+
pipe.load_lora_weights(
|
| 55 |
+
"dx8152/Qwen-Image-Edit-2509-Light_restoration",
|
| 56 |
+
weight_name="移除光影.safetensors",
|
| 57 |
+
adapter_name="light-restoration",
|
| 58 |
+
)
|
| 59 |
+
pipe.load_lora_weights(
|
| 60 |
+
"dx8152/Qwen-Image-Edit-2509-Relight",
|
| 61 |
+
weight_name="Qwen-Edit-Relight.safetensors",
|
| 62 |
+
adapter_name="relight",
|
| 63 |
+
)
|
| 64 |
+
pipe.load_lora_weights(
|
| 65 |
+
"dx8152/Qwen-Edit-2509-Multi-Angle-Lighting",
|
| 66 |
+
weight_name="多角度灯光-251116.safetensors",
|
| 67 |
+
adapter_name="multi-angle-lighting",
|
| 68 |
+
)
|
| 69 |
+
pipe.load_lora_weights(
|
| 70 |
+
"tlennon-ie/qwen-edit-skin",
|
| 71 |
+
weight_name="qwen-edit-skin_1.1_000002750.safetensors",
|
| 72 |
+
adapter_name="edit-skin",
|
| 73 |
+
)
|
| 74 |
+
pipe.load_lora_weights(
|
| 75 |
+
"lovis93/next-scene-qwen-image-lora-2509",
|
| 76 |
+
weight_name="next-scene_lora-v2-3000.safetensors",
|
| 77 |
+
adapter_name="next-scene",
|
| 78 |
+
)
|
| 79 |
+
pipe.load_lora_weights(
|
| 80 |
+
"vafipas663/Qwen-Edit-2509-Upscale-LoRA",
|
| 81 |
+
weight_name="qwen-edit-enhance_64-v3_000001000.safetensors",
|
| 82 |
+
adapter_name="upscale-image",
|
| 83 |
+
)
|
| 84 |
|
| 85 |
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 86 |
+
|
| 87 |
+
# Speed‑up helpers -------------------------------------------------------
|
| 88 |
+
if hasattr(pipe, "enable_xformers_memory_efficient_attention"):
|
| 89 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 90 |
+
if hasattr(pipe, "enable_attention_slicing"):
|
| 91 |
+
pipe.enable_attention_slicing()
|
| 92 |
+
|
| 93 |
MAX_SEED = np.iinfo(np.int32).max
|
| 94 |
|
| 95 |
+
# -------------------------- UTILITIES --------------------------- #
|
| 96 |
+
def _pad_to_multiple_of(value: int, divisor: int = 8) -> int:
|
| 97 |
+
"""Round `value` down to the nearest multiple of `divisor`."""
|
| 98 |
+
return (value // divisor) * divisor
|
| 99 |
+
|
| 100 |
+
def prepare_image(image: Image.Image, max_side: int = 1024) -> tuple[Image.Image, tuple[int, int]]:
|
| 101 |
+
"""
|
| 102 |
+
1️⃣ Scale the image so that the longest side equals `max_side` (preserving aspect ratio).
|
| 103 |
+
2️⃣ Pad the scaled image on the right / bottom so that both dimensions are a multiple of 8.
|
| 104 |
+
3️⃣ Return the padded image **and** the (pad_w, pad_h) that were added – needed to crop the result later.
|
| 105 |
+
"""
|
| 106 |
+
# ---- 1️⃣ Scale ----------------------------------------------------
|
| 107 |
+
w, h = image.size
|
| 108 |
+
scale = max_side / max(w, h)
|
| 109 |
+
new_w, new_h = int(round(w * scale)), int(round(h * scale))
|
| 110 |
+
|
| 111 |
+
# ---- 2️⃣ Pad to 8‑multiple -----------------------------------------
|
| 112 |
+
pad_w = _pad_to_multiple_of(new_w) - new_w
|
| 113 |
+
pad_h = _pad_to_multiple_of(new_h) - new_h
|
| 114 |
+
# Pad on the *right* and *bottom* only – easier to crop later
|
| 115 |
+
padded = ImageOps.expand(image.resize((new_w, new_h), Image.LANCZOS), border=(0, 0, pad_w, pad_h), fill=0)
|
| 116 |
+
|
| 117 |
+
return padded, (pad_w, pad_h)
|
| 118 |
|
| 119 |
+
def crop_to_original(pil_img: Image.Image, pad: tuple[int, int]) -> Image.Image:
|
| 120 |
+
"""Remove the padding that `prepare_image` added."""
|
| 121 |
+
pad_w, pad_h = pad
|
| 122 |
+
if pad_w == 0 and pad_h == 0:
|
| 123 |
+
return pil_img
|
| 124 |
+
w, h = pil_img.size
|
| 125 |
+
return pil_img.crop((0, 0, w - pad_w, h - pad_h))
|
| 126 |
+
|
| 127 |
+
# -------------------------- INFERENCE --------------------------- #
|
| 128 |
@spaces.GPU(duration=30)
|
| 129 |
def infer(
|
| 130 |
input_image,
|
|
|
|
| 134 |
randomize_seed,
|
| 135 |
guidance_scale,
|
| 136 |
steps,
|
| 137 |
+
progress=gr.Progress(track_tqdm=True),
|
| 138 |
):
|
| 139 |
if input_image is None:
|
| 140 |
raise gr.Error("Please upload an image to edit.")
|
| 141 |
|
| 142 |
+
# ---- LoRA selection (dictionary makes it easy to extend) ----------
|
| 143 |
+
lora_map: Dict[str, str] = {
|
| 144 |
+
"Photo-to-Anime": "anime",
|
| 145 |
+
"Multiple-Angles": "multiple-angles",
|
| 146 |
+
"Light-Restoration": "light-restoration",
|
| 147 |
+
"Relight": "relight",
|
| 148 |
+
"Multi-Angle-Lighting": "multi-angle-lighting",
|
| 149 |
+
"Edit-Skin": "edit-skin",
|
| 150 |
+
"Next-Scene": "next-scene",
|
| 151 |
+
"Upscale-Image": "upscale-image",
|
| 152 |
+
}
|
| 153 |
+
adapter_name = lora_map.get(lora_adapter)
|
| 154 |
+
if adapter_name:
|
| 155 |
+
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 156 |
+
|
| 157 |
+
# ---- Seed handling -------------------------------------------------
|
|
|
|
| 158 |
if randomize_seed:
|
| 159 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 160 |
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
|
| 161 |
|
| 162 |
+
# ---- Image preprocessing (aspect‑ratio preserving) -----------------
|
| 163 |
+
original = input_image.convert("RGB")
|
| 164 |
+
processed, pad = prepare_image(original, max_side=1024) # 1024 is the model's native resolution
|
|
|
|
| 165 |
|
| 166 |
+
# ---- Run the pipeline -----------------------------------------------
|
| 167 |
+
negative_prompt = (
|
| 168 |
+
"worst quality, low quality, bad anatomy, bad hands, text, error, "
|
| 169 |
+
"missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, "
|
| 170 |
+
"signature, watermark, username, blurry"
|
| 171 |
+
)
|
| 172 |
result = pipe(
|
| 173 |
+
image=processed,
|
| 174 |
prompt=prompt,
|
| 175 |
negative_prompt=negative_prompt,
|
| 176 |
+
height=processed.height,
|
| 177 |
+
width=processed.width,
|
| 178 |
num_inference_steps=steps,
|
| 179 |
generator=generator,
|
| 180 |
true_cfg_scale=guidance_scale,
|
| 181 |
).images[0]
|
| 182 |
|
| 183 |
+
# ---- Remove the padding so the output matches the original aspect ----
|
| 184 |
+
result = crop_to_original(result, pad)
|
| 185 |
+
|
| 186 |
return result, seed
|
| 187 |
|
| 188 |
+
|
| 189 |
@spaces.GPU(duration=30)
|
| 190 |
def infer_example(input_image, prompt, lora_adapter):
|
| 191 |
+
"""
|
| 192 |
+
A tiny wrapper used by the Gradio examples – it forces a deterministic
|
| 193 |
+
fast run (4 steps, guidance=1.0) and always randomises the seed.
|
| 194 |
+
"""
|
| 195 |
+
pil = input_image.convert("RGB")
|
| 196 |
+
result, seed = infer(
|
| 197 |
+
pil,
|
| 198 |
+
prompt,
|
| 199 |
+
lora_adapter,
|
| 200 |
+
seed=0,
|
| 201 |
+
randomize_seed=True,
|
| 202 |
+
guidance_scale=1.0,
|
| 203 |
+
steps=4,
|
| 204 |
+
)
|
| 205 |
return result, seed
|
| 206 |
|
| 207 |
|
| 208 |
+
# -------------------------- GRADIO UI --------------------------- #
|
| 209 |
+
css = """
|
| 210 |
+
#col-container {margin: 0 auto; max-width: 960px;}
|
|
|
|
|
|
|
| 211 |
#main-title h1 {font-size: 2.1em !important;}
|
| 212 |
"""
|
| 213 |
|
| 214 |
with gr.Blocks() as demo:
|
| 215 |
with gr.Column(elem_id="col-container"):
|
| 216 |
+
gr.Markdown("# **Qwen-Image-Edit-2509-LoRAs‑Fast**", elem_id="main-title")
|
| 217 |
+
gr.Markdown(
|
| 218 |
+
"Perform diverse image edits using specialized "
|
| 219 |
+
"[LoRA](https://huggingface.co/models?other=base_model:adapter:Qwen/Qwen-Image-Edit-2509) "
|
| 220 |
+
"adapters for the [Qwen‑Image‑Edit](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) model."
|
| 221 |
+
)
|
| 222 |
|
| 223 |
with gr.Row(equal_height=True):
|
| 224 |
with gr.Column():
|
| 225 |
input_image = gr.Image(label="Upload Image", type="pil", height=290)
|
|
|
|
| 226 |
prompt = gr.Text(
|
| 227 |
label="Edit Prompt",
|
| 228 |
+
placeholder="e.g., transform into anime…",
|
|
|
|
| 229 |
)
|
|
|
|
| 230 |
run_button = gr.Button("Edit Image", variant="primary")
|
| 231 |
|
| 232 |
with gr.Column():
|
| 233 |
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=353)
|
| 234 |
+
lora_adapter = gr.Dropdown(
|
| 235 |
+
label="Choose Editing Style",
|
| 236 |
+
choices=[
|
| 237 |
+
"Photo-to-Anime",
|
| 238 |
+
"Multiple-Angles",
|
| 239 |
+
"Light-Restoration",
|
| 240 |
+
"Multi-Angle-Lighting",
|
| 241 |
+
"Upscale-Image",
|
| 242 |
+
"Relight",
|
| 243 |
+
"Next-Scene",
|
| 244 |
+
"Edit-Skin",
|
| 245 |
+
],
|
| 246 |
+
value="Photo-to-Anime",
|
| 247 |
+
)
|
| 248 |
with gr.Accordion("Advanced Settings", open=False, visible=False):
|
| 249 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 250 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 251 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 252 |
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
|
| 253 |
+
|
| 254 |
+
# --------------------------------------------------- Examples ----
|
| 255 |
gr.Examples(
|
| 256 |
examples=[
|
| 257 |
["examples/1.jpg", "Transform into anime.", "Photo-to-Anime"],
|
| 258 |
["examples/5.jpg", "Remove shadows and relight the image using soft lighting.", "Light-Restoration"],
|
| 259 |
+
["examples/4.jpg", "Use a subtle golden‑hour filter with smooth light diffusion.", "Relight"],
|
| 260 |
["examples/2.jpeg", "Rotate the camera 45 degrees to the left.", "Multiple-Angles"],
|
| 261 |
["examples/7.jpg", "Light source from the Right Rear", "Multi-Angle-Lighting"],
|
| 262 |
["examples/10.jpeg", "Upscale the image.", "Upscale-Image"],
|
| 263 |
["examples/7.jpg", "Light source from the Below", "Multi-Angle-Lighting"],
|
| 264 |
+
["examples/2.jpeg", "Switch the camera to a top‑down right corner view.", "Multiple-Angles"],
|
| 265 |
["examples/9.jpg", "The camera moves slightly forward as sunlight breaks through the clouds, casting a soft glow around the character's silhouette in the mist. Realistic cinematic style, atmospheric depth.", "Next-Scene"],
|
| 266 |
["examples/8.jpg", "Make the subjects skin details more prominent and natural.", "Edit-Skin"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
],
|
| 268 |
inputs=[input_image, prompt, lora_adapter],
|
| 269 |
outputs=[output_image, seed],
|
| 270 |
fn=infer_example,
|
| 271 |
cache_examples=False,
|
| 272 |
+
label="Examples",
|
| 273 |
)
|
| 274 |
|
| 275 |
+
# ---------------------------------------------------- Click ----
|
| 276 |
+
run_button.click(
|
| 277 |
+
fn=infer,
|
| 278 |
+
inputs=[
|
| 279 |
+
input_image,
|
| 280 |
+
prompt,
|
| 281 |
+
lora_adapter,
|
| 282 |
+
seed,
|
| 283 |
+
randomize_seed,
|
| 284 |
+
guidance_scale,
|
| 285 |
+
steps,
|
| 286 |
+
],
|
| 287 |
+
outputs=[output_image, seed],
|
| 288 |
+
)
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
| 291 |
+
demo.queue(max_size=30).launch(
|
| 292 |
+
css=css,
|
| 293 |
+
theme=steel_blue_theme,
|
| 294 |
+
mcp_server=True,
|
| 295 |
+
ssr_mode=False,
|
| 296 |
+
show_error=True,
|
| 297 |
+
)
|