LPX55 commited on
Commit
5bab260
·
verified ·
1 Parent(s): 4d170a2

chore: add simple image resizing to prevent OOMs

Browse files
Files changed (1) hide show
  1. app_local.py +51 -16
app_local.py CHANGED
@@ -254,8 +254,59 @@ def infer(
254
  if device == "cuda":
255
  torch.cuda.empty_cache()
256
  gc.collect()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  original_prompt = prompt
258
  prompt_info = ""
 
259
  # Handle prompt rewriting
260
  if rewrite_prompt:
261
  try:
@@ -293,22 +344,6 @@ def infer(
293
  f"</div>"
294
  )
295
 
296
- # Add noise function
297
- def add_noise_to_image(pil_image, noise_level=0.02):
298
- """Add slight noise to image to create variation in outputs"""
299
- try:
300
- img_array = np.array(pil_image).astype(np.float32) / 255.0
301
- noise = np.random.normal(0, noise_level, img_array.shape)
302
- noisy_array = img_array + noise
303
- # Clip values to valid range
304
- noisy_array = np.clip(noisy_array, 0, 1)
305
- # Convert back to PIL
306
- noisy_array = (noisy_array * 255).astype(np.uint8)
307
- return Image.fromarray(noisy_array)
308
- except Exception as e:
309
- print(f"Warning: Could not add noise to image: {e}")
310
- return pil_image # Return original if noise addition fails
311
-
312
  # Set base seed for reproducibility
313
  base_seed = seed if not randomize_seed else random.randint(0, MAX_SEED)
314
 
 
254
  if device == "cuda":
255
  torch.cuda.empty_cache()
256
  gc.collect()
257
+
258
+ # Resize image to max 1024px on longest side
259
+ def resize_image(pil_image, max_size=1024):
260
+ """Resize image to maximum dimension of 1024px while maintaining aspect ratio"""
261
+ try:
262
+ if pil_image is None:
263
+ return pil_image
264
+
265
+ width, height = pil_image.size
266
+ max_dimension = max(width, height)
267
+
268
+ if max_dimension <= max_size:
269
+ return pil_image # No resize needed
270
+
271
+ # Calculate new dimensions maintaining aspect ratio
272
+ scale = max_size / max_dimension
273
+ new_width = int(width * scale)
274
+ new_height = int(height * scale)
275
+
276
+ # Resize image
277
+ resized_image = pil_image.resize((new_width, new_height), Image.LANCZOS)
278
+ print(f"📝 Image resized from {width}x{height} to {new_width}x{new_height}")
279
+ return resized_image
280
+
281
+ except Exception as e:
282
+ print(f"⚠️ Image resize failed: {e}")
283
+ return pil_image # Return original if resize fails
284
+
285
+ # Add noise function for batch variation
286
+ def add_noise_to_image(pil_image, noise_level=0.02):
287
+ """Add slight noise to image to create variation in outputs"""
288
+ try:
289
+ if pil_image is None:
290
+ return pil_image
291
+
292
+ img_array = np.array(pil_image).astype(np.float32) / 255.0
293
+ noise = np.random.normal(0, noise_level, img_array.shape)
294
+ noisy_array = img_array + noise
295
+ # Clip values to valid range
296
+ noisy_array = np.clip(noisy_array, 0, 1)
297
+ # Convert back to PIL
298
+ noisy_array = (noisy_array * 255).astype(np.uint8)
299
+ return Image.fromarray(noisy_array)
300
+ except Exception as e:
301
+ print(f"Warning: Could not add noise to image: {e}")
302
+ return pil_image # Return original if noise addition fails
303
+
304
+ # Resize input image first
305
+ image = resize_image(image, max_size=1024)
306
+
307
  original_prompt = prompt
308
  prompt_info = ""
309
+
310
  # Handle prompt rewriting
311
  if rewrite_prompt:
312
  try:
 
344
  f"</div>"
345
  )
346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  # Set base seed for reproducibility
348
  base_seed = seed if not randomize_seed else random.randint(0, MAX_SEED)
349