multimodalart HF Staff commited on
Commit
d773cbc
·
verified ·
1 Parent(s): 624b65c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +48 -6
app.py CHANGED
@@ -28,7 +28,11 @@ from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchE
28
  from transformers.models.qwen2_5_vl import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
29
  from transformers.models.qwen2 import Qwen2Tokenizer
30
 
31
- from models.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
 
 
 
 
32
  from models.styctrl import register_styctrl, add_styctrl, activate
33
  from models.transformer_qwenimage import QwenImageTransformer2DModel
34
 
@@ -68,8 +72,37 @@ DEFAULT_PROMPT = (
68
  )
69
  DEFAULT_STEPS = 16
70
  DEFAULT_SEED = 42
71
- DEFAULT_HEIGHT = 1024
72
- DEFAULT_WIDTH = 1024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # ---------------------------------------------------------------------------
75
  # Download helper
@@ -359,8 +392,15 @@ def stylize(
359
  return None
360
 
361
  logger.info("stylize called: strength=%.2f, seed=%d, steps=%d", strength, seed, steps)
362
- content_image = content_image.convert("RGB").resize((DEFAULT_WIDTH, DEFAULT_HEIGHT))
363
- style_image = style_image.convert("RGB").resize((DEFAULT_WIDTH, DEFAULT_HEIGHT))
 
 
 
 
 
 
 
364
 
365
  # Interpolate projector states at the requested strength
366
  logger.info("Interpolating projector states ...")
@@ -395,6 +435,8 @@ def stylize(
395
  output = _pipe(
396
  image=[content_image, style_image],
397
  prompt=DEFAULT_PROMPT,
 
 
398
  attention_kwargs=attention_kwargs,
399
  num_inference_steps=steps,
400
  generator=generator,
@@ -482,7 +524,7 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
482
 
483
  gr.Examples(
484
  examples=[
485
- ["examples/content_0000.jpg", "examples/style_1011.jpg", 0.6],
486
  ["examples/content_fox.jpg", "examples/style_anime.jpg", 0.7],
487
  ["examples/content_astronaut.jpg", "examples/style_graffiti.jpg", 0.8],
488
  ["examples/content_owl.jpg", "examples/style_0005.jpg", 0.55],
 
28
  from transformers.models.qwen2_5_vl import Qwen2_5_VLForConditionalGeneration, Qwen2_5_VLProcessor
29
  from transformers.models.qwen2 import Qwen2Tokenizer
30
 
31
+ from models.pipeline_qwenimage_edit_plus import (
32
+ QwenImageEditPlusPipeline,
33
+ calculate_dimensions,
34
+ VAE_IMAGE_SIZE,
35
+ )
36
  from models.styctrl import register_styctrl, add_styctrl, activate
37
  from models.transformer_qwenimage import QwenImageTransformer2DModel
38
 
 
72
  )
73
  DEFAULT_STEPS = 16
74
  DEFAULT_SEED = 42
75
+
76
+
77
+ def _resize_to_content_aspect(content_image: Image.Image):
78
+ """Size the generation to the CONTENT image's aspect ratio.
79
+
80
+ The output aspect ratio must be dictated by the input (content) image, not
81
+ the style reference. We snap the content aspect ratio to the closest model-
82
+ supported resolution around the ideal inference area (~1 megapixel, on a
83
+ 32-pixel grid), center-crop the content image to exactly that aspect ratio,
84
+ and resize it to those dimensions. Returns (cropped_content, width, height)
85
+ so the caller can pass explicit width/height to the pipeline. Works for any
86
+ arbitrary user-uploaded input resolution.
87
+ """
88
+ src_w, src_h = content_image.size
89
+ target_w, target_h = calculate_dimensions(VAE_IMAGE_SIZE, src_w / src_h)
90
+
91
+ # Center-crop the content image to the target aspect ratio, then resize.
92
+ target_ratio = target_w / target_h
93
+ src_ratio = src_w / src_h
94
+ if src_ratio > target_ratio:
95
+ # Source is wider than target: crop width.
96
+ new_w = round(src_h * target_ratio)
97
+ left = (src_w - new_w) // 2
98
+ box = (left, 0, left + new_w, src_h)
99
+ else:
100
+ # Source is taller than target: crop height.
101
+ new_h = round(src_w / target_ratio)
102
+ top = (src_h - new_h) // 2
103
+ box = (0, top, src_w, top + new_h)
104
+ cropped = content_image.crop(box).resize((target_w, target_h), Image.LANCZOS)
105
+ return cropped, target_w, target_h
106
 
107
  # ---------------------------------------------------------------------------
108
  # Download helper
 
392
  return None
393
 
394
  logger.info("stylize called: strength=%.2f, seed=%d, steps=%d", strength, seed, steps)
395
+ content_image = content_image.convert("RGB")
396
+ style_image = style_image.convert("RGB")
397
+
398
+ # The OUTPUT aspect ratio is dictated by the CONTENT image (cropped to the
399
+ # closest supported aspect ratio at the model's ideal inference resolution),
400
+ # NOT by the style reference. The style image keeps its own aspect ratio and
401
+ # is resized adaptively inside the pipeline for conditioning only.
402
+ content_image, out_w, out_h = _resize_to_content_aspect(content_image)
403
+ logger.info("Content-driven output size: %dx%d (WxH)", out_w, out_h)
404
 
405
  # Interpolate projector states at the requested strength
406
  logger.info("Interpolating projector states ...")
 
435
  output = _pipe(
436
  image=[content_image, style_image],
437
  prompt=DEFAULT_PROMPT,
438
+ height=out_h,
439
+ width=out_w,
440
  attention_kwargs=attention_kwargs,
441
  num_inference_steps=steps,
442
  generator=generator,
 
524
 
525
  gr.Examples(
526
  examples=[
527
+ ["examples/content_lighthouse.jpg", "examples/style_aurora.jpg", 0.6],
528
  ["examples/content_fox.jpg", "examples/style_anime.jpg", 0.7],
529
  ["examples/content_astronaut.jpg", "examples/style_graffiti.jpg", 0.8],
530
  ["examples/content_owl.jpg", "examples/style_0005.jpg", 0.55],