Spaces:
Running on Zero
Running on Zero
Download app.py from sanetium/testing_lora: direct link, hf CLI and curl.
- Browser
- Download file 15.8 kB
-
https://huggingface.co/spaces/sanetium/testing_lora/resolve/main/app.py
- Command line
-
hf download hf://spaces/sanetium/testing_lora/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sanetium/testing_lora/resolve/main/app.py
15.8 kB
| """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))) | |
| 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 | |
| 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) | |
| 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) |