sanetium commited on
Commit
05eb148
·
verified ·
1 Parent(s): 57b88ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -97
app.py CHANGED
@@ -1,14 +1,14 @@
1
- """BFS (Best Face Swap) head-swap LoRA for Qwen-Image-2.1, on ZeroGPU.
2
  -------------------------------------------------------------------------------------------------------------
3
 
4
- Pipeline (mirrors the author's "Head Swap V1 Qwen 2.1" ComfyUI workflow):
5
- image 1 = target body/scene, image 2 = reference head, the author's head_swap prompt,
6
- N_lora at strength 1.0, cfg 1, output follows image 1's aspect ratio.
7
 
 
8
  Two base-model modes:
9
- * Turbo - Viggle/Qwen-Image-2.1-viggle-turbo LoRA stacked under N_lora, 6 steps on the
10
- shipped sigma nodes, scheduler with shift_terminal=None (model card recipe).
11
- * Base - plain Qwen/Qwen-Image-2.1 with N_lora only, Euler flow-matching, ~30 steps.
12
  """
13
 
14
  import os
@@ -43,14 +43,6 @@ MODES = [TURBO, BASE]
43
  DEFAULT_STEPS = {TURBO: 6, BASE: 30}
44
  RESOLUTIONS = [("1K (1024² area)", 1024), ("1.5K (1536² area)", 1536), ("2K (2048² area, full GPU)", 2048)]
45
 
46
- # The author's prompt from the model card / workflow (TextEncodeQwenImage21 node).
47
- FACE_SWAP_PROMPT = (
48
- "head_swap: start with <image1> as the base image, keeping its lighting, environment, and background. "
49
- "remove the head from <image1> completely and replace it with the head from <image2>, strictly preserving "
50
- "the hair, eye color, nose structure from <image2>. copy the direction of the eye, head rotation, micro "
51
- "expressions from <image1>, high quality, sharp details, 4k"
52
- )
53
-
54
  # ----------------------------------------------------------------------------------------
55
  # Models
56
  # ----------------------------------------------------------------------------------------
@@ -104,7 +96,7 @@ def kv_cache_fits(resolution: int, budget_gb: float) -> bool:
104
  return prefix_tokens * 32 * 2 * 4096 * 2 <= budget_gb * 1e9
105
 
106
 
107
- def estimate_duration(reference_face=None, target_image=None, base_model=TURBO, resolution=1536,
108
  num_inference_steps=6, *args, **kwargs):
109
  try:
110
  res, steps = int(resolution), int(num_inference_steps)
@@ -112,8 +104,6 @@ def estimate_duration(reference_face=None, target_image=None, base_model=TURBO,
112
  res, steps = 1536, 6
113
  if base_model == TURBO:
114
  steps = min(max(steps, 4), 8)
115
- # Fitted to [swap] timings measured on this Space (turbo 6 steps / base 30 steps):
116
- # 1024: 8.9s / 24.0s, 1536: 22.6s / 70.0s (48 GB slice); 2048 turbo: 23.0s (full card).
117
  per_step = {1024: 0.65, 1536: 2.0, 2048: 2.6}.get(res, 2.0)
118
  fixed = {1024: 6, 1536: 11, 2048: 9}.get(res, 11)
119
  return int(min(300, math.ceil((fixed + steps * per_step) * 1.15 + 3)))
@@ -140,7 +130,7 @@ def decode_latents(latents, height, width, tile_decode):
140
  return pipe.image_processor.postprocess(image, output_type="pil")[0]
141
 
142
 
143
- def _run(reference_face, target_image, base_model, resolution, num_inference_steps, lora_scale,
144
  seed, prompt, kv_budget_gb, tile_decode):
145
  res = int(resolution)
146
  if base_model == TURBO:
@@ -152,13 +142,19 @@ def _run(reference_face, target_image, base_model, resolution, num_inference_ste
152
  pipe.scheduler = base_scheduler
153
  steps, sigmas = int(num_inference_steps), None
154
 
155
- # Output follows image 1 (the target), like the ComfyUI workflow; both images are
156
- # resized to the same pixel budget before encoding.
157
- width, height, _ = calculate_dimensions(res * res, target_image.width / target_image.height)
 
 
 
 
 
 
158
  started = time.perf_counter()
159
  latents = pipe(
160
  prompt=prompt,
161
- image=[target_image, reference_face],
162
  width=width,
163
  height=height,
164
  output_resolution=res,
@@ -171,68 +167,72 @@ def _run(reference_face, target_image, base_model, resolution, num_inference_ste
171
  ).images
172
  image = decode_latents(latents, height, width, tile_decode)
173
  elapsed = time.perf_counter() - started
174
- print(f"[swap] mode={base_model} res={res} size={image.size} steps={steps} "
175
- f"lora={lora_scale} elapsed={elapsed:.1f}s", flush=True)
176
  return image.convert("RGB"), elapsed
177
 
178
 
179
  @spaces.GPU(duration=estimate_duration)
180
- def _swap_large(reference_face, target_image, base_model, resolution, num_inference_steps,
181
  lora_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
182
- return _run(reference_face, target_image, base_model, resolution, num_inference_steps,
183
  lora_scale, seed, prompt, kv_budget_gb=10.0, tile_decode=True)
184
 
185
 
186
  @spaces.GPU(duration=estimate_duration, size="xlarge")
187
- def _swap_xlarge(reference_face, target_image, base_model, resolution, num_inference_steps,
188
  lora_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
189
- return _run(reference_face, target_image, base_model, resolution, num_inference_steps,
190
  lora_scale, seed, prompt, kv_budget_gb=30.0, tile_decode=False)
191
 
192
 
193
- def face_swap(
194
- reference_face: Image.Image,
195
- target_image: Image.Image,
196
  base_model: str = TURBO,
197
  resolution: int = 1536,
198
  num_inference_steps: int = 6,
199
  lora_scale: float = 1.0,
200
  seed: int = 0,
201
  randomize_seed: bool = True,
202
- prompt: str = FACE_SWAP_PROMPT,
203
  progress=gr.Progress(track_tqdm=True),
204
  ):
205
- """Swap the head of the person in `target_image` with the head from `reference_face`.
206
-
207
- Uses the N_lora (NSFW Qwen by TheseAlpacas V2) on Qwen-Image-2.1.
208
 
209
  Args:
210
- reference_face: Image of the face/head to transfer (becomes <image2>).
211
- target_image: Body/scene image whose head is replaced (becomes <image1>); output follows its framing.
212
- base_model: "Turbo · Viggle 6-step" (fast, Viggle turbo LoRA stacked) or "Base · Qwen-Image-2.1" (slower).
213
- resolution: Output pixel budget side (1024, 1536 or 2048); higher keeps more facial detail.
214
- num_inference_steps: Denoising steps (Turbo: 4-8, 6 recommended; Base: ~30).
215
  lora_scale: N_lora strength (1.0 recommended).
216
  seed: Random seed.
217
  randomize_seed: Pick a random seed instead of `seed`.
218
- prompt: The head-swap instruction; refer to the images as <image1> (target) and <image2> (reference).
219
 
220
  Returns:
221
- A (target, result) pair for the before/after slider, and the seed used.
222
  """
223
- if reference_face is None or target_image is None:
224
- raise gr.Error("Please provide both a reference face and a target image.")
225
  if base_model not in MODES:
226
  base_model = TURBO
227
- prompt = (prompt or "").strip() or FACE_SWAP_PROMPT
228
- reference_face = ImageOps.exif_transpose(reference_face).convert("RGB")
229
- target_image = ImageOps.exif_transpose(target_image).convert("RGB")
 
 
 
 
 
 
230
  seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
231
 
232
- runner = _swap_xlarge if int(resolution) >= 2048 else _swap_large
233
- result, _ = runner(reference_face, target_image, base_model, int(resolution),
234
  int(num_inference_steps), float(lora_scale), seed, prompt, progress)
235
- return (target_image, result), seed
236
 
237
 
238
  def on_mode_change(base_model):
@@ -244,7 +244,7 @@ def on_mode_change(base_model):
244
 
245
 
246
  # ----------------------------------------------------------------------------------------
247
- # UI (layout follows linoyts/Flux2-Klein-Face-Swap)
248
  # ----------------------------------------------------------------------------------------
249
 
250
  css = """
@@ -253,70 +253,98 @@ css = """
253
  .dark .gradio-container { color: var(--body-text-color); }
254
  """
255
 
256
- EX = "examples"
257
- EXAMPLES = [
258
- [f"{EX}/reference_man.jpg", f"{EX}/target_1.jpg"],
259
- [f"{EX}/reference_man.jpg", f"{EX}/target_3.jpg"],
260
- [f"{EX}/reference_woman.jpg", f"{EX}/target_4.jpg"],
261
- [f"{EX}/reference_woman.jpg", f"{EX}/target_5.jpg"],
262
- [f"{EX}/reference_woman.jpg", f"{EX}/target_7.jpg"],
263
- ]
264
-
265
- with gr.Blocks(title="N_lora · Qwen-Image-2.1") as demo:
266
  with gr.Column(elem_id="col-container"):
267
  gr.Markdown(
268
- "# N_lora · Qwen-Image-2.1\n"
269
- "Using [sanetium/N_lora_sfw_Qwen](https://huggingface.co/sanetium/N_lora_sfw_Qwen) "
270
- "`NSFW Qwen by TheseAlpacas V2.safetensors` on [Qwen-Image-2.1](https://huggingface.co/Qwen/Qwen-Image-2.1), optionally "
271
- "accelerated with [Viggle turbo](https://huggingface.co/Viggle/Qwen-Image-2.1-viggle-turbo) (6 steps). "
272
- "Upload the face to transfer and the target body/scene. Only use images of people who have given consent."
 
 
273
  )
274
 
275
  with gr.Row():
276
  with gr.Column():
277
  with gr.Row():
278
- reference_face = gr.Image(label="Reference Face", type="pil", sources=["upload", "clipboard"],
279
- elem_classes="image-container", height=380)
280
- target_image = gr.Image(label="Target Image (Body/Scene)", type="pil",
281
- sources=["upload", "clipboard"], elem_classes="image-container",
282
- height=380)
283
- base_model = gr.Radio(MODES, value=TURBO, label="Base model",
284
- info="Turbo: Viggle's 6-step distilled LoRA (~5x faster). Base: 40-step-class quality path.")
285
- run_button = gr.Button("Swap Face", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  with gr.Accordion("Advanced Settings", open=False):
287
- resolution = gr.Dropdown(RESOLUTIONS, value=1536, label="Resolution",
288
- info="Higher resolutions keep more detail. 2K runs on the full GPU (2x quota).")
289
- num_inference_steps = gr.Slider(label="Inference Steps", minimum=4, maximum=50, step=1,
290
- value=DEFAULT_STEPS[TURBO],
291
- info="Turbo: 6 is the validated schedule (clamped to 4-8; extra steps go to the high-noise end).")
292
- lora_scale = gr.Slider(label="N_lora strength", minimum=0.0, maximum=1.5, step=0.05, value=1.0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  with gr.Row():
294
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
295
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
296
- prompt = gr.Textbox(label="Prompt", value=FACE_SWAP_PROMPT, lines=4,
297
- info="<image1> = target, <image2> = reference face.")
298
 
299
  with gr.Column():
300
  comparison_slider = gr.ImageSlider(label="Before / After", type="pil", format="png")
301
  seed_output = gr.Number(label="Seed used", interactive=False)
302
 
303
- gr.Examples(
304
- examples=EXAMPLES,
305
- inputs=[reference_face, target_image],
306
- outputs=[comparison_slider, seed_output],
307
- fn=face_swap,
308
- cache_examples=True,
309
- cache_mode="lazy",
310
- label="Examples",
311
- )
312
-
313
  base_model.change(on_mode_change, inputs=base_model, outputs=num_inference_steps)
314
  run_button.click(
315
- face_swap,
316
- inputs=[reference_face, target_image, base_model, resolution, num_inference_steps, lora_scale,
317
- seed, randomize_seed, prompt],
 
 
 
 
 
 
 
 
 
318
  outputs=[comparison_slider, seed_output],
319
- api_name="face_swap",
320
  )
321
 
322
  if __name__ == "__main__":
 
1
+ """Qwen-Image-2.1 image edit with N_lora (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
+ N_lora at strength 1.0, cfg 1.
9
  Two base-model modes:
10
+ * Turbo - Viggle/Qwen-Image-2.1-viggle-turbo LoRA stacked under N_lora, 6 steps.
11
+ * Base - plain Qwen/Qwen-Image-2.1 with N_lora only, ~30 steps.
 
12
  """
13
 
14
  import os
 
43
  DEFAULT_STEPS = {TURBO: 6, BASE: 30}
44
  RESOLUTIONS = [("1K (1024² area)", 1024), ("1.5K (1536² area)", 1536), ("2K (2048² area, full GPU)", 2048)]
45
 
 
 
 
 
 
 
 
 
46
  # ----------------------------------------------------------------------------------------
47
  # Models
48
  # ----------------------------------------------------------------------------------------
 
96
  return prefix_tokens * 32 * 2 * 4096 * 2 <= budget_gb * 1e9
97
 
98
 
99
+ def estimate_duration(main_image=None, reference_image=None, base_model=TURBO, resolution=1536,
100
  num_inference_steps=6, *args, **kwargs):
101
  try:
102
  res, steps = int(resolution), int(num_inference_steps)
 
104
  res, steps = 1536, 6
105
  if base_model == TURBO:
106
  steps = min(max(steps, 4), 8)
 
 
107
  per_step = {1024: 0.65, 1536: 2.0, 2048: 2.6}.get(res, 2.0)
108
  fixed = {1024: 6, 1536: 11, 2048: 9}.get(res, 11)
109
  return int(min(300, math.ceil((fixed + steps * per_step) * 1.15 + 3)))
 
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, lora_scale,
134
  seed, prompt, kv_budget_gb, tile_decode):
135
  res = int(resolution)
136
  if base_model == TURBO:
 
142
  pipe.scheduler = base_scheduler
143
  steps, sigmas = int(num_inference_steps), None
144
 
145
+ # Output follows the main image aspect ratio
146
+ width, height, _ = calculate_dimensions(res * res, main_image.width / main_image.height)
147
+
148
+ # Build image list: single or dual
149
+ if reference_image is not None:
150
+ images = [main_image, reference_image]
151
+ else:
152
+ images = [main_image]
153
+
154
  started = time.perf_counter()
155
  latents = pipe(
156
  prompt=prompt,
157
+ image=images,
158
  width=width,
159
  height=height,
160
  output_resolution=res,
 
167
  ).images
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"lora={lora_scale} images={len(images)} elapsed={elapsed:.1f}s", flush=True)
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
  lora_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
178
+ return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
179
  lora_scale, seed, prompt, kv_budget_gb=10.0, tile_decode=True)
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
  lora_scale, seed, prompt, progress=gr.Progress(track_tqdm=True)):
185
+ return _run(main_image, reference_image, base_model, resolution, num_inference_steps,
186
  lora_scale, seed, prompt, kv_budget_gb=30.0, tile_decode=False)
187
 
188
 
189
+ def edit_image(
190
+ main_image: Image.Image,
191
+ reference_image: Image.Image = None,
192
  base_model: str = TURBO,
193
  resolution: int = 1536,
194
  num_inference_steps: int = 6,
195
  lora_scale: float = 1.0,
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).
205
+ reference_image: Optional second image (reference / style / identity etc.).
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
  lora_scale: N_lora strength (1.0 recommended).
210
  seed: Random seed.
211
  randomize_seed: Pick a random seed instead of `seed`.
212
+ prompt: Your edit instruction (write freely).
213
 
214
  Returns:
215
+ A (before, after) pair for the slider, and the seed used.
216
  """
217
+ if main_image is None:
218
+ raise gr.Error("Please provide at least the main image.")
219
  if base_model not in MODES:
220
  base_model = TURBO
221
+
222
+ prompt = (prompt or "").strip()
223
+ if not prompt:
224
+ raise gr.Error("Please write a prompt describing the edit you want.")
225
+
226
+ main_image = ImageOps.exif_transpose(main_image).convert("RGB")
227
+ if reference_image is not None:
228
+ reference_image = ImageOps.exif_transpose(reference_image).convert("RGB")
229
+
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(main_image, reference_image, base_model, int(resolution),
234
  int(num_inference_steps), float(lora_scale), seed, prompt, progress)
235
+ return (main_image, result), seed
236
 
237
 
238
  def on_mode_change(base_model):
 
244
 
245
 
246
  # ----------------------------------------------------------------------------------------
247
+ # UI
248
  # ----------------------------------------------------------------------------------------
249
 
250
  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 [sanetium/N_lora_sfw_Qwen](https://huggingface.co/sanetium/N_lora_sfw_Qwen) "
261
+ "`NSFW Qwen by TheseAlpacas V2.safetensors` on [Qwen-Image-2.1](https://huggingface.co/Qwen/Qwen-Image-2.1).\n\n"
262
+ "• Upload **Main Image** (required)\n"
263
+ "• Optionally upload a **Reference Image** for dual-image edit\n"
264
+ "• Write your own prompt\n"
265
+ "Optionally accelerated with [Viggle turbo](https://huggingface.co/Viggle/Qwen-Image-2.1-viggle-turbo) (6 steps)."
266
  )
267
 
268
  with gr.Row():
269
  with gr.Column():
270
  with gr.Row():
271
+ main_image = gr.Image(
272
+ label="Main Image (required)",
273
+ type="pil",
274
+ sources=["upload", "clipboard"],
275
+ elem_classes="image-container",
276
+ height=380,
277
+ )
278
+ reference_image = gr.Image(
279
+ label="Reference Image (optional)",
280
+ type="pil",
281
+ sources=["upload", "clipboard"],
282
+ elem_classes="image-container",
283
+ height=380,
284
+ )
285
+
286
+ prompt = gr.Textbox(
287
+ label="Prompt",
288
+ value="",
289
+ lines=4,
290
+ placeholder="Describe the edit you want... (e.g. change the background to a beach at sunset, make her wear a red dress, etc.)",
291
+ info="Write freely. For dual-image: refer to images as needed in your prompt.",
292
+ )
293
+
294
+ base_model = gr.Radio(
295
+ MODES,
296
+ value=TURBO,
297
+ label="Base model",
298
+ info="Turbo: Viggle's 6-step distilled LoRA (~5x faster). Base: higher quality path.",
299
+ )
300
+ run_button = gr.Button("Generate Edit", variant="primary")
301
+
302
  with gr.Accordion("Advanced Settings", open=False):
303
+ resolution = gr.Dropdown(
304
+ RESOLUTIONS,
305
+ value=1536,
306
+ label="Resolution",
307
+ info="Higher resolutions keep more detail. 2K runs on the full GPU (2x quota).",
308
+ )
309
+ num_inference_steps = gr.Slider(
310
+ label="Inference Steps",
311
+ minimum=4,
312
+ maximum=50,
313
+ step=1,
314
+ value=DEFAULT_STEPS[TURBO],
315
+ info="Turbo: 6 is the validated schedule (clamped to 4-8).",
316
+ )
317
+ lora_scale = gr.Slider(
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)
 
 
327
 
328
  with gr.Column():
329
  comparison_slider = gr.ImageSlider(label="Before / After", type="pil", format="png")
330
  seed_output = gr.Number(label="Seed used", interactive=False)
331
 
 
 
 
 
 
 
 
 
 
 
332
  base_model.change(on_mode_change, inputs=base_model, outputs=num_inference_steps)
333
  run_button.click(
334
+ edit_image,
335
+ inputs=[
336
+ main_image,
337
+ reference_image,
338
+ base_model,
339
+ resolution,
340
+ num_inference_steps,
341
+ lora_scale,
342
+ seed,
343
+ randomize_seed,
344
+ prompt,
345
+ ],
346
  outputs=[comparison_slider, seed_output],
347
+ api_name="edit_image",
348
  )
349
 
350
  if __name__ == "__main__":