Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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
|
184 |
elif aspect_ratio == "16:9":
|
185 |
-
width, height =
|
186 |
elif aspect_ratio == "9:16":
|
187 |
-
width, height =
|
188 |
elif aspect_ratio == "4:3":
|
189 |
-
width, height =
|
190 |
else:
|
191 |
-
width
|
192 |
-
|
193 |
-
|
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 |
-
#
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;">
|