Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
| 1 |
-
"""Qwen-Image-2.1 image edit with
|
| 2 |
-------------------------------------------------------------------------------------------------------------
|
| 3 |
|
| 4 |
Supports:
|
| 5 |
- Single image edit
|
| 6 |
- Optional dual-image edit (image1 = main, image2 = reference)
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
Two base-model modes:
|
| 10 |
-
* Turbo - Viggle
|
| 11 |
-
* Base - plain Qwen
|
| 12 |
"""
|
| 13 |
|
| 14 |
import os
|
|
@@ -31,8 +34,15 @@ from PIL import Image, ImageOps # noqa: E402
|
|
| 31 |
from safetensors.torch import load_file # noqa: E402
|
| 32 |
|
| 33 |
BASE_MODEL_ID = "Qwen/Qwen-Image-2.1"
|
|
|
|
|
|
|
| 34 |
LORA_REPO = "sanetium/N_lora_sfw_Qwen"
|
| 35 |
LORA_FILE = "NSFW Qwen by TheseAlpacas V2.safetensors"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
TURBO_REPO = "Viggle/Qwen-Image-2.1-viggle-turbo"
|
| 37 |
TURBO_FILE = "Qwen-Image-2.1-viggle-turbo-v0.2.1-6step-lora-r256.safetensors"
|
| 38 |
|
|
@@ -52,6 +62,10 @@ pipe = QwenImage21Pipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=torch.bflo
|
|
| 52 |
|
| 53 |
print("[load] N_lora ...", flush=True)
|
| 54 |
pipe.load_lora_weights(LORA_REPO, weight_name=LORA_FILE, adapter_name="n_lora")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
print("[load] Viggle turbo LoRA ...", flush=True)
|
| 56 |
pipe.load_lora_weights(TURBO_REPO, weight_name=TURBO_FILE, adapter_name="turbo")
|
| 57 |
|
|
@@ -130,15 +144,22 @@ def decode_latents(latents, height, width, tile_decode):
|
|
| 130 |
return pipe.image_processor.postprocess(image, output_type="pil")[0]
|
| 131 |
|
| 132 |
|
| 133 |
-
def _run(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 134 |
-
seed, prompt, kv_budget_gb, tile_decode):
|
| 135 |
res = int(resolution)
|
|
|
|
| 136 |
if base_model == TURBO:
|
| 137 |
-
pipe.set_adapters(
|
|
|
|
|
|
|
|
|
|
| 138 |
pipe.scheduler = turbo_scheduler
|
| 139 |
steps, sigmas = turbo_sigmas(num_inference_steps)
|
| 140 |
else:
|
| 141 |
-
pipe.set_adapters(
|
|
|
|
|
|
|
|
|
|
| 142 |
pipe.scheduler = base_scheduler
|
| 143 |
steps, sigmas = int(num_inference_steps), None
|
| 144 |
|
|
@@ -168,22 +189,22 @@ def _run(main_image, reference_image, base_model, resolution, num_inference_step
|
|
| 168 |
image = decode_latents(latents, height, width, tile_decode)
|
| 169 |
elapsed = time.perf_counter() - started
|
| 170 |
print(f"[edit] mode={base_model} res={res} size={image.size} steps={steps} "
|
| 171 |
-
f"
|
| 172 |
return image.convert("RGB"), elapsed
|
| 173 |
|
| 174 |
|
| 175 |
@spaces.GPU(duration=estimate_duration)
|
| 176 |
def _edit_large(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 177 |
-
|
| 178 |
return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 179 |
-
|
| 180 |
|
| 181 |
|
| 182 |
@spaces.GPU(duration=estimate_duration, size="xlarge")
|
| 183 |
def _edit_xlarge(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 184 |
-
|
| 185 |
return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 186 |
-
|
| 187 |
|
| 188 |
|
| 189 |
def edit_image(
|
|
@@ -192,13 +213,14 @@ def edit_image(
|
|
| 192 |
base_model: str = TURBO,
|
| 193 |
resolution: int = 1536,
|
| 194 |
num_inference_steps: int = 6,
|
| 195 |
-
|
|
|
|
| 196 |
seed: int = 0,
|
| 197 |
randomize_seed: bool = True,
|
| 198 |
prompt: str = "",
|
| 199 |
progress=gr.Progress(track_tqdm=True),
|
| 200 |
):
|
| 201 |
-
"""Edit an image with optional second reference image.
|
| 202 |
|
| 203 |
Args:
|
| 204 |
main_image: The main image to edit (required).
|
|
@@ -206,7 +228,8 @@ def edit_image(
|
|
| 206 |
base_model: "Turbo · Viggle 6-step" or "Base · Qwen-Image-2.1".
|
| 207 |
resolution: Output pixel budget side (1024, 1536 or 2048).
|
| 208 |
num_inference_steps: Denoising steps (Turbo: 4-8 recommended; Base: ~30).
|
| 209 |
-
|
|
|
|
| 210 |
seed: Random seed.
|
| 211 |
randomize_seed: Pick a random seed instead of `seed`.
|
| 212 |
prompt: Your edit instruction (write freely).
|
|
@@ -230,8 +253,11 @@ def edit_image(
|
|
| 230 |
seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 231 |
|
| 232 |
runner = _edit_xlarge if int(resolution) >= 2048 else _edit_large
|
| 233 |
-
result, _ = runner(
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
| 235 |
return (main_image, result), seed
|
| 236 |
|
| 237 |
|
|
@@ -253,12 +279,13 @@ css = """
|
|
| 253 |
.dark .gradio-container { color: var(--body-text-color); }
|
| 254 |
"""
|
| 255 |
|
| 256 |
-
with gr.Blocks(title="N_lora · Qwen-Image-2.1 Edit") as demo:
|
| 257 |
with gr.Column(elem_id="col-container"):
|
| 258 |
gr.Markdown(
|
| 259 |
-
"# N_lora · Qwen-Image-2.1 Edit\n"
|
| 260 |
-
"Single or dual-image editing with [
|
| 261 |
-
"
|
|
|
|
| 262 |
"• Upload **Main Image** (required)\n"
|
| 263 |
"• Optionally upload a **Reference Image** for dual-image edit\n"
|
| 264 |
"• Write your own prompt\n"
|
|
@@ -314,13 +341,20 @@ with gr.Blocks(title="N_lora · Qwen-Image-2.1 Edit") as demo:
|
|
| 314 |
value=DEFAULT_STEPS[TURBO],
|
| 315 |
info="Turbo: 6 is the validated schedule (clamped to 4-8).",
|
| 316 |
)
|
| 317 |
-
|
| 318 |
label="N_lora strength",
|
| 319 |
minimum=0.0,
|
| 320 |
maximum=1.5,
|
| 321 |
step=0.05,
|
| 322 |
value=1.0,
|
| 323 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
with gr.Row():
|
| 325 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 326 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
@@ -338,7 +372,8 @@ with gr.Blocks(title="N_lora · Qwen-Image-2.1 Edit") as demo:
|
|
| 338 |
base_model,
|
| 339 |
resolution,
|
| 340 |
num_inference_steps,
|
| 341 |
-
|
|
|
|
| 342 |
seed,
|
| 343 |
randomize_seed,
|
| 344 |
prompt,
|
|
|
|
| 1 |
+
"""Qwen-Image-2.1 image edit with stacked LoRAs (single or dual image) on ZeroGPU.
|
| 2 |
-------------------------------------------------------------------------------------------------------------
|
| 3 |
|
| 4 |
Supports:
|
| 5 |
- Single image edit
|
| 6 |
- Optional dual-image edit (image1 = main, image2 = reference)
|
| 7 |
|
| 8 |
+
LoRAs stacked:
|
| 9 |
+
1. N_lora (NSFW Qwen by TheseAlpacas V2)
|
| 10 |
+
2. FV (qwen2_1-femaledVaginus-nsfw-v1_3-fine_L22-31)
|
| 11 |
+
|
| 12 |
Two base-model modes:
|
| 13 |
+
* Turbo - Viggle turbo LoRA stacked on top, 6 steps
|
| 14 |
+
* Base - plain Qwen-Image-2.1 + the two LoRAs, ~30 steps
|
| 15 |
"""
|
| 16 |
|
| 17 |
import os
|
|
|
|
| 34 |
from safetensors.torch import load_file # noqa: E402
|
| 35 |
|
| 36 |
BASE_MODEL_ID = "Qwen/Qwen-Image-2.1"
|
| 37 |
+
|
| 38 |
+
# Main NSFW LoRA
|
| 39 |
LORA_REPO = "sanetium/N_lora_sfw_Qwen"
|
| 40 |
LORA_FILE = "NSFW Qwen by TheseAlpacas V2.safetensors"
|
| 41 |
+
|
| 42 |
+
# Second LoRA (FV)
|
| 43 |
+
FV_REPO = "sanetium/FV"
|
| 44 |
+
FV_FILE = "qwen2_1-femaledVaginus-nsfw-v1_3-fine_L22-31.safetensors"
|
| 45 |
+
|
| 46 |
TURBO_REPO = "Viggle/Qwen-Image-2.1-viggle-turbo"
|
| 47 |
TURBO_FILE = "Qwen-Image-2.1-viggle-turbo-v0.2.1-6step-lora-r256.safetensors"
|
| 48 |
|
|
|
|
| 62 |
|
| 63 |
print("[load] N_lora ...", flush=True)
|
| 64 |
pipe.load_lora_weights(LORA_REPO, weight_name=LORA_FILE, adapter_name="n_lora")
|
| 65 |
+
|
| 66 |
+
print("[load] FV LoRA ...", flush=True)
|
| 67 |
+
pipe.load_lora_weights(FV_REPO, weight_name=FV_FILE, adapter_name="fv")
|
| 68 |
+
|
| 69 |
print("[load] Viggle turbo LoRA ...", flush=True)
|
| 70 |
pipe.load_lora_weights(TURBO_REPO, weight_name=TURBO_FILE, adapter_name="turbo")
|
| 71 |
|
|
|
|
| 144 |
return pipe.image_processor.postprocess(image, output_type="pil")[0]
|
| 145 |
|
| 146 |
|
| 147 |
+
def _run(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 148 |
+
n_lora_scale, fv_scale, seed, prompt, kv_budget_gb, tile_decode):
|
| 149 |
res = int(resolution)
|
| 150 |
+
|
| 151 |
if base_model == TURBO:
|
| 152 |
+
pipe.set_adapters(
|
| 153 |
+
["n_lora", "fv", "turbo"],
|
| 154 |
+
adapter_weights=[float(n_lora_scale), float(fv_scale), 1.0]
|
| 155 |
+
)
|
| 156 |
pipe.scheduler = turbo_scheduler
|
| 157 |
steps, sigmas = turbo_sigmas(num_inference_steps)
|
| 158 |
else:
|
| 159 |
+
pipe.set_adapters(
|
| 160 |
+
["n_lora", "fv"],
|
| 161 |
+
adapter_weights=[float(n_lora_scale), float(fv_scale)]
|
| 162 |
+
)
|
| 163 |
pipe.scheduler = base_scheduler
|
| 164 |
steps, sigmas = int(num_inference_steps), None
|
| 165 |
|
|
|
|
| 189 |
image = decode_latents(latents, height, width, tile_decode)
|
| 190 |
elapsed = time.perf_counter() - started
|
| 191 |
print(f"[edit] mode={base_model} res={res} size={image.size} steps={steps} "
|
| 192 |
+
f"n_lora={n_lora_scale} fv={fv_scale} images={len(images)} elapsed={elapsed:.1f}s", flush=True)
|
| 193 |
return image.convert("RGB"), elapsed
|
| 194 |
|
| 195 |
|
| 196 |
@spaces.GPU(duration=estimate_duration)
|
| 197 |
def _edit_large(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 198 |
+
n_lora_scale, fv_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
|
| 199 |
return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 200 |
+
n_lora_scale, fv_scale, seed, prompt, kv_budget_gb=10.0, tile_decode=True)
|
| 201 |
|
| 202 |
|
| 203 |
@spaces.GPU(duration=estimate_duration, size="xlarge")
|
| 204 |
def _edit_xlarge(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 205 |
+
n_lora_scale, fv_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
|
| 206 |
return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
|
| 207 |
+
n_lora_scale, fv_scale, seed, prompt, kv_budget_gb=30.0, tile_decode=False)
|
| 208 |
|
| 209 |
|
| 210 |
def edit_image(
|
|
|
|
| 213 |
base_model: str = TURBO,
|
| 214 |
resolution: int = 1536,
|
| 215 |
num_inference_steps: int = 6,
|
| 216 |
+
n_lora_scale: float = 1.0,
|
| 217 |
+
fv_scale: float = 1.0,
|
| 218 |
seed: int = 0,
|
| 219 |
randomize_seed: bool = True,
|
| 220 |
prompt: str = "",
|
| 221 |
progress=gr.Progress(track_tqdm=True),
|
| 222 |
):
|
| 223 |
+
"""Edit an image with optional second reference image and two stacked LoRAs.
|
| 224 |
|
| 225 |
Args:
|
| 226 |
main_image: The main image to edit (required).
|
|
|
|
| 228 |
base_model: "Turbo · Viggle 6-step" or "Base · Qwen-Image-2.1".
|
| 229 |
resolution: Output pixel budget side (1024, 1536 or 2048).
|
| 230 |
num_inference_steps: Denoising steps (Turbo: 4-8 recommended; Base: ~30).
|
| 231 |
+
n_lora_scale: Strength of the main N_lora.
|
| 232 |
+
fv_scale: Strength of the FV LoRA.
|
| 233 |
seed: Random seed.
|
| 234 |
randomize_seed: Pick a random seed instead of `seed`.
|
| 235 |
prompt: Your edit instruction (write freely).
|
|
|
|
| 253 |
seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 254 |
|
| 255 |
runner = _edit_xlarge if int(resolution) >= 2048 else _edit_large
|
| 256 |
+
result, _ = runner(
|
| 257 |
+
main_image, reference_image, base_model, int(resolution),
|
| 258 |
+
int(num_inference_steps), float(n_lora_scale), float(fv_scale),
|
| 259 |
+
seed, prompt, progress
|
| 260 |
+
)
|
| 261 |
return (main_image, result), seed
|
| 262 |
|
| 263 |
|
|
|
|
| 279 |
.dark .gradio-container { color: var(--body-text-color); }
|
| 280 |
"""
|
| 281 |
|
| 282 |
+
with gr.Blocks(title="N_lora + FV · Qwen-Image-2.1 Edit") as demo:
|
| 283 |
with gr.Column(elem_id="col-container"):
|
| 284 |
gr.Markdown(
|
| 285 |
+
"# N_lora + FV · Qwen-Image-2.1 Edit\n"
|
| 286 |
+
"Single or dual-image editing with two stacked LoRAs on [Qwen-Image-2.1](https://huggingface.co/Qwen/Qwen-Image-2.1):\n\n"
|
| 287 |
+
"• **N_lora** – [sanetium/N_lora_sfw_Qwen](https://huggingface.co/sanetium/N_lora_sfw_Qwen)\n"
|
| 288 |
+
"• **FV** – [sanetium/FV](https://huggingface.co/sanetium/FV)\n\n"
|
| 289 |
"• Upload **Main Image** (required)\n"
|
| 290 |
"• Optionally upload a **Reference Image** for dual-image edit\n"
|
| 291 |
"• Write your own prompt\n"
|
|
|
|
| 341 |
value=DEFAULT_STEPS[TURBO],
|
| 342 |
info="Turbo: 6 is the validated schedule (clamped to 4-8).",
|
| 343 |
)
|
| 344 |
+
n_lora_scale = gr.Slider(
|
| 345 |
label="N_lora strength",
|
| 346 |
minimum=0.0,
|
| 347 |
maximum=1.5,
|
| 348 |
step=0.05,
|
| 349 |
value=1.0,
|
| 350 |
)
|
| 351 |
+
fv_scale = gr.Slider(
|
| 352 |
+
label="FV LoRA strength",
|
| 353 |
+
minimum=0.0,
|
| 354 |
+
maximum=1.5,
|
| 355 |
+
step=0.05,
|
| 356 |
+
value=1.0,
|
| 357 |
+
)
|
| 358 |
with gr.Row():
|
| 359 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 360 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 372 |
base_model,
|
| 373 |
resolution,
|
| 374 |
num_inference_steps,
|
| 375 |
+
n_lora_scale,
|
| 376 |
+
fv_scale,
|
| 377 |
seed,
|
| 378 |
randomize_seed,
|
| 379 |
prompt,
|