Onise commited on
Commit
7fb172f
·
verified ·
1 Parent(s): 276cb4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +374 -213
app.py CHANGED
@@ -1,27 +1,20 @@
1
  import os
 
 
2
  import gradio as gr
3
  import numpy as np
4
  import spaces
5
  import torch
6
- import random
7
- from PIL import Image
8
  from typing import Iterable
9
  from gradio.themes import Soft
10
  from gradio.themes.utils import colors, fonts, sizes
11
 
 
12
  colors.steel_blue = colors.Color(
13
  name="steel_blue",
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):
@@ -40,265 +33,433 @@ class SteelBlueTheme(Soft):
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
- print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
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", # [transformer weights extracted from: Phr00t/Qwen-Image-Edit-Rapid-AIO]
105
- subfolder='transformer',
106
  torch_dtype=dtype,
107
- device_map='cuda'
108
  ),
109
- torch_dtype=dtype
110
  ).to(device)
111
 
112
- pipe.load_lora_weights("Daverrrr75/QWEN_MCNL",
113
- weight_name="qwen_MCNL_v1.0.safetensors",
114
- adapter_name="anime")
115
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
116
- weight_name="loras/Meta4.safetensors",
117
- adapter_name="multiple-angles")
118
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
119
- weight_name="loras/bfs_v2_head_000007000.safetensors",
120
- adapter_name="light-restoration")
121
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
122
- weight_name="loras2/consistence_edit_v2.safetensors",
123
- adapter_name="relight")
124
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
125
- weight_name="loras/qwen_image_edit_remove-clothing_v1.0.safetensors",
126
- adapter_name="multi-angle-lighting")
127
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
128
- weight_name="loras-2/qwen真实+一致性-A2R_2509_Base.safetensors",
129
- adapter_name="edit-skin")
130
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
131
- weight_name="loras/p0ssy_lora_v1.safetensors",
132
- adapter_name="next-scene")
133
- pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
134
- weight_name="loras/Qwen4Play_v2.safetensors",
135
- adapter_name="upscale-image")
136
- # pipe.load_lora_weights("lovis93/next-scene-qwen-image-lora-2509",
137
- # weight_name="next-scene_lora-v2-3000.safetensors",
138
- # adapter_name="next-scene")
139
- # pipe.load_lora_weights("wiikoo/Qwen-lora-nsfw",
140
- # weight_name="loras/Qwen_Nsfw_Body_V14-10K.safetensors",
141
- # adapter_name="upscale-image")
142
-
143
-
144
- pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
145
- MAX_SEED = np.iinfo(np.int32).max
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
- def update_dimensions_on_upload(image):
148
- if image is None:
149
- return 1024, 1024
150
-
151
- original_width, original_height = image.size
152
-
153
- if original_width > original_height:
154
- new_width = 1024
155
- aspect_ratio = original_height / original_width
156
- new_height = int(new_width * aspect_ratio)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  else:
158
- new_height = 1024
159
- aspect_ratio = original_width / original_height
160
- new_width = int(new_height * aspect_ratio)
161
-
162
- # Ensure dimensions are multiples of 8
163
- new_width = (new_width // 8) * 8
164
- new_height = (new_height // 8) * 8
165
-
166
- return new_width, new_height
167
-
168
- @spaces.GPU(duration=60)
 
 
169
  def infer(
170
  input_image,
171
  prompt,
172
- lora_adapter,
173
  seed,
174
  randomize_seed,
175
  guidance_scale,
176
  steps,
177
- progress=gr.Progress(track_tqdm=True)
 
178
  ):
 
 
 
 
 
 
 
 
 
179
  if input_image is None:
180
- raise gr.Error("Please upload an image to edit.")
181
-
182
- if lora_adapter == "QWEN_MCNL":
183
- pipe.set_adapters(["anime"], adapter_weights=[1.0])
184
- elif lora_adapter == "Multiple-Angles":
185
- pipe.set_adapters(["multiple-angles"], adapter_weights=[1.0])
186
- elif lora_adapter == "Real PS":
187
- pipe.set_adapters(["light-restoration"], adapter_weights=[1.0])
188
- elif lora_adapter == "Qwen-lora-nsfw":
189
- pipe.set_adapters(["relight", "edit-skin"], adapter_weights=[0.4, 0.6])
190
- elif lora_adapter == "Jib":
191
- pipe.set_adapters(["multi-angle-lighting"], adapter_weights=[1.0])
192
- elif lora_adapter == "A2R_2509_Base":
193
- pipe.set_adapters(["edit-skin"], adapter_weights=[1.0])
194
- elif lora_adapter == "Next-Scene":
195
- pipe.set_adapters(["next-scene"], adapter_weights=[1.0])
196
- elif lora_adapter == "V14-10":
197
- pipe.set_adapters(["upscale-image"], adapter_weights=[1.0])
198
-
 
 
 
 
 
199
  if randomize_seed:
200
  seed = random.randint(0, MAX_SEED)
201
 
202
  generator = torch.Generator(device=device).manual_seed(seed)
203
- 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"
204
-
205
- original_image = input_image.convert("RGB")
206
-
207
- # Use the new function to update dimensions
208
- # width, height = update_dimensions_on_upload(original_image)
209
- width, height = None, None
210
-
211
- result = pipe(
212
- image=original_image,
213
- prompt=prompt,
214
- negative_prompt=negative_prompt,
215
- height=height,
216
- width=width,
217
- num_inference_steps=steps,
218
- generator=generator,
219
- true_cfg_scale=guidance_scale,
220
- ).images[0]
221
-
222
- return result, seed
223
-
224
- @spaces.GPU(duration=60)
225
- def infer_example(input_image, prompt, lora_adapter):
226
- input_pil = input_image.convert("RGB")
227
- guidance_scale = 1.0
228
- steps = 5
229
- result, seed = infer(input_pil, prompt, lora_adapter, 0, True, guidance_scale, steps)
230
- return result, seed
231
-
232
-
233
- css="""
234
- #col-container {
235
- margin: 0 auto;
236
- max-width: 960px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  }
238
- #main-title h1 {font-size: 2.1em !important;}
239
  """
240
 
 
 
241
  with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
242
  with gr.Column(elem_id="col-container"):
243
- gr.Markdown("# **Qwen-Image-Edit-2509-LoRAs-Fast**", elem_id="main-title")
244
- gr.Markdown("Perform diverse image edits using specialized [LoRA](https://huggingface.co/models?other=base_model:adapter:Qwen/Qwen-Image-Edit-2509) adapters for the [Qwen-Image-Edit](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) model.")
 
 
 
245
 
246
- with gr.Row(equal_height=True):
247
  with gr.Column():
248
- input_image = gr.Image(label="Upload Image", type="pil", height=290)
249
-
250
- prompt = gr.Text(
 
 
 
251
  label="Edit Prompt",
252
- show_label=True,
253
- placeholder="e.g., transform into anime..",
254
  )
255
-
256
- run_button = gr.Button("Edit Image", variant="primary")
257
 
258
  with gr.Column():
259
- output_image = gr.Image(label="Output Image", interactive=False, format="png", height=350)
260
-
261
- with gr.Row():
262
- lora_adapter = gr.Dropdown(
263
- label="Choose Editing Style",
264
- choices=["QWEN_MCNL", "Multiple-Angles", "Real PS", "Jib", "V14-10", "Qwen-lora-nsfw", "Next-Scene", "A2R_2509_Base"],
265
- value="QWEN_MCNL"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  )
267
- with gr.Accordion("Advanced Settings", open=False, visible=True):
268
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
269
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
270
- guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
271
- steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
272
-
273
- gr.Examples(
274
- examples=[
275
- ["examples/1.jpg", "Transform into anime.", "Photo-to-Anime"],
276
- ["examples/5.jpg", "Remove shadows and relight the image using soft lighting.", "Light-Restoration"],
277
- ["examples/4.jpg", "Use a subtle golden-hour filter with smooth light diffusion.", "Relight"],
278
- ["examples/2.jpeg", "Rotate the camera 45 degrees to the left.", "Multiple-Angles"],
279
- ["examples/7.jpg", "Light source from the Right Rear", "Multi-Angle-Lighting"],
280
- ["examples/10.jpeg", "Upscale the image.", "Upscale-Image"],
281
- ["examples/7.jpg", "Light source from the Below", "Multi-Angle-Lighting"],
282
- ["examples/2.jpeg", "Switch the camera to a top-down right corner view.", "Multiple-Angles"],
283
- ["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"],
284
- ["examples/8.jpg", "Make the subjects skin details more prominent and natural.", "Edit-Skin"],
285
- ["examples/6.jpg", "Switch the camera to a bottom-up view.", "Multiple-Angles"],
286
- ["examples/6.jpg", "Rotate the camera 180 degrees upside down.", "Multiple-Angles"],
287
- ["examples/4.jpg", "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
288
- ["examples/4.jpg", "Switch the camera to a top-down view.", "Multiple-Angles"],
289
- ["examples/4.jpg", "Switch the camera to a wide-angle lens.", "Multiple-Angles"],
290
- ],
291
- inputs=[input_image, prompt, lora_adapter],
292
- outputs=[output_image, seed],
293
- fn=infer_example,
294
- cache_examples=False,
295
- label="Examples"
296
- )
297
 
298
  run_button.click(
299
  fn=infer,
300
- inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
301
- outputs=[output_image, seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  )
303
-
304
- demo.launch(mcp_server=True, ssr_mode=False, show_error=True)
 
1
  import os
2
+ import gc
3
+ import random
4
  import gradio as gr
5
  import numpy as np
6
  import spaces
7
  import torch
 
 
8
  from typing import Iterable
9
  from gradio.themes import Soft
10
  from gradio.themes.utils import colors, fonts, sizes
11
 
12
+ # ── Theme ──────────────────────────────────────────────────────────────────────
13
  colors.steel_blue = colors.Color(
14
  name="steel_blue",
15
+ c50="#EBF3F8", c100="#D3E5F0", c200="#A8CCE1", c300="#7DB3D2",
16
+ c400="#529AC3", c500="#4682B4", c600="#3E72A0", c700="#36638C",
17
+ c800="#2E5378", c900="#264364", c950="#1E3450",
 
 
 
 
 
 
 
 
18
  )
19
 
20
  class SteelBlueTheme(Soft):
 
33
  ),
34
  ):
35
  super().__init__(
36
+ primary_hue=primary_hue, secondary_hue=secondary_hue,
37
+ neutral_hue=neutral_hue, text_size=text_size, font=font, font_mono=font_mono,
 
 
 
 
38
  )
39
  super().set(
 
 
40
  body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
41
  body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
42
  button_primary_text_color="white",
 
43
  button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
44
  button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
 
 
 
 
 
 
 
 
45
  slider_color="*secondary_500",
 
46
  block_title_text_weight="600",
47
  block_border_width="3px",
48
  block_shadow="*shadow_drop_lg",
 
 
 
 
49
  )
50
 
51
  steel_blue_theme = SteelBlueTheme()
52
 
53
+ # ── Device / dtype ─────────────────────────────────────────────────────────────
54
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
55
+ dtype = torch.bfloat16
56
 
57
+ print("CUDA available:", torch.cuda.is_available())
 
 
 
 
 
 
 
 
58
  print("Using device:", device)
59
 
60
+ # ── Model loading (local qwenimage package + FA3) ──────────────────────────────
61
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
62
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
63
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
64
 
 
 
 
65
  pipe = QwenImageEditPlusPipeline.from_pretrained(
66
  "Qwen/Qwen-Image-Edit-2509",
67
  transformer=QwenImageTransformer2DModel.from_pretrained(
68
+ "prithivMLmods/Qwen-Image-Edit-Rapid-AIO-V4",
 
69
  torch_dtype=dtype,
70
+ device_map="cuda",
71
  ),
72
+ torch_dtype=dtype,
73
  ).to(device)
74
 
75
+ # ── OOM FIX: Enable VAE tiling and slicing to bound VRAM usage ─────────────────
76
+ pipe.vae.enable_tiling(tile_sample_min_width=256, tile_sample_min_height=256)
77
+ pipe.vae.enable_slicing()
78
+ # ───────────────────────────────────────────────────────────────────────────────
79
+
80
+ try:
81
+ pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
82
+ print("Flash Attention 3 Processor set successfully.")
83
+ except Exception as e:
84
+ print(f"Warning: Could not set FA3 processor: {e}")
85
+
86
+ # ── LoRA catalog ──────────────────────────────────────────────────────────
87
+ LORA_REPO = "wiikoo/Qwen-lora-nsfw"
88
+ LORA_CONFIGS = {
89
+ "CockQwen_v3": "loras/CockQwen-v3.safetensors",
90
+ "Eva_Qwen_V3": "loras/Eva_Qwen_V3.safetensors",
91
+ "Facial_Cumshots_V1": "loras/Facial_Cumshots_For_Qwen_Image_V1.safetensors",
92
+ "HearmemanAI_V3_Breasts": "loras/HearmemanAI_V3_Rank64_BreastsLoRA_Epoch60.safetensors",
93
+ "HearmemanAI_V4_Breasts": "loras/HearmemanAI_V4_Rank128_BreastsLoRA_Epoch80.safetensors",
94
+ "InniePussy": "loras/InniePussy.safetensors",
95
+ "JTT2_5": "loras/[QWEN] JTT2_5.safetensors",
96
+ "LumiNude01a": "loras/LumiNude01a_CE_QWEN_AIT3k.safetensors",
97
+ "MEXX_QWEN_TG300": "loras/MEXX_QWEN_TG300_23.safetensors",
98
+ "Meta4": "loras/Meta4.safetensors",
99
+ "MysticXXX": "loras/Qwen-MysticXXX-v1.safetensors",
100
+ "Nsfw_Body_V10": "loras/Qwen_Nsfw_Body_V10-4K.safetensors",
101
+ "Nsfw_Body_V14": "loras/Qwen_Nsfw_Body_V14-10K.safetensors",
102
+ "OilySkin_V2": "loras/Oily Skin QWEN V2-GMR.safetensors",
103
+ "PillowHump_2509": "loras/PillowHump_2509.safetensors",
104
+ "PutItHere_V2": "loras/Put it here_Qwen edit_V2.0.safetensors",
105
+ "PutItHere_V01": "loras/put it here_QwenEdit_V0.1.safetensors",
106
+ "Qwen4Play_v2": "loras/Qwen4Play_v2.safetensors",
107
+ "QwenHentai_v3": "loras/QwenImageHentaiPIV_v3.1.safetensors",
108
+ "Qwen_Helm": "loras/Qwen-Image-Helm_v0.1.safetensors",
109
+ "Qwen_NSFW_Beta1": "loras/Qwen-NSFW.safetensors",
110
+ "Qwen_NSFW_Beta2": "loras/Qwen-NSFW-Beta2.safetensors",
111
+ "Qwen_NSFW_Beta4": "loras/Qwen-NSFW-Beta4.safetensors",
112
+ "Qwen_NSFW_Beta5": "loras/Qwen-NSFW-Beta5.safetensors",
113
+ "Qwen_Real_Nud3s": "loras/Qwen_Real_Nud3s.safetensors",
114
+ "Qwen_Real_PS": "loras/Qwen-Real PS_v1_83K.safetensors",
115
+ "QwenSnofs_v1": "loras/qwen_snofs.safetensors",
116
+ "QwenSnofs_v1_1": "loras/QwenSnofs1_1.safetensors",
117
+ "Real_Breast_Nipples": "loras/Real Breast Nipples-QWEN-[rbn]-GMR.safetensors",
118
+ "SendDudes": "loras/[QWEN] SendDudes.safetensors",
119
+ "SendNudesLite": "loras/SendNudesLite (Qwen).safetensors",
120
+ "SendNudesPro_Beta": "loras/[QWEN] Send Nudes Pro - Beta v1.safetensors",
121
+ "Ultimate_Breast_Nipples": "loras/Ultimate Realistic Breast NIPPLES-QWEN-[rab]-GMR.safetensors",
122
+ "ass_up_QWEN": "loras/ass_up_QWEN.safetensors",
123
+ "barbell_nipples_QWEN": "loras/QWEN_jtn_barbell.safetensors",
124
+ "bfs_v2_face": "loras-sfw/face_swap_5500_qwen_image_edit_2509_v1.safetensors",
125
+ "bfs_v2_focus_face": "loras-sfw/bfs_v2_000005000.safetensors",
126
+ "bfs_v2_head": "loras-sfw/bfs_v2_head_000007000.safetensors",
127
+ "big_nipples_QWEN": "loras/big_nipples_QWEN.safetensors",
128
+ "bumpynipples": "loras/bumpynipples1.safetensors",
129
+ "cmslt_cum_on_her": "loras/cmslt_2509_2.safetensors",
130
+ "consistence_edit_v1": "loras-2/consistence_edit_v1.safetensors",
131
+ "consistence_edit_v2": "loras2/consistence_edit_v2.safetensors",
132
+ "d33p7hroa7": "loras/d33p7hroa7_qwen.safetensors",
133
+ "d1ck_p3n1s_V1_1": "loras/qwen-image_d!ck_P3N1S_LoRA_V1.1.safetensors",
134
+ "goblin_anal_v1": "loras/goblin_anal_v1_qwen.safetensors",
135
+ "horseshoe_nipple_rings": "loras/horseshoe_nipple_rings_QWEN.safetensors",
136
+ "jib_nudity_fixer": "loras/jib_qwen_fix_000002750.safetensors",
137
+ "jillin": "loras/jillin1.safetensors",
138
+ "male_nude": "loras/lora_nudenan_v1.safetensors",
139
+ "milk_juggs": "loras/milk_juggs_QWEN.safetensors",
140
+ "n00d_b": "loras/n00d-b-qwen.safetensors",
141
+ "nsfw_adv_v1": "loras/qwen-image_nsfw_adv_v1.0.safetensors",
142
+ "p0ssy_lora_v1": "loras/p0ssy_lora_v1.safetensors",
143
+ "p3nis": "loras/p3nis.safetensors",
144
+ "qwen_MCNL": "loras/qwen_MCNL_v1.0.safetensors",
145
+ "qwen_PENISLORA": "loras/qwen-PENISLORA.safetensors",
146
+ "qwen_hand_grab": "loras/qwen_hand_grab_6000s.safetensors",
147
+ "qwen_uncensor": "loras/qwen_uncensor_000014928.safetensors",
148
+ "reclining_nude": "loras/reclining_nude_v1_000003500.safetensors",
149
+ "remove_clothing": "loras/qwen_image_edit_remove-clothing_v1.0.safetensors",
150
+ "royal_treatment_V3": "loras/royal+treatment+V3.safetensors",
151
+ "sabi_character": "loras-2/sabi_character_v1.safetensors",
152
+ "snapchat_selfie": "loras/qwen_image_snapchat.safetensors",
153
+ "uka_qwen": "loras/uka_1_qwen.safetensors",
154
+ "ultimate_realistic_breast":"loras/ultimate realistic breast.safetensors",
155
+ }
156
+
157
+ LORA_TRIGGER_WORDS = {
158
+ "Qwen4Play_v2": "d0gg13, c0wg1rl, r3v3rs3_c0wg1rl, m15510n4ry, bl0wj0b, penis",
159
+ "qwen_MCNL": "nsfw, cum_on_face, blowjob, cowgirlout, creamp1e, penis, l1ck, missionary, nipples, reversecowgirlpov, vagina",
160
+ "remove_clothing": "remove her clothing",
161
+ "Qwen_Real_Nud3s": "nud3",
162
+ "HearmemanAI_V4_Breasts": "large breasts, hard nipples, erect nipples",
163
+ "HearmemanAI_V3_Breasts": "large breasts, hard nipples, erect nipples",
164
+ "Ultimate_Breast_Nipples": "rab",
165
+ "ass_up_QWEN": "ass up showing pussy and anus",
166
+ "PillowHump_2509": "Pillow, Humping",
167
+ "InniePussy": "Innie pussy, Clean shaven, Vertical slit",
168
+ "p0ssy_lora_v1": "Nude",
169
+ "CockQwen_v3": "Erect Penis",
170
+ "p3nis": "holding a p3nis",
171
+ "qwen_PENISLORA": "PENISLORA",
172
+ "Facial_Cumshots_V1": "cum",
173
+ "bfs_v2_head": "head swap, transfer head from image 1 to image 2",
174
+ "bfs_v2_face": "keep the face consistent, preserve facial identity",
175
+ "bfs_v2_focus_face": "head swap from Image 1 to Image 2",
176
+ "goblin_anal_v1": "anal penetration, spread ass",
177
+ "d33p7hroa7": "deepthroat, penis deep in mouth",
178
+ "QwenHentai_v3": "nsfw, anime style, explicit",
179
+ "Eva_Qwen_V3": "Eva_gothic, in a kneeling position",
180
+ "JTT2_5": "massive breasts, large breasts, medium breasts, small breasts",
181
+ "MEXX_QWEN_TG300": "nsfw, female body",
182
+ "OilySkin_V2": "oilski",
183
+ "barbell_nipples_QWEN": "barbell nipple piercings",
184
+ "Qwen_Helm": "nsfw, anime style",
185
+ "MysticXXX": "nsfw",
186
+ "Qwen_NSFW_Beta1": "nsfw",
187
+ "Qwen_NSFW_Beta2": "nsfw",
188
+ "Qwen_NSFW_Beta4": "nsfw",
189
+ "Qwen_NSFW_Beta5": "nsfw",
190
+ "QwenSnofs_v1": "sex, missionary, cum, cowgirl, reverse cowgirl, selfie, snapchat selfie, prone position, spooning position, undressing",
191
+ "QwenSnofs_v1_1": "nsfw, nude, sex, blowjob, cum, selfie",
192
+ "Nsfw_Body_V10": "Hourglass figure, Hairless pussy, Hairly pussy",
193
+ "Nsfw_Body_V14": "SSS Waistline, Hairless pussy, Hairly pussy",
194
+ "SendNudesLite": "nude",
195
+ "SendNudesPro_Beta": "flat chest, small breasts, medium breasts, large breasts, massive breasts, big nipples",
196
+ "SendDudes": "Penis",
197
+ "cmslt_cum_on_her": "Put cum on her",
198
+ "horseshoe_nipple_rings": "horseshoe-ring nipple piercings, circular-barbell nipple piercings",
199
+ "jib_nudity_fixer": "nude, nipples, vagina",
200
+ "jillin": "masturbating",
201
+ "male_nude": "nudeman",
202
+ "n00d_b": "nude, art photography",
203
+ "nsfw_adv_v1": "nsfw",
204
+ "d1ck_p3n1s_V1_1": "P3N1S, penis",
205
+ "qwen_uncensor": "nsfw, cum_on_face, blowjob, cowgirlout, creamp1e, penis, l1ck, missionary, nipples, reversecowgirlpov, vagina",
206
+ "royal_treatment_V3": "lick ass, blowjob",
207
+ "snapchat_selfie": "selfie, snapchat",
208
+ "ultimate_realistic_breast":"urb, realistic breast",
209
+ }
210
+
211
+ # Tracks which adapter names have been loaded into the pipeline this session.
212
+ # ZeroGPU resets VRAM on every @spaces.GPU call, so we reload as needed.
213
+ LOADED_ADAPTERS: set[str] = set()
214
+
215
+ # ── Helpers ────────────────────────────────────────────────────────────────────
216
+ def append_triggers(current_prompt: str, lora_name: str) -> str:
217
+ """Append a LoRA's trigger words to the prompt (no duplicates)."""
218
+ if lora_name == "None":
219
+ return current_prompt
220
+ triggers = LORA_TRIGGER_WORDS.get(lora_name, "")
221
+ if not triggers:
222
+ return current_prompt
223
+ existing = {w.strip().lower() for w in current_prompt.replace(",", " ").split()}
224
+ new_words = [w.strip() for w in triggers.split(",")
225
+ if w.strip().lower() not in existing and w.strip()]
226
+ if not new_words:
227
+ return current_prompt
228
+ sep = ", " if current_prompt.strip() else ""
229
+ return current_prompt.rstrip(", ") + sep + ", ".join(new_words)
230
 
231
+
232
+ def load_and_apply_stack(extra_adapters: list[str], extra_weights: list[float]):
233
+ """Lazy-load any unseen adapters, then activate the full stack.
234
+
235
+ Returns the list of adapters that were successfully loaded (some may be
236
+ skipped if the remote file is missing or corrupted).
237
+ """
238
+ if not extra_adapters:
239
+ pipe.disable_lora()
240
+ return [], []
241
+
242
+ loaded, weights_out = [], []
243
+ for name, weight in zip(extra_adapters, extra_weights):
244
+ if name not in LORA_CONFIGS:
245
+ continue
246
+ if name not in LOADED_ADAPTERS:
247
+ try:
248
+ print(f"--- Loading adapter: {name} ---")
249
+ pipe.load_lora_weights(
250
+ LORA_REPO,
251
+ weight_name=LORA_CONFIGS[name],
252
+ adapter_name=name,
253
+ )
254
+ LOADED_ADAPTERS.add(name)
255
+ except Exception as e:
256
+ print(f"WARNING: Failed to load LoRA '{name}': {e}")
257
+ continue
258
+ loaded.append(name)
259
+ weights_out.append(weight)
260
+
261
+ if loaded:
262
+ pipe.enable_lora()
263
+ pipe.set_adapters(loaded, adapter_weights=weights_out)
264
  else:
265
+ pipe.disable_lora()
266
+
267
+ return loaded, weights_out
268
+
269
+ # ── Inference ──────────────────────────────────────────────────────────────────
270
+ MAX_SEED = np.iinfo(np.int32).max
271
+ NEGATIVE_PROMPT = (
272
+ "worst quality, low quality, bad anatomy, bad hands, text, error, "
273
+ "missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, "
274
+ "signature, watermark, username, blurry"
275
+ )
276
+
277
+ @spaces.GPU(duration=120)
278
  def infer(
279
  input_image,
280
  prompt,
 
281
  seed,
282
  randomize_seed,
283
  guidance_scale,
284
  steps,
285
+ *lora_params,
286
+ progress=gr.Progress(track_tqdm=True),
287
  ):
288
+ # ── OOM FIX: Aggressive memory cleanup before inference ──────────────────
289
+ gc.collect()
290
+ torch.cuda.empty_cache()
291
+ # ─────────────────────────────────────────��───────────────────────────────
292
+
293
+ # ── ZeroGPU FIX: GPU state resets between @spaces.GPU calls, so we must
294
+ # clear the adapter tracking set to force reloading on each invocation.
295
+ LOADED_ADAPTERS.clear()
296
+
297
  if input_image is None:
298
+ raise gr.Error("Please upload an image.")
299
+
300
+ # ── Validate aspect ratio ────────────────────────────────────────────────
301
+ # Extreme ratios (>4:1) produce degenerate latent shapes that crash the
302
+ # transformer or produce garbage. Reject early with a clear message.
303
+ image = input_image.convert("RGB")
304
+ w, h = image.size
305
+ ratio = max(w, h) / max(min(w, h), 1)
306
+ if ratio > 4.0:
307
+ raise gr.Error(
308
+ f"Image aspect ratio too extreme ({w}x{h}, ratio {ratio:.1f}:1). "
309
+ "Please use an image with aspect ratio ≤ 4:1."
310
+ )
311
+ # ─────────────────────────────────────────────────────────────────────────
312
+
313
+ extra_adapters, extra_weights = [], []
314
+ for i in range(0, len(lora_params), 2):
315
+ name, strength = lora_params[i], lora_params[i + 1]
316
+ if name != "None" and float(strength) > 0.05:
317
+ extra_adapters.append(name)
318
+ extra_weights.append(float(strength))
319
+
320
+ loaded_adapters, _ = load_and_apply_stack(extra_adapters, extra_weights)
321
+
322
  if randomize_seed:
323
  seed = random.randint(0, MAX_SEED)
324
 
325
  generator = torch.Generator(device=device).manual_seed(seed)
326
+
327
+ try:
328
+ # Pipeline's __call__ is already decorated with @torch.no_grad().
329
+ # Do NOT use torch.inference_mode() here — it breaks LoRA in-place
330
+ # weight scaling (scale_lora_layers / unscale_lora_layers).
331
+ # Let the pipeline auto-calculate output dimensions from the input
332
+ # image's aspect ratio (targeting 1MP, snapped to multiples of 32).
333
+ result = pipe(
334
+ image=image,
335
+ prompt=prompt,
336
+ negative_prompt=NEGATIVE_PROMPT if guidance_scale > 1.0 else None,
337
+ num_inference_steps=steps,
338
+ generator=generator,
339
+ true_cfg_scale=guidance_scale,
340
+ ).images[0]
341
+ return result, seed
342
+ except torch.cuda.OutOfMemoryError:
343
+ gc.collect()
344
+ torch.cuda.empty_cache()
345
+ raise gr.Error(
346
+ "GPU out of memory. Try reducing inference steps or using fewer LoRAs."
347
+ )
348
+ except RuntimeError as e:
349
+ if "CUDA" in str(e) or "out of memory" in str(e).lower():
350
+ gc.collect()
351
+ torch.cuda.empty_cache()
352
+ raise gr.Error(f"GPU error: {e}")
353
+ raise gr.Error(f"Inference failed: {e}")
354
+ finally:
355
+ # ── OOM FIX: Unload LoRAs and clean up after each inference ──────────
356
+ if loaded_adapters:
357
+ pipe.disable_lora()
358
+ gc.collect()
359
+ torch.cuda.empty_cache()
360
+ # ─────────────────────────────────────────────────────────────────────
361
+
362
+ # ── UI ─────────────────────────────────────────────────────────────────────────
363
+ css = """
364
+ #col-container { margin: 0 auto; max-width: 960px; }
365
+ #main-title h1 { font-size: 2.1em !important; }
366
+ /* Preview defaults to cropping (cover-like); contain keeps the full image in frame */
367
+ #col-container .image-frame,
368
+ #col-container .image-frame > div,
369
+ .contain-preview .image-frame,
370
+ .contain-preview .image-frame > div {
371
+ overflow: visible !important;
372
+ }
373
+ #col-container .image-frame img,
374
+ .contain-preview .image-frame img {
375
+ object-fit: contain !important;
376
+ height: auto !important;
377
+ max-height: min(75vh, 900px) !important;
378
+ width: auto !important;
379
+ max-width: 100% !important;
380
+ margin-inline: auto;
381
  }
 
382
  """
383
 
384
+ LORA_NAMES = ["None"] + sorted(LORA_CONFIGS.keys())
385
+
386
  with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
387
  with gr.Column(elem_id="col-container"):
388
+ gr.Markdown("# **Qwen-Image-Edit - 2509", elem_id="main-title")
389
+ gr.Markdown(
390
+ "Base: `Qwen-Image-Edit-2509` + `Qwen-Image-Edit-Rapid-AIO-V4`\n"
391
+ "Add optional extra LoRAs below — selecting one auto-fills its trigger words."
392
+ )
393
 
394
+ with gr.Row(equal_height=False):
395
  with gr.Column():
396
+ input_image = gr.Image(
397
+ label="Input Image",
398
+ type="pil",
399
+ elem_classes=["contain-preview"],
400
+ )
401
+ prompt = gr.Textbox(
402
  label="Edit Prompt",
403
+ placeholder="e.g. nude woman, remove clothing, detailed skin texture...",
404
+ lines=3,
405
  )
406
+ run_button = gr.Button("✨ Edit Image", variant="primary", size="lg")
 
407
 
408
  with gr.Column():
409
+ output_image = gr.Image(
410
+ label="Output",
411
+ interactive=False,
412
+ format="png",
413
+ elem_classes=["contain-preview"],
414
+ )
415
+
416
+ with gr.Accordion("➕ Extra LoRAs (optional)", open=False):
417
+ gr.Markdown(
418
+ "Stack up to 6 LoRAs."
419
+ )
420
+ lora_stack = []
421
+ for i in range(6):
422
+ with gr.Row():
423
+ dd = gr.Dropdown(
424
+ choices=LORA_NAMES,
425
+ value="None",
426
+ label=f"LoRA {i + 1}",
427
+ scale=3,
428
+ )
429
+ sl = gr.Slider(
430
+ 0.0, 1.5, value=0.75,
431
+ step=0.05, label="Strength", scale=2,
432
+ )
433
+ lora_stack.extend([dd, sl])
434
+
435
+ with gr.Accordion("⚙️ Advanced", open=False):
436
+ seed = gr.Slider(
437
+ label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0
438
  )
 
 
439
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
440
+ guidance_scale = gr.Slider(
441
+ label="CFG Scale", minimum=1.0, maximum=5.0, step=0.1, value=1.0
442
+ )
443
+ steps = gr.Slider(
444
+ label="Steps", minimum=1, maximum=30, step=1, value=4
445
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
 
447
  run_button.click(
448
  fn=infer,
449
+ inputs=[input_image, prompt, seed, randomize_seed, guidance_scale, steps]
450
+ + lora_stack,
451
+ outputs=[output_image, seed],
452
+ )
453
+
454
+ # Auto-fill trigger words when a LoRA is selected
455
+ for i in range(0, len(lora_stack), 2):
456
+ lora_stack[i].change(
457
+ fn=append_triggers,
458
+ inputs=[prompt, lora_stack[i]],
459
+ outputs=[prompt],
460
+ )
461
+
462
+ if __name__ == "__main__":
463
+ demo.queue(max_size=30).launch(
464
+ mcp_server=True, ssr_mode=False, show_error=True
465
  )