multimodalart HF Staff commited on
Commit
856914d
·
verified ·
1 Parent(s): 08f71fc

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +55 -29
app.py CHANGED
@@ -248,6 +248,47 @@ def estimate_duration(
248
 
249
 
250
  @spaces.GPU(duration=estimate_duration)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  def generate_image(
252
  prompt: str = "",
253
  input_images=None,
@@ -257,7 +298,6 @@ def generate_image(
257
  seed: int = 0,
258
  resolution: int = 1024,
259
  aspect_ratio: str = "Auto",
260
- progress=gr.Progress(track_tqdm=True),
261
  ):
262
  """Generate or edit an image with Qwen-Image-2.1.
263
 
@@ -277,6 +317,9 @@ def generate_image(
277
  Returns:
278
  The generated image, as RGBA (the model natively supports transparency).
279
  """
 
 
 
280
  prompt = (prompt or "").strip()
281
  if not prompt:
282
  raise gr.Error("Please enter a prompt.")
@@ -285,38 +328,21 @@ def generate_image(
285
  if len(image_paths) > MAX_INPUT_IMAGES:
286
  raise gr.Error(f"Up to {MAX_INPUT_IMAGES} input images are supported.")
287
 
288
- # The UI already screened this in `prepare`; repeating it here is cheap (a 270M CPU
289
- # classifier) and keeps the guard in force for callers that hit /generate directly.
290
  if image_paths:
291
  check_guard(prompt)
292
 
293
- condition_images = [Image.open(p) for p in image_paths] or None
294
- width, height = resolve_size(resolution, aspect_ratio)
295
-
296
- negative_prompt = (negative_prompt or "").strip()
297
- call_kwargs = {}
298
- if negative_prompt and float(true_cfg_scale) > 1.0:
299
- call_kwargs["negative_prompt"] = negative_prompt
300
- call_kwargs["true_cfg_scale"] = float(true_cfg_scale)
301
-
302
- started = time.perf_counter()
303
- image = pipe(
304
- prompt=prompt,
305
- image=condition_images,
306
- height=height,
307
- width=width,
308
- output_resolution=int(resolution),
309
- num_inference_steps=int(num_inference_steps),
310
- generator=torch.Generator(device="cuda").manual_seed(int(seed) % (MAX_SEED + 1)),
311
- **call_kwargs,
312
- ).images[0]
313
- print(
314
- f"[generate] images={len(image_paths)} steps={num_inference_steps} "
315
- f"res={resolution} ratio={aspect_ratio} size={image.size} "
316
- f"elapsed={time.perf_counter() - started:.1f}s",
317
- flush=True,
318
  )
319
- return image
320
 
321
 
322
  def prepare(prompt, input_images, enhance, seed, randomize_seed, aspect_ratio):
 
248
 
249
 
250
  @spaces.GPU(duration=estimate_duration)
251
+ def _diffuse(
252
+ prompt: str,
253
+ image_paths: list,
254
+ negative_prompt: str,
255
+ true_cfg_scale: float,
256
+ num_inference_steps: int,
257
+ seed: int,
258
+ resolution: int,
259
+ aspect_ratio: str,
260
+ progress=gr.Progress(track_tqdm=True),
261
+ ):
262
+ """Run the diffusion pipeline. Inputs are already validated and screened."""
263
+ condition_images = [Image.open(p) for p in image_paths] or None
264
+ width, height = resolve_size(resolution, aspect_ratio)
265
+
266
+ negative_prompt = (negative_prompt or "").strip()
267
+ call_kwargs = {}
268
+ if negative_prompt and float(true_cfg_scale) > 1.0:
269
+ call_kwargs["negative_prompt"] = negative_prompt
270
+ call_kwargs["true_cfg_scale"] = float(true_cfg_scale)
271
+
272
+ started = time.perf_counter()
273
+ image = pipe(
274
+ prompt=prompt,
275
+ image=condition_images,
276
+ height=height,
277
+ width=width,
278
+ output_resolution=int(resolution),
279
+ num_inference_steps=int(num_inference_steps),
280
+ generator=torch.Generator(device="cuda").manual_seed(int(seed) % (MAX_SEED + 1)),
281
+ **call_kwargs,
282
+ ).images[0]
283
+ print(
284
+ f"[generate] images={len(image_paths)} steps={num_inference_steps} "
285
+ f"res={resolution} ratio={aspect_ratio} size={image.size} "
286
+ f"elapsed={time.perf_counter() - started:.1f}s",
287
+ flush=True,
288
+ )
289
+ return image
290
+
291
+
292
  def generate_image(
293
  prompt: str = "",
294
  input_images=None,
 
298
  seed: int = 0,
299
  resolution: int = 1024,
300
  aspect_ratio: str = "Auto",
 
301
  ):
302
  """Generate or edit an image with Qwen-Image-2.1.
303
 
 
317
  Returns:
318
  The generated image, as RGBA (the model natively supports transparency).
319
  """
320
+ # Validation and screening deliberately happen here, in the main process: a rejected
321
+ # prompt must never reserve a GPU slot, and gr.Error raised inside the ZeroGPU worker
322
+ # reaches the browser as a bare "RuntimeError" instead of its message.
323
  prompt = (prompt or "").strip()
324
  if not prompt:
325
  raise gr.Error("Please enter a prompt.")
 
328
  if len(image_paths) > MAX_INPUT_IMAGES:
329
  raise gr.Error(f"Up to {MAX_INPUT_IMAGES} input images are supported.")
330
 
331
+ # `prepare` already screened the UI path; this keeps the guard in force for callers
332
+ # that hit /generate directly.
333
  if image_paths:
334
  check_guard(prompt)
335
 
336
+ return _diffuse(
337
+ prompt,
338
+ image_paths,
339
+ negative_prompt,
340
+ true_cfg_scale,
341
+ num_inference_steps,
342
+ seed,
343
+ resolution,
344
+ aspect_ratio,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  )
 
346
 
347
 
348
  def prepare(prompt, input_images, enhance, seed, randomize_seed, aspect_ratio):