Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -180,43 +180,53 @@ def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
|
|
| 180 |
def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int, int]:
|
| 181 |
"""์ ํ๋ ๋น์จ์ ๋ฐ๋ผ ์ด๋ฏธ์ง ํฌ๊ธฐ ๊ณ์ฐ"""
|
| 182 |
if aspect_ratio == "1:1":
|
| 183 |
-
|
| 184 |
elif aspect_ratio == "16:9":
|
| 185 |
-
|
| 186 |
elif aspect_ratio == "9:16":
|
| 187 |
-
|
| 188 |
elif aspect_ratio == "4:3":
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
@spaces.GPU(duration=20) # 40์ด์์ 20์ด๋ก ๊ฐ์
|
| 193 |
def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
|
| 194 |
try:
|
|
|
|
| 195 |
width, height = calculate_dimensions(aspect_ratio)
|
| 196 |
-
width, height = adjust_size_to_multiple_of_8(width, height)
|
| 197 |
|
|
|
|
| 198 |
max_size = 768
|
| 199 |
if width > max_size or height > max_size:
|
| 200 |
ratio = max_size / max(width, height)
|
| 201 |
width = int(width * ratio)
|
| 202 |
height = int(height * ratio)
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
image = pipe(
|
| 206 |
-
prompt=prompt,
|
| 207 |
-
width=width,
|
| 208 |
-
height=height,
|
| 209 |
-
num_inference_steps=8,
|
| 210 |
-
guidance_scale=4.0
|
| 211 |
-
).images[0]
|
| 212 |
-
|
| 213 |
-
return image
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
except Exception as e:
|
| 216 |
print(f"Background generation error: {str(e)}")
|
| 217 |
-
return Image.new('RGB', (
|
| 218 |
-
finally:
|
| 219 |
-
clear_memory()
|
| 220 |
|
| 221 |
def create_position_grid():
|
| 222 |
return """
|
|
|
|
| 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"):
|
| 213 |
+
try:
|
| 214 |
+
with torch.inference_mode():
|
| 215 |
+
image = pipe(
|
| 216 |
+
prompt=prompt,
|
| 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 """
|