ginipick commited on
Commit
9d4caea
ยท
verified ยท
1 Parent(s): d922f5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -171,42 +171,27 @@ def apply_mask(img: Image.Image, mask_img: Image.Image, defringe: bool = True) -
171
  return result
172
 
173
 
174
- def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
175
- """์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ 8์˜ ๋ฐฐ์ˆ˜๋กœ ์กฐ์ •ํ•˜๋Š” ํ•จ์ˆ˜"""
176
- new_width = ((width + 7) // 8) * 8
177
- new_height = ((height + 7) // 8) * 8
178
- return new_width, new_height
179
-
180
  def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int, int]:
181
  """์„ ํƒ๋œ ๋น„์œจ์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๊ณ„์‚ฐ"""
 
182
  if aspect_ratio == "1:1":
183
- width, height = base_size, base_size
184
  elif aspect_ratio == "16:9":
185
- width, height = base_size * 16 // 9, base_size
186
  elif aspect_ratio == "9:16":
187
- width, height = base_size, base_size * 16 // 9
188
  elif aspect_ratio == "4:3":
189
- width, height = base_size * 4 // 3, base_size
190
  else:
191
- width, height = base_size, base_size
192
-
193
- # 8์˜ ๋ฐฐ์ˆ˜๋กœ ์กฐ์ •
194
- return adjust_size_to_multiple_of_8(width, height)
195
 
196
  def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
197
  try:
198
- # ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๊ณ„์‚ฐ ๋ฐ 8์˜ ๋ฐฐ์ˆ˜๋กœ ์กฐ์ •
199
  width, height = calculate_dimensions(aspect_ratio)
200
 
201
- # ์ตœ๋Œ€ ํฌ๊ธฐ ์ œํ•œ ์ ์šฉ
202
- max_size = 768
203
- if width > max_size or height > max_size:
204
- ratio = max_size / max(width, height)
205
- width = int(width * ratio)
206
- height = int(height * ratio)
207
- # ๋‹ค์‹œ 8์˜ ๋ฐฐ์ˆ˜๋กœ ์กฐ์ •
208
- width, height = adjust_size_to_multiple_of_8(width, height)
209
-
210
  print(f"Generating background with size: {width}x{height}")
211
 
212
  with timer("Background generation"):
@@ -217,17 +202,40 @@ def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
217
  width=width,
218
  height=height,
219
  num_inference_steps=8,
220
- guidance_scale=4.0
221
  ).images[0]
 
222
  except Exception as e:
223
  print(f"Pipeline error: {str(e)}")
224
- return Image.new('RGB', (width, height), 'white')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
- return image
227
  except Exception as e:
228
  print(f"Background generation error: {str(e)}")
229
  return Image.new('RGB', (512, 512), 'white')
230
 
 
 
 
 
 
 
231
  def create_position_grid():
232
  return """
233
  <div class="position-grid" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 150px; margin: auto;">
 
171
  return result
172
 
173
 
 
 
 
 
 
 
174
  def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int, int]:
175
  """์„ ํƒ๋œ ๋น„์œจ์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๊ณ„์‚ฐ"""
176
+ # ๊ธฐ๋ณธ ํฌ๊ธฐ๋ฅผ 512๋กœ ๊ณ ์ •ํ•˜๊ณ , ๋น„์œจ์— ๋”ฐ๋ผ ์กฐ์ •
177
  if aspect_ratio == "1:1":
178
+ width = height = 512
179
  elif aspect_ratio == "16:9":
180
+ width, height = 512, 288 # 16:9 ๋น„์œจ ์œ ์ง€ํ•˜๋ฉด์„œ ๋†’์ด ์กฐ์ •
181
  elif aspect_ratio == "9:16":
182
+ width, height = 288, 512 # 9:16 ๋น„์œจ ์œ ์ง€ํ•˜๋ฉด์„œ ๋„ˆ๋น„ ์กฐ์ •
183
  elif aspect_ratio == "4:3":
184
+ width, height = 512, 384 # 4:3 ๋น„์œจ ์œ ์ง€
185
  else:
186
+ width = height = 512
187
+
188
+ return width, height
 
189
 
190
  def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
191
  try:
192
+ # ์•ˆ์ „ํ•œ ํฌ๊ธฐ ๊ณ„์‚ฐ
193
  width, height = calculate_dimensions(aspect_ratio)
194
 
 
 
 
 
 
 
 
 
 
195
  print(f"Generating background with size: {width}x{height}")
196
 
197
  with timer("Background generation"):
 
202
  width=width,
203
  height=height,
204
  num_inference_steps=8,
205
+ guidance_scale=4.0,
206
  ).images[0]
207
+ return image
208
  except Exception as e:
209
  print(f"Pipeline error: {str(e)}")
210
+ # ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ๊ธฐ๋ณธ ํฌ๊ธฐ๋กœ ์žฌ์‹œ๋„
211
+ try:
212
+ with torch.inference_mode():
213
+ image = pipe(
214
+ prompt=prompt,
215
+ width=512,
216
+ height=512,
217
+ num_inference_steps=8,
218
+ guidance_scale=4.0,
219
+ ).images[0]
220
+
221
+ # ์›ํ•˜๋Š” ํฌ๊ธฐ๋กœ ๋ฆฌ์‚ฌ์ด์ฆˆ
222
+ if width != 512 or height != 512:
223
+ image = image.resize((width, height), Image.LANCZOS)
224
+ return image
225
+ except Exception as e2:
226
+ print(f"Fallback generation error: {str(e2)}")
227
+ return Image.new('RGB', (width, height), 'white')
228
 
 
229
  except Exception as e:
230
  print(f"Background generation error: {str(e)}")
231
  return Image.new('RGB', (512, 512), 'white')
232
 
233
+ def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
234
+ """์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ 8์˜ ๋ฐฐ์ˆ˜๋กœ ์กฐ์ •"""
235
+ new_width = max(8, ((width + 7) // 8) * 8) # ์ตœ์†Œ 8ํ”ฝ์…€ ๋ณด์žฅ
236
+ new_height = max(8, ((height + 7) // 8) * 8) # ์ตœ์†Œ 8ํ”ฝ์…€ ๋ณด์žฅ
237
+ return new_width, new_height
238
+
239
  def create_position_grid():
240
  return """
241
  <div class="position-grid" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 150px; margin: auto;">