File size: 15,805 Bytes
c6bb0ae
57b88ed
01660e0
05eb148
 
 
01660e0
c6bb0ae
 
 
 
01660e0
c6bb0ae
 
01660e0
 
 
 
 
 
57b88ed
01660e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6bb0ae
 
57b88ed
 
c6bb0ae
 
 
 
 
01660e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57b88ed
 
c6bb0ae
 
 
 
01660e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05eb148
01660e0
 
 
 
 
 
 
4093b55
 
 
01660e0
 
b8acb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6bb0ae
 
01660e0
c6bb0ae
01660e0
c6bb0ae
 
 
 
01660e0
 
 
c6bb0ae
 
 
 
01660e0
 
 
05eb148
 
 
 
 
 
 
 
 
01660e0
b8acb3e
01660e0
05eb148
01660e0
 
 
 
 
 
 
 
b8acb3e
 
 
01660e0
05eb148
c6bb0ae
01660e0
 
 
 
05eb148
c6bb0ae
05eb148
c6bb0ae
01660e0
 
 
05eb148
c6bb0ae
05eb148
c6bb0ae
01660e0
 
05eb148
 
 
01660e0
 
 
c6bb0ae
 
01660e0
 
05eb148
01660e0
 
c6bb0ae
01660e0
 
05eb148
 
 
 
 
c6bb0ae
 
01660e0
 
05eb148
01660e0
 
05eb148
01660e0
05eb148
 
01660e0
 
05eb148
 
 
 
 
 
 
 
 
01660e0
 
05eb148
c6bb0ae
 
 
 
 
05eb148
01660e0
 
 
 
7797269
 
 
01660e0
 
 
 
05eb148
01660e0
 
 
 
 
 
 
 
c6bb0ae
01660e0
 
c6bb0ae
 
 
 
05eb148
 
 
 
01660e0
 
 
 
 
05eb148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01660e0
05eb148
 
 
 
 
 
 
 
 
 
 
 
 
 
c6bb0ae
05eb148
 
 
 
 
 
c6bb0ae
 
 
 
 
 
 
01660e0
 
 
 
 
 
 
 
57b88ed
 
05eb148
 
 
 
 
 
 
c6bb0ae
 
05eb148
 
 
 
57b88ed
05eb148
57b88ed
01660e0
 
57b88ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""Qwen-Image-2.1 image edit with stacked LoRAs (single or dual image) on ZeroGPU.
-------------------------------------------------------------------------------------------------------------

Supports:
- Single image edit
- Optional dual-image edit (image1 = main, image2 = reference)

LoRAs stacked:
1. N_lora (NSFW Qwen by TheseAlpacas V2)
2. FV (qwen2_1-femaledVaginus-nsfw-v1_3-fine_L22-31)

Two base-model modes:
* Turbo - Viggle turbo LoRA stacked on top, 6 steps
* Base  - plain Qwen-Image-2.1 + the two LoRAs, ~30 steps
"""

import os

os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")

import spaces  # noqa: E402 - must precede torch

import math  # noqa: E402
import random  # noqa: E402
import time  # noqa: E402

import gradio as gr  # noqa: E402
import numpy as np  # noqa: E402
import torch  # noqa: E402
from diffusers import FlowMatchEulerDiscreteScheduler, QwenImage21Pipeline  # noqa: E402
from diffusers.pipelines.qwenimage21.pipeline_qwenimage21 import calculate_dimensions  # noqa: E402
from huggingface_hub import hf_hub_download  # noqa: E402
from PIL import Image, ImageOps  # noqa: E402
from safetensors.torch import load_file  # noqa: E402

BASE_MODEL_ID = "Qwen/Qwen-Image-2.1"

# Main NSFW LoRA
LORA_REPO = "sanetium/N_lora_sfw_Qwen"
LORA_FILE = "NSFW Qwen by TheseAlpacas V2.safetensors"

# Second LoRA (FV)
FV_REPO = "sanetium/FV"
FV_FILE = "qwen2_1-femaledVaginus-nsfw-v1_3-fine_L22-31.safetensors"

TURBO_REPO = "Viggle/Qwen-Image-2.1-viggle-turbo"
TURBO_FILE = "Qwen-Image-2.1-viggle-turbo-v0.2.1-6step-lora-r256.safetensors"

MAX_SEED = np.iinfo(np.int32).max
TURBO = "Turbo · Viggle 6-step"
BASE = "Base · Qwen-Image-2.1"
MODES = [TURBO, BASE]
DEFAULT_STEPS = {TURBO: 6, BASE: 30}
RESOLUTIONS = [("1K (1024² area)", 1024), ("1.5K (1536² area)", 1536), ("2K (2048² area, full GPU)", 2048)]

# ----------------------------------------------------------------------------------------
# Models
# ----------------------------------------------------------------------------------------

print("[load] base pipeline ...", flush=True)
pipe = QwenImage21Pipeline.from_pretrained(BASE_MODEL_ID, torch_dtype=torch.bfloat16)

print("[load] N_lora ...", flush=True)
pipe.load_lora_weights(LORA_REPO, weight_name=LORA_FILE, adapter_name="n_lora")

print("[load] FV LoRA ...", flush=True)
pipe.load_lora_weights(FV_REPO, weight_name=FV_FILE, adapter_name="fv")

print("[load] Viggle turbo LoRA ...", flush=True)
pipe.load_lora_weights(TURBO_REPO, weight_name=TURBO_FILE, adapter_name="turbo")

base_scheduler = pipe.scheduler
# The stock config has shift_terminal=0.02, which would wreck turbo's last step.
turbo_scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config, shift_terminal=None)

pipe.to("cuda")

# Qwen3-VL's patch embed is a Conv3d with kernel == stride, i.e. a linear map; cuDNN's bf16
# Conv3d fallback costs tens of seconds per reference image. Same result as a matmul.
try:
    patch_embed = pipe.text_encoder.model.visual.patch_embed

    def _patch_embed_forward(hidden_states):
        proj = patch_embed.proj
        weight = proj.weight.reshape(proj.weight.shape[0], -1)
        return hidden_states.to(weight.dtype).flatten(1) @ weight.T + proj.bias

    patch_embed.forward = _patch_embed_forward
except AttributeError as exc:
    print(f"[load] patch_embed fast path unavailable: {exc!r}", flush=True)

print("[load] ready", flush=True)


# ----------------------------------------------------------------------------------------
# Helpers
# ----------------------------------------------------------------------------------------


def turbo_sigmas(steps: int):
    """Viggle's raw sigma nodes: keep 0.875/0.75/0.5/0.25, add steps in the 1 -> 0.875 segment."""
    steps = min(max(int(steps), 4), 8)
    if steps == 4:
        return steps, [1.0, 0.75, 0.5, 0.25]
    return steps, [1.0 - 0.125 * i / (steps - 4) for i in range(steps - 4)] + [0.875, 0.75, 0.5, 0.25]


def kv_cache_fits(resolution: int, budget_gb: float) -> bool:
    prefix_tokens = 2 * (int(resolution) // 16) ** 2
    return prefix_tokens * 32 * 2 * 4096 * 2 <= budget_gb * 1e9


def estimate_duration(main_image=None, reference_image=None, base_model=TURBO, resolution=1536,
                      num_inference_steps=6, *args, **kwargs):
    try:
        res, steps = int(resolution), int(num_inference_steps)
    except (TypeError, ValueError):
        res, steps = 1536, 6
    if base_model == TURBO:
        steps = min(max(steps, 4), 8)
    per_step = {1024: 0.65, 1536: 2.0, 2048: 2.6}.get(res, 2.0)
    fixed = {1024: 6, 1536: 11, 2048: 9}.get(res, 11)
    return int(min(300, math.ceil((fixed + steps * per_step) * 1.15 + 3)))


@torch.no_grad()
def decode_latents(latents, height, width, tile_decode):
    """The pipeline's own decode, with the KV cache freed first and (on the 48 GB slice) a tiled
    decode: the untiled VAE decoder upcasts to fp32 and OOMs at 1536² after two 1536² references.
    Only the decode is tiled - tiling the reference *encode* is known to degrade edits."""
    torch.cuda.empty_cache()
    vae = pipe.vae
    latents = pipe._unpack_latents(latents, height, width, pipe.vae_scale_factor).to(vae.dtype)
    mean = torch.tensor(vae.config.latents_mean).view(1, vae.config.z_dim, 1, 1, 1).to(latents)
    std = torch.tensor(vae.config.latents_std).view(1, vae.config.z_dim, 1, 1, 1).to(latents)
    latents = latents * std + mean
    if tile_decode:
        vae.enable_tiling(tile_sample_min_height=1024, tile_sample_min_width=1024,
                          tile_sample_stride_height=768, tile_sample_stride_width=768)
    try:
        image = vae.decode(latents, return_dict=False)[0][:, :, 0]
    finally:
        vae.disable_tiling()
    return pipe.image_processor.postprocess(image, output_type="pil")[0]


def _run(main_image, reference_image, base_model, resolution, num_inference_steps,
         n_lora_scale, fv_scale, seed, prompt, kv_budget_gb, tile_decode):
    res = int(resolution)

    if base_model == TURBO:
        pipe.set_adapters(
            ["n_lora", "fv", "turbo"],
            adapter_weights=[float(n_lora_scale), float(fv_scale), 1.0]
        )
        pipe.scheduler = turbo_scheduler
        steps, sigmas = turbo_sigmas(num_inference_steps)
    else:
        pipe.set_adapters(
            ["n_lora", "fv"],
            adapter_weights=[float(n_lora_scale), float(fv_scale)]
        )
        pipe.scheduler = base_scheduler
        steps, sigmas = int(num_inference_steps), None

    # Output follows the main image aspect ratio
    width, height, _ = calculate_dimensions(res * res, main_image.width / main_image.height)

    # Build image list: single or dual
    if reference_image is not None:
        images = [main_image, reference_image]
    else:
        images = [main_image]

    started = time.perf_counter()
    latents = pipe(
        prompt=prompt,
        image=images,
        width=width,
        height=height,
        output_resolution=res,
        num_inference_steps=steps,
        sigmas=sigmas,
        true_cfg_scale=1.0,
        use_kv_cache=kv_cache_fits(res, kv_budget_gb),
        generator=torch.Generator(device="cuda").manual_seed(int(seed)),
        output_type="latent",
    ).images
    image = decode_latents(latents, height, width, tile_decode)
    elapsed = time.perf_counter() - started
    print(f"[edit] mode={base_model} res={res} size={image.size} steps={steps} "
          f"n_lora={n_lora_scale} fv={fv_scale} images={len(images)} elapsed={elapsed:.1f}s", flush=True)
    return image.convert("RGB"), elapsed


@spaces.GPU(duration=estimate_duration)
def _edit_large(main_image, reference_image, base_model, resolution, num_inference_steps,
                n_lora_scale, fv_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
    return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
                n_lora_scale, fv_scale, seed, prompt, kv_budget_gb=10.0, tile_decode=True)


@spaces.GPU(duration=estimate_duration, size="xlarge")
def _edit_xlarge(main_image, reference_image, base_model, resolution, num_inference_steps,
                 n_lora_scale, fv_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
    return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
                n_lora_scale, fv_scale, seed, prompt, kv_budget_gb=30.0, tile_decode=False)


def edit_image(
    main_image: Image.Image,
    reference_image: Image.Image = None,
    base_model: str = TURBO,
    resolution: int = 1536,
    num_inference_steps: int = 6,
    n_lora_scale: float = 1.0,
    fv_scale: float = 1.0,
    seed: int = 0,
    randomize_seed: bool = True,
    prompt: str = "",
    progress=gr.Progress(track_tqdm=True),
):
    """Edit an image with optional second reference image and two stacked LoRAs.

    Args:
        main_image: The main image to edit (required).
        reference_image: Optional second image (reference / style / identity etc.).
        base_model: "Turbo · Viggle 6-step" or "Base · Qwen-Image-2.1".
        resolution: Output pixel budget side (1024, 1536 or 2048).
        num_inference_steps: Denoising steps (Turbo: 4-8 recommended; Base: ~30).
        n_lora_scale: Strength of the main N_lora.
        fv_scale: Strength of the FV LoRA.
        seed: Random seed.
        randomize_seed: Pick a random seed instead of `seed`.
        prompt: Your edit instruction (write freely).

    Returns:
        A (before, after) pair for the slider, and the seed used.
    """
    if main_image is None:
        raise gr.Error("Please provide at least the main image.")
    if base_model not in MODES:
        base_model = TURBO

    prompt = (prompt or "").strip()
    if not prompt:
        raise gr.Error("Please write a prompt describing the edit you want.")

    main_image = ImageOps.exif_transpose(main_image).convert("RGB")
    if reference_image is not None:
        reference_image = ImageOps.exif_transpose(reference_image).convert("RGB")

    seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)

    runner = _edit_xlarge if int(resolution) >= 2048 else _edit_large
    result, _ = runner(
        main_image, reference_image, base_model, int(resolution),
        int(num_inference_steps), float(n_lora_scale), float(fv_scale),
        seed, prompt, progress
    )
    return (main_image, result), seed


def on_mode_change(base_model):
    if base_model == TURBO:
        return gr.update(value=DEFAULT_STEPS[TURBO],
                         info="Turbo: 6 is the validated schedule (clamped to 4-8; extra steps go to the high-noise end).")
    return gr.update(value=DEFAULT_STEPS[BASE],
                     info="Base: the official pipeline uses ~40-50 Euler steps; 30 is a good trade-off.")


# ----------------------------------------------------------------------------------------
# UI
# ----------------------------------------------------------------------------------------

css = """
#col-container { margin: 0 auto; max-width: 1200px; }
.image-container img { object-fit: contain; }
.dark .gradio-container { color: var(--body-text-color); }
"""

with gr.Blocks(title="N_lora + FV · Qwen-Image-2.1 Edit") as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(
            "# N_lora + FV · Qwen-Image-2.1 Edit\n"
            "Single or dual-image editing with two stacked LoRAs on [Qwen-Image-2.1](https://huggingface.co/Qwen/Qwen-Image-2.1):\n\n"
            "• **N_lora** – [sanetium/N_lora_sfw_Qwen](https://huggingface.co/sanetium/N_lora_sfw_Qwen)\n"
            "• **FV** – [sanetium/FV](https://huggingface.co/sanetium/FV)\n\n"
            "• Upload **Main Image** (required)\n"
            "• Optionally upload a **Reference Image** for dual-image edit\n"
            "• Write your own prompt\n"
            "Optionally accelerated with [Viggle turbo](https://huggingface.co/Viggle/Qwen-Image-2.1-viggle-turbo) (6 steps)."
        )

        with gr.Row():
            with gr.Column():
                with gr.Row():
                    main_image = gr.Image(
                        label="Main Image (required)",
                        type="pil",
                        sources=["upload", "clipboard"],
                        elem_classes="image-container",
                        height=380,
                    )
                    reference_image = gr.Image(
                        label="Reference Image (optional)",
                        type="pil",
                        sources=["upload", "clipboard"],
                        elem_classes="image-container",
                        height=380,
                    )

                prompt = gr.Textbox(
                    label="Prompt",
                    value="",
                    lines=4,
                    placeholder="Describe the edit you want... (e.g. change the background to a beach at sunset, make her wear a red dress, etc.)",
                    info="Write freely. For dual-image: refer to images as needed in your prompt.",
                )

                base_model = gr.Radio(
                    MODES,
                    value=TURBO,
                    label="Base model",
                    info="Turbo: Viggle's 6-step distilled LoRA (~5x faster). Base: higher quality path.",
                )
                run_button = gr.Button("Generate Edit", variant="primary")

                with gr.Accordion("Advanced Settings", open=False):
                    resolution = gr.Dropdown(
                        RESOLUTIONS,
                        value=1536,
                        label="Resolution",
                        info="Higher resolutions keep more detail. 2K runs on the full GPU (2x quota).",
                    )
                    num_inference_steps = gr.Slider(
                        label="Inference Steps",
                        minimum=4,
                        maximum=50,
                        step=1,
                        value=DEFAULT_STEPS[TURBO],
                        info="Turbo: 6 is the validated schedule (clamped to 4-8).",
                    )
                    n_lora_scale = gr.Slider(
                        label="N_lora strength",
                        minimum=0.0,
                        maximum=1.5,
                        step=0.05,
                        value=1.0,
                    )
                    fv_scale = gr.Slider(
                        label="FV LoRA strength",
                        minimum=0.0,
                        maximum=1.5,
                        step=0.05,
                        value=1.0,
                    )
                    with gr.Row():
                        seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
                        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

            with gr.Column():
                comparison_slider = gr.ImageSlider(label="Before / After", type="pil", format="png")
                seed_output = gr.Number(label="Seed used", interactive=False)

        base_model.change(on_mode_change, inputs=base_model, outputs=num_inference_steps)
        run_button.click(
            edit_image,
            inputs=[
                main_image,
                reference_image,
                base_model,
                resolution,
                num_inference_steps,
                n_lora_scale,
                fv_scale,
                seed,
                randomize_seed,
                prompt,
            ],
            outputs=[comparison_slider, seed_output],
            api_name="edit_image",
        )

if __name__ == "__main__":
    demo.queue(max_size=20).launch(theme=gr.themes.Citrus(), css=css, mcp_server=True)