Chipsdfgds commited on
Commit
037ebc2
·
verified ·
1 Parent(s): 9edaf6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -80,7 +80,6 @@ class OrangeRedTheme(Soft):
80
  orange_red_theme = OrangeRedTheme()
81
 
82
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
83
-
84
  print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
85
  print("torch.__version__ =", torch.__version__)
86
  print("Using device:", device)
@@ -110,29 +109,29 @@ except Exception as e:
110
 
111
  # 不加 VAE 显存优化。因为 VAE Tiling/Slicing 会增加生成耗时,进而消耗你的 ZeroGPU Quota。
112
  # A100 处理 1024 分辨率足够,所以全速运行即可。
113
-
114
  MAX_SEED = np.iinfo(np.int32).max
115
 
116
- def update_dimensions_on_upload(image, max_size=768):
 
 
 
 
117
  if image is None:
118
  return max_size, max_size
119
-
120
  original_width, original_height = image.size
121
 
122
  # Optimization: Do not upscale small images unnecessarily
123
  if original_width <= max_size and original_height <= max_size:
124
  new_width, new_height = original_width, original_height
125
- elif original_width > original_height:
126
- new_width = max_size
127
- aspect_ratio = original_height / original_width
128
- new_height = int(new_width * aspect_ratio)
129
  else:
130
- new_height = max_size
131
- aspect_ratio = original_width / original_height
132
- new_width = int(new_height * aspect_ratio)
 
133
 
134
- new_width = (new_width // 8) * 8
135
- new_height = (new_height // 8) * 8
 
136
 
137
  return new_width, new_height
138
 
@@ -189,12 +188,13 @@ def process_and_concat(images, prompt, seed, randomize_seed, guidance_scale, ste
189
  if randomize_seed:
190
  seed = random.randint(0, MAX_SEED)
191
 
192
- # --- NEW: Explicitly resize to avoid Diffusers' internal CenterCrop ---
 
 
193
  pre_resized_image = orig_image.resize((width, height), Image.Resampling.LANCZOS)
194
 
195
  # --- Step 2: Dispatch to GPU ---
196
  # GPU block is completely clean, measuring only pure generation time
197
- # Pass 'pre_resized_image' instead of 'orig_image'
198
  result_image = run_gpu_inference(
199
  pre_resized_image, prompt, seed, guidance_scale, steps, width, height
200
  )
@@ -242,7 +242,7 @@ css = """
242
 
243
  with gr.Blocks() as demo:
244
  with gr.Column(elem_id="col-container"):
245
- gr.Markdown("# **FireRed-Image-Edit-1.0-Fast**", elem_id="main-title")
246
  gr.Markdown("Perform image edits using [FireRed-Image-Edit-1.0](https://huggingface.co/FireRedTeam/FireRed-Image-Edit-1.0) with 4-step fast inference. Open on [GitHub](https://github.com/PRITHIVSAKTHIUR/FireRed-Image-Edit-1.0-Fast)")
247
 
248
  with gr.Row(equal_height=True):
@@ -255,20 +255,17 @@ with gr.Blocks() as demo:
255
  height=300,
256
  allow_preview=True
257
  )
258
-
259
  prompt = gr.Text(
260
  label="Edit Prompt",
261
  show_label=True,
262
  max_lines=2,
263
  placeholder="e.g., transform into anime, upscale, change lighting...",
264
  )
265
-
266
  run_button = gr.Button("Edit Image", variant="primary")
267
 
268
  with gr.Column():
269
  output_image = gr.Image(interactive=False, format="png")
270
  output_file = gr.File(label="Download Lossless Merged Image")
271
-
272
  with gr.Accordion("Advanced Settings", open=False, visible=False):
273
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
274
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
 
80
  orange_red_theme = OrangeRedTheme()
81
 
82
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
83
  print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
84
  print("torch.__version__ =", torch.__version__)
85
  print("Using device:", device)
 
109
 
110
  # 不加 VAE 显存优化。因为 VAE Tiling/Slicing 会增加生成耗时,进而消耗你的 ZeroGPU Quota。
111
  # A100 处理 1024 分辨率足够,所以全速运行即可。
 
112
  MAX_SEED = np.iinfo(np.int32).max
113
 
114
+ def update_dimensions_on_upload(image, max_size=768, multiple=32):
115
+ """
116
+ 修改点:将原先的向下取整为 8 的倍数,改为四舍五入到 32 的倍数。
117
+ 这彻底阻止了 Diffusers 模型由于不匹配 DiT patch 结构而产生的暗中裁剪。
118
+ """
119
  if image is None:
120
  return max_size, max_size
 
121
  original_width, original_height = image.size
122
 
123
  # Optimization: Do not upscale small images unnecessarily
124
  if original_width <= max_size and original_height <= max_size:
125
  new_width, new_height = original_width, original_height
 
 
 
 
126
  else:
127
+ # Scale down proportionally based on the longest edge
128
+ scale = min(max_size / original_width, max_size / original_height)
129
+ new_width = int(original_width * scale)
130
+ new_height = int(original_height * scale)
131
 
132
+ # 强制将宽高锁定为 `multiple` ( 32) 的倍数
133
+ new_width = max(multiple, round(new_width / multiple) * multiple)
134
+ new_height = max(multiple, round(new_height / multiple) * multiple)
135
 
136
  return new_width, new_height
137
 
 
188
  if randomize_seed:
189
  seed = random.randint(0, MAX_SEED)
190
 
191
+ # --- 避免裁剪的关键 ---
192
+ # CPU 端直接把原图 resize 到严格兼容模型底层的尺寸 (32的倍数)。
193
+ # 不依赖 Diffusers 管道内部做任何尺寸处理。
194
  pre_resized_image = orig_image.resize((width, height), Image.Resampling.LANCZOS)
195
 
196
  # --- Step 2: Dispatch to GPU ---
197
  # GPU block is completely clean, measuring only pure generation time
 
198
  result_image = run_gpu_inference(
199
  pre_resized_image, prompt, seed, guidance_scale, steps, width, height
200
  )
 
242
 
243
  with gr.Blocks() as demo:
244
  with gr.Column(elem_id="col-container"):
245
+ gr.Markdown("# FireRed-Image-Edit-1.0-Fast", elem_id="main-title")
246
  gr.Markdown("Perform image edits using [FireRed-Image-Edit-1.0](https://huggingface.co/FireRedTeam/FireRed-Image-Edit-1.0) with 4-step fast inference. Open on [GitHub](https://github.com/PRITHIVSAKTHIUR/FireRed-Image-Edit-1.0-Fast)")
247
 
248
  with gr.Row(equal_height=True):
 
255
  height=300,
256
  allow_preview=True
257
  )
 
258
  prompt = gr.Text(
259
  label="Edit Prompt",
260
  show_label=True,
261
  max_lines=2,
262
  placeholder="e.g., transform into anime, upscale, change lighting...",
263
  )
 
264
  run_button = gr.Button("Edit Image", variant="primary")
265
 
266
  with gr.Column():
267
  output_image = gr.Image(interactive=False, format="png")
268
  output_file = gr.File(label="Download Lossless Merged Image")
 
269
  with gr.Accordion("Advanced Settings", open=False, visible=False):
270
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
271
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)