Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,358 +1,195 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from diffusers import DiffusionPipeline
|
| 6 |
-
import random
|
| 7 |
-
import uuid
|
| 8 |
-
from typing import Union, List, Optional
|
| 9 |
import numpy as np
|
| 10 |
-
import
|
| 11 |
-
import
|
| 12 |
-
import
|
| 13 |
-
import requests
|
| 14 |
-
from urllib.parse import urlparse
|
| 15 |
-
import tempfile
|
| 16 |
-
import shutil
|
| 17 |
-
|
| 18 |
-
# Description for the app
|
| 19 |
-
DESCRIPTION = """## Qwen Image Hpc/."""
|
| 20 |
-
|
| 21 |
-
# Helper functions
|
| 22 |
-
def save_image(img):
|
| 23 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
| 24 |
-
img.save(unique_name)
|
| 25 |
-
return unique_name
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
MAX_IMAGE_SIZE = 2048
|
| 34 |
|
| 35 |
-
# Load Qwen/Qwen-Image pipeline
|
| 36 |
-
dtype = torch.bfloat16
|
| 37 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 38 |
|
| 39 |
# --- Model Loading ---
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# Aspect ratios
|
| 43 |
-
aspect_ratios = {
|
| 44 |
-
"1:1": (1328, 1328),
|
| 45 |
-
"16:9": (1664, 928),
|
| 46 |
-
"9:16": (928, 1664),
|
| 47 |
-
"4:3": (1472, 1140),
|
| 48 |
-
"3:4": (1140, 1472)
|
| 49 |
-
}
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# If it's just an ID like "author/model"
|
| 57 |
-
if "/" in lora_input and not lora_input.startswith("http"):
|
| 58 |
-
pipe.load_lora_weights(lora_input, adapter_name="default")
|
| 59 |
-
return
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
if "huggingface.co" in url and "/blob/" not in url and "/resolve/" not in url:
|
| 66 |
-
repo_id = urlparse(url).path.strip("/")
|
| 67 |
-
pipe.load_lora_weights(repo_id, adapter_name="default")
|
| 68 |
-
return
|
| 69 |
|
| 70 |
-
# Blob link → convert to resolve link
|
| 71 |
-
if "/blob/" in url:
|
| 72 |
-
url = url.replace("/blob/", "/resolve/")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
tmp_dir = tempfile.mkdtemp()
|
| 76 |
-
local_path = os.path.join(tmp_dir, os.path.basename(urlparse(url).path))
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
# Generation function for Qwen/Qwen-Image
|
| 91 |
-
@spaces.GPU(duration=120)
|
| 92 |
-
def generate_qwen(
|
| 93 |
-
prompt: str,
|
| 94 |
-
negative_prompt: str = "",
|
| 95 |
-
seed: int = 0,
|
| 96 |
-
width: int = 1024,
|
| 97 |
-
height: int = 1024,
|
| 98 |
-
guidance_scale: float = 4.0,
|
| 99 |
-
randomize_seed: bool = False,
|
| 100 |
-
num_inference_steps: int = 50,
|
| 101 |
-
num_images: int = 1,
|
| 102 |
-
zip_images: bool = False,
|
| 103 |
-
lora_input: str = "",
|
| 104 |
-
lora_scale: float = 1.0,
|
| 105 |
-
progress=gr.Progress(track_tqdm=True),
|
| 106 |
-
):
|
| 107 |
if randomize_seed:
|
| 108 |
seed = random.randint(0, MAX_SEED)
|
| 109 |
-
generator = torch.Generator(device).manual_seed(seed)
|
| 110 |
-
|
| 111 |
-
start_time = time.time()
|
| 112 |
-
|
| 113 |
-
current_adapters = pipe_qwen.get_list_adapters()
|
| 114 |
-
for adapter in current_adapters:
|
| 115 |
-
pipe_qwen.delete_adapters(adapter)
|
| 116 |
-
pipe_qwen.disable_lora()
|
| 117 |
|
| 118 |
-
|
| 119 |
-
if lora_input and lora_input.strip() != "":
|
| 120 |
-
load_lora_opt(pipe_qwen, lora_input)
|
| 121 |
-
pipe_qwen.set_adapters(["default"], adapter_weights=[lora_scale])
|
| 122 |
-
use_lora = True
|
| 123 |
-
|
| 124 |
-
images = pipe_qwen(
|
| 125 |
-
prompt=prompt,
|
| 126 |
-
negative_prompt=negative_prompt if negative_prompt else "",
|
| 127 |
-
height=height,
|
| 128 |
-
width=width,
|
| 129 |
-
guidance_scale=guidance_scale,
|
| 130 |
-
num_inference_steps=num_inference_steps,
|
| 131 |
-
num_images_per_prompt=num_images,
|
| 132 |
-
generator=generator,
|
| 133 |
-
output_type="pil",
|
| 134 |
-
).images
|
| 135 |
|
| 136 |
-
|
| 137 |
-
duration = end_time - start_time
|
| 138 |
-
|
| 139 |
-
image_paths = [save_image(img) for img in images]
|
| 140 |
-
zip_path = None
|
| 141 |
-
if zip_images:
|
| 142 |
-
zip_name = str(uuid.uuid4()) + ".zip"
|
| 143 |
-
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 144 |
-
for i, img_path in enumerate(image_paths):
|
| 145 |
-
zipf.write(img_path, arcname=f"Img_{i}.png")
|
| 146 |
-
zip_path = zip_name
|
| 147 |
-
|
| 148 |
-
# Clean up adapters
|
| 149 |
-
current_adapters = pipe_qwen.get_list_adapters()
|
| 150 |
-
for adapter in current_adapters:
|
| 151 |
-
pipe_qwen.delete_adapters(adapter)
|
| 152 |
-
pipe_qwen.disable_lora()
|
| 153 |
-
|
| 154 |
-
return image_paths, seed, f"{duration:.2f}", zip_path
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
def generate(
|
| 159 |
-
prompt: str,
|
| 160 |
-
negative_prompt: str,
|
| 161 |
-
use_negative_prompt: bool,
|
| 162 |
-
seed: int,
|
| 163 |
-
width: int,
|
| 164 |
-
height: int,
|
| 165 |
-
guidance_scale: float,
|
| 166 |
-
randomize_seed: bool,
|
| 167 |
-
num_inference_steps: int,
|
| 168 |
-
num_images: int,
|
| 169 |
-
zip_images: bool,
|
| 170 |
-
lora_input: str,
|
| 171 |
-
lora_scale: float,
|
| 172 |
-
progress=gr.Progress(track_tqdm=True),
|
| 173 |
-
):
|
| 174 |
-
final_negative_prompt = negative_prompt if use_negative_prompt else ""
|
| 175 |
-
return generate_qwen(
|
| 176 |
prompt=prompt,
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
css = '''
|
| 202 |
-
.gradio-container {
|
| 203 |
-
max-width: 590px !important;
|
| 204 |
-
margin: 0 auto !important;
|
| 205 |
-
}
|
| 206 |
-
h1 {
|
| 207 |
-
text-align: center;
|
| 208 |
-
}
|
| 209 |
-
footer {
|
| 210 |
-
visibility: hidden;
|
| 211 |
}
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
# Gradio interface
|
| 215 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme", delete_cache=(240, 240)) as demo:
|
| 216 |
-
gr.Markdown(DESCRIPTION)
|
| 217 |
-
with gr.Row():
|
| 218 |
-
prompt = gr.Text(
|
| 219 |
-
label="Prompt",
|
| 220 |
-
show_label=False,
|
| 221 |
-
max_lines=1,
|
| 222 |
-
placeholder="✦︎ Enter your prompt",
|
| 223 |
-
container=False,
|
| 224 |
-
)
|
| 225 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 226 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True)
|
| 227 |
-
|
| 228 |
-
with gr.Row():
|
| 229 |
-
aspect_ratio = gr.Dropdown(
|
| 230 |
-
label="Aspect Ratio",
|
| 231 |
-
choices=list(aspect_ratios.keys()),
|
| 232 |
-
value="1:1",
|
| 233 |
-
)
|
| 234 |
-
lora = gr.Textbox(label="qwen image lora (optional)", placeholder="enter the path...")
|
| 235 |
-
with gr.Accordion("Additional Options", open=False):
|
| 236 |
-
use_negative_prompt = gr.Checkbox(
|
| 237 |
-
label="Use negative prompt",
|
| 238 |
-
value=True,
|
| 239 |
-
visible=True
|
| 240 |
-
)
|
| 241 |
-
negative_prompt = gr.Text(
|
| 242 |
-
label="Negative prompt",
|
| 243 |
-
max_lines=1,
|
| 244 |
-
placeholder="Enter a negative prompt",
|
| 245 |
-
value="text, watermark, copyright, blurry, low resolution",
|
| 246 |
-
visible=True,
|
| 247 |
-
)
|
| 248 |
-
seed = gr.Slider(
|
| 249 |
-
label="Seed",
|
| 250 |
-
minimum=0,
|
| 251 |
-
maximum=MAX_SEED,
|
| 252 |
-
step=1,
|
| 253 |
-
value=0,
|
| 254 |
-
)
|
| 255 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 256 |
with gr.Row():
|
| 257 |
-
|
| 258 |
-
label="
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
)
|
| 292 |
-
zip_images = gr.Checkbox(label="Zip generated images", value=False)
|
| 293 |
-
with gr.Row():
|
| 294 |
-
lora_scale = gr.Slider(
|
| 295 |
-
label="LoRA Scale",
|
| 296 |
-
minimum=0,
|
| 297 |
-
maximum=2,
|
| 298 |
-
step=0.01,
|
| 299 |
-
value=1,
|
| 300 |
-
)
|
| 301 |
-
|
| 302 |
-
gr.Markdown("### Output Information")
|
| 303 |
-
seed_display = gr.Textbox(label="Seed used", interactive=False)
|
| 304 |
-
generation_time = gr.Textbox(label="Generation time (seconds)", interactive=False)
|
| 305 |
-
zip_file = gr.File(label="Download ZIP")
|
| 306 |
-
|
| 307 |
-
# Update aspect ratio
|
| 308 |
-
def set_dimensions(ar):
|
| 309 |
-
w, h = aspect_ratios[ar]
|
| 310 |
-
return gr.update(value=w), gr.update(value=h)
|
| 311 |
-
|
| 312 |
-
aspect_ratio.change(
|
| 313 |
-
fn=set_dimensions,
|
| 314 |
-
inputs=aspect_ratio,
|
| 315 |
-
outputs=[width, height]
|
| 316 |
-
)
|
| 317 |
-
|
| 318 |
-
# Negative prompt visibility
|
| 319 |
-
use_negative_prompt.change(
|
| 320 |
-
fn=lambda x: gr.update(visible=x),
|
| 321 |
-
inputs=use_negative_prompt,
|
| 322 |
-
outputs=negative_prompt
|
| 323 |
-
)
|
| 324 |
|
| 325 |
-
# Run button and prompt submit
|
| 326 |
gr.on(
|
| 327 |
-
triggers=[
|
| 328 |
-
fn=
|
| 329 |
-
inputs=[
|
| 330 |
-
|
| 331 |
-
negative_prompt,
|
| 332 |
-
use_negative_prompt,
|
| 333 |
-
seed,
|
| 334 |
-
width,
|
| 335 |
-
height,
|
| 336 |
-
guidance_scale,
|
| 337 |
-
randomize_seed,
|
| 338 |
-
num_inference_steps,
|
| 339 |
-
num_images,
|
| 340 |
-
zip_images,
|
| 341 |
-
lora,
|
| 342 |
-
lora_scale,
|
| 343 |
-
],
|
| 344 |
-
outputs=[result, seed_display, generation_time, zip_file],
|
| 345 |
-
api_name="run",
|
| 346 |
)
|
| 347 |
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
outputs=[result, seed_display, generation_time, zip_file],
|
| 353 |
-
fn=generate,
|
| 354 |
-
cache_examples=False,
|
| 355 |
)
|
| 356 |
|
| 357 |
-
|
| 358 |
-
demo.queue(max_size=50).launch(share=False, mcp_server=True, ssr_mode=False, debug=True, show_error=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import torch
|
| 5 |
+
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 9 |
+
from optimization import optimize_pipeline_
|
| 10 |
+
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
| 11 |
+
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
|
| 12 |
+
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
|
| 13 |
+
from gradio_imageslider import ImageSlider
|
| 14 |
+
from gradio.themes import Soft
|
| 15 |
+
from gradio.themes.utils import colors, fonts, sizes
|
| 16 |
+
|
| 17 |
+
# --- Custom Theme Definition ---
|
| 18 |
+
colors.orange_red = colors.Color(
|
| 19 |
+
name="orange_red",
|
| 20 |
+
c50="#FFF0E5", c100="#FFE0CC", c200="#FFC299", c300="#FFA366",
|
| 21 |
+
c400="#FF8533", c500="#FF4500", c600="#E63E00", c700="#CC3700",
|
| 22 |
+
c800="#B33000", c900="#992900", c950="#802200",
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
class OrangeRedTheme(Soft):
|
| 26 |
+
def __init__(self, *args, **kwargs):
|
| 27 |
+
super().__init__(*args, **kwargs)
|
| 28 |
+
super().set(
|
| 29 |
+
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
|
| 30 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
|
| 31 |
+
button_primary_text_color="white",
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
orange_red_theme = OrangeRedTheme()
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# --- Model Loading ---
|
| 38 |
+
dtype = torch.bfloat16
|
| 39 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509",
|
| 42 |
+
transformer=QwenImageTransformer2DModel.from_pretrained("linoyts/Qwen-Image-Edit-Rapid-AIO",
|
| 43 |
+
subfolder='transformer',
|
| 44 |
+
torch_dtype=dtype,
|
| 45 |
+
device_map='cuda'), torch_dtype=dtype).to(device)
|
| 46 |
+
# Load all LoRA adapters
|
| 47 |
+
pipe.load_lora_weights("dx8152/Qwen-Edit-2509-Multiple-angles", weight_name="镜头转换.safetensors", adapter_name="angles")
|
| 48 |
+
pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Light_restoration", weight_name="移除光影.safetensors", adapter_name="light_restoration")
|
| 49 |
+
pipe.load_lora_weights("autoweeb/Qwen-Image-Edit-2509-Photo-to-Anime", weight_name="Qwen-Image-Edit-2509-Photo-to-Anime_000001000.safetensors", adapter_name="photo_to_anime")
|
| 50 |
+
pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Relight", weight_name="Qwen-Edit-Relight.safetensors", adapter_name="relight")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
pipe.transformer.__class__ = QwenImageTransformer2DModel
|
| 54 |
+
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 55 |
|
| 56 |
+
optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
@spaces.GPU
|
| 62 |
+
def infer(input_image, prompt, lora_adapter, seed=42, randomize_seed=True, guidance_scale=1.0, steps=4, progress=gr.Progress(track_tqdm=True)):
|
| 63 |
+
"""
|
| 64 |
+
Perform image editing based on the selected LoRA adapter and prompt.
|
| 65 |
+
"""
|
| 66 |
+
if not input_image:
|
| 67 |
+
raise gr.Error("Please upload an image for editing.")
|
| 68 |
+
|
| 69 |
+
# Set the LoRA adapter based on user selection
|
| 70 |
+
if lora_adapter == "Multiple Angles":
|
| 71 |
+
pipe.set_adapters(["angles"], adapter_weights=[1.0])
|
| 72 |
+
elif lora_adapter == "Light Restoration":
|
| 73 |
+
pipe.set_adapters(["light_restoration"], adapter_weights=[1.0])
|
| 74 |
+
elif lora_adapter == "Photo to Anime":
|
| 75 |
+
pipe.set_adapters(["photo_to_anime"], adapter_weights=[1.0])
|
| 76 |
+
elif lora_adapter == "Relight":
|
| 77 |
+
pipe.set_adapters(["relight"], adapter_weights=[1.0])
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if randomize_seed:
|
| 80 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
original_image = input_image.copy().convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
result = pipe(
|
| 87 |
+
image=original_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
prompt=prompt,
|
| 89 |
+
height=original_image.size[1],
|
| 90 |
+
width=original_image.size[0],
|
| 91 |
+
num_inference_steps=steps,
|
| 92 |
+
generator=generator,
|
| 93 |
+
true_cfg_scale=guidance_scale,
|
| 94 |
+
num_images_per_prompt=1,
|
| 95 |
+
).images[0]
|
| 96 |
+
|
| 97 |
+
return (original_image, result), seed, gr.Button(visible=True)
|
| 98 |
+
|
| 99 |
+
@spaces.GPU
|
| 100 |
+
def infer_example(input_image, prompt, lora_adapter):
|
| 101 |
+
"""
|
| 102 |
+
Wrapper function for gr.Examples to call the main infer logic for the slider.
|
| 103 |
+
"""
|
| 104 |
+
(original_image, generated_image), seed, _ = infer(input_image, prompt, lora_adapter, upscale_image=False)
|
| 105 |
+
return (original_image, generated_image), seed
|
| 106 |
+
|
| 107 |
+
# --- UI ---
|
| 108 |
+
css = """
|
| 109 |
+
#col-container {
|
| 110 |
+
margin: 0 auto;
|
| 111 |
+
max-width: 960px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
+
#main-title h1 {font-size: 2.1em !important;}
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
with gr.Blocks(css=css, theme=orange_red_theme) as demo:
|
| 117 |
+
with gr.Column(elem_id="col-container"):
|
| 118 |
+
gr.Markdown("# **Qwen-Image-Edit-2509-LoRAs-Fast**", elem_id="main-title")
|
| 119 |
+
gr.Markdown("Image manipulation with Qwen Image Edit 2509 and various LoRA adapters.")
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
with gr.Row():
|
| 122 |
+
with gr.Column():
|
| 123 |
+
input_image = gr.Image(label="Upload Image", type="pil", height="300")
|
| 124 |
+
with gr.Row():
|
| 125 |
+
prompt = gr.Text(
|
| 126 |
+
label="Edit Prompt",
|
| 127 |
+
show_label=False,
|
| 128 |
+
max_lines=1,
|
| 129 |
+
placeholder="Enter your prompt for editing",
|
| 130 |
+
container=False,
|
| 131 |
+
)
|
| 132 |
+
run_button = gr.Button("Run", variant="primary", scale=0)
|
| 133 |
+
|
| 134 |
+
lora_adapter = gr.Dropdown(
|
| 135 |
+
label="Choose LoRA Adapter",
|
| 136 |
+
choices=["Multiple Angles", "Light Restoration", "Photo to Anime", "Relight"],
|
| 137 |
+
value="Multiple Angles"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 141 |
+
seed = gr.Slider(
|
| 142 |
+
label="Seed",
|
| 143 |
+
minimum=0,
|
| 144 |
+
maximum=MAX_SEED,
|
| 145 |
+
step=1,
|
| 146 |
+
value=0,
|
| 147 |
+
)
|
| 148 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 149 |
+
guidance_scale = gr.Slider(
|
| 150 |
+
label="Guidance Scale",
|
| 151 |
+
minimum=1,
|
| 152 |
+
maximum=10,
|
| 153 |
+
step=0.1,
|
| 154 |
+
value=1.0,
|
| 155 |
+
)
|
| 156 |
+
steps = gr.Slider(
|
| 157 |
+
label="Steps",
|
| 158 |
+
minimum=1,
|
| 159 |
+
maximum=30,
|
| 160 |
+
value=4,
|
| 161 |
+
step=1
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
with gr.Column():
|
| 165 |
+
output_slider = ImageSlider(label="Before / After", show_label=False, interactive=False)
|
| 166 |
+
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 167 |
+
|
| 168 |
+
gr.Examples(
|
| 169 |
+
examples=[
|
| 170 |
+
["examples/sea.png", "Rotate the camera 90 degrees to the left.", "Multiple Angles"],
|
| 171 |
+
["examples/shadow.jpg", "Remove shadows and relight the image using soft lighting.", "Light Restoration"],
|
| 172 |
+
["examples/girl.jpg", "transform into anime", "Photo to Anime"],
|
| 173 |
+
["examples/dark.jpg", "Relight the image using soft, diffused lighting that simulates sunlight filtering through curtains.", "Relight"],
|
| 174 |
+
],
|
| 175 |
+
inputs=[input_image, prompt, lora_adapter],
|
| 176 |
+
outputs=[output_slider, seed],
|
| 177 |
+
fn=infer,
|
| 178 |
+
cache_examples="lazy",
|
| 179 |
+
label="Examples"
|
| 180 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
|
|
|
| 182 |
gr.on(
|
| 183 |
+
triggers=[run_button.click, prompt.submit],
|
| 184 |
+
fn=infer,
|
| 185 |
+
inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 186 |
+
outputs=[output_slider, seed, reuse_button]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
+
reuse_button.click(
|
| 190 |
+
fn=lambda images: images[1] if isinstance(images, (list, tuple)) and len(images) > 1 else images,
|
| 191 |
+
inputs=[output_slider],
|
| 192 |
+
outputs=[input_image]
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
+
demo.launch(mcp_server=True, ssr_mode=False, show_error=True)
|
|
|