Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,13 +92,22 @@ gd_model = GroundingDinoForObjectDetection.from_pretrained(gd_model_path, torch_
|
|
| 92 |
gd_model = gd_model.to(device=device)
|
| 93 |
assert isinstance(gd_model, GroundingDinoForObjectDetection)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
pipe = FluxPipeline.from_pretrained(
|
| 97 |
"black-forest-labs/FLUX.1-dev",
|
| 98 |
torch_dtype=torch.float16,
|
| 99 |
use_auth_token=HF_TOKEN
|
| 100 |
)
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# LoRA ๊ฐ์ค์น ๋ก๋
|
| 104 |
pipe.load_lora_weights(
|
|
@@ -173,18 +182,22 @@ def apply_mask(img: Image.Image, mask_img: Image.Image, defringe: bool = True) -
|
|
| 173 |
|
| 174 |
def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int, int]:
|
| 175 |
"""์ ํ๋ ๋น์จ์ ๋ฐ๋ผ ์ด๋ฏธ์ง ํฌ๊ธฐ ๊ณ์ฐ"""
|
| 176 |
-
#
|
| 177 |
if aspect_ratio == "1:1":
|
| 178 |
width = height = 512
|
| 179 |
elif aspect_ratio == "16:9":
|
| 180 |
-
width, height =
|
| 181 |
elif aspect_ratio == "9:16":
|
| 182 |
-
width, height =
|
| 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:
|
|
@@ -196,40 +209,31 @@ def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
|
|
| 196 |
|
| 197 |
with timer("Background generation"):
|
| 198 |
try:
|
|
|
|
| 199 |
with torch.inference_mode():
|
| 200 |
image = pipe(
|
| 201 |
prompt=prompt,
|
| 202 |
-
width=
|
| 203 |
-
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 |
-
|
| 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ํฝ์
๋ณด์ฅ
|
|
|
|
| 92 |
gd_model = gd_model.to(device=device)
|
| 93 |
assert isinstance(gd_model, GroundingDinoForObjectDetection)
|
| 94 |
|
| 95 |
+
# ํ์ดํ๋ผ์ธ ์ด๊ธฐํ ๋ถ๋ถ๋ ์์
|
| 96 |
pipe = FluxPipeline.from_pretrained(
|
| 97 |
"black-forest-labs/FLUX.1-dev",
|
| 98 |
torch_dtype=torch.float16,
|
| 99 |
use_auth_token=HF_TOKEN
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
# ๋ฉ๋ชจ๋ฆฌ ๋ฐ ์ฑ๋ฅ ์ต์ ํ ์ค์
|
| 103 |
+
pipe.enable_attention_slicing()
|
| 104 |
+
pipe.enable_vae_slicing()
|
| 105 |
+
pipe.enable_xformers_memory_efficient_attention() # xformers๊ฐ ์ค์น๋์ด ์๋ค๋ฉด
|
| 106 |
+
|
| 107 |
+
if torch.cuda.is_available():
|
| 108 |
+
pipe = pipe.to("cuda")
|
| 109 |
+
pipe.enable_model_cpu_offload() # ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ํ CPU ์คํ๋ก๋
|
| 110 |
+
|
| 111 |
|
| 112 |
# LoRA ๊ฐ์ค์น ๋ก๋
|
| 113 |
pipe.load_lora_weights(
|
|
|
|
| 182 |
|
| 183 |
def calculate_dimensions(aspect_ratio: str, base_size: int = 512) -> tuple[int, int]:
|
| 184 |
"""์ ํ๋ ๋น์จ์ ๋ฐ๋ผ ์ด๋ฏธ์ง ํฌ๊ธฐ ๊ณ์ฐ"""
|
| 185 |
+
# FLUX ํ์ดํ๋ผ์ธ์ด ์ง์ํ๋ ์์ ํ ํฌ๊ธฐ ์ฌ์ฉ
|
| 186 |
if aspect_ratio == "1:1":
|
| 187 |
width = height = 512
|
| 188 |
elif aspect_ratio == "16:9":
|
| 189 |
+
width, height = 576, 320 # 16:9์ ๊ฐ๊น์ด ์์ ํ ํฌ๊ธฐ
|
| 190 |
elif aspect_ratio == "9:16":
|
| 191 |
+
width, height = 320, 576 # 9:16์ ๊ฐ๊น์ด ์์ ํ ํฌ๊ธฐ
|
| 192 |
elif aspect_ratio == "4:3":
|
| 193 |
+
width, height = 512, 384 # 4:3์ ๊ฐ๊น์ด ์์ ํ ํฌ๊ธฐ
|
| 194 |
else:
|
| 195 |
width = height = 512
|
| 196 |
|
| 197 |
+
# 8์ ๋ฐฐ์๋ก ์กฐ์
|
| 198 |
+
width = (width // 8) * 8
|
| 199 |
+
height = (height // 8) * 8
|
| 200 |
+
|
| 201 |
return width, height
|
| 202 |
|
| 203 |
def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
|
|
|
|
| 209 |
|
| 210 |
with timer("Background generation"):
|
| 211 |
try:
|
| 212 |
+
# ๋จผ์ 512x512๋ก ์์ฑ
|
| 213 |
with torch.inference_mode():
|
| 214 |
image = pipe(
|
| 215 |
prompt=prompt,
|
| 216 |
+
width=512,
|
| 217 |
+
height=512,
|
| 218 |
num_inference_steps=8,
|
| 219 |
guidance_scale=4.0,
|
| 220 |
).images[0]
|
| 221 |
+
|
| 222 |
+
# ์ํ๋ ํฌ๊ธฐ๋ก ๋ฆฌ์ฌ์ด์ฆ
|
| 223 |
+
if width != 512 or height != 512:
|
| 224 |
+
image = image.resize((width, height), Image.LANCZOS)
|
| 225 |
return image
|
| 226 |
+
|
| 227 |
except Exception as e:
|
| 228 |
print(f"Pipeline error: {str(e)}")
|
| 229 |
+
# ์๋ฌ ๋ฐ์ ์ ํฐ์ ๋ฐฐ๊ฒฝ ๋ฐํ
|
| 230 |
+
return Image.new('RGB', (width, height), 'white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
except Exception as e:
|
| 233 |
print(f"Background generation error: {str(e)}")
|
| 234 |
return Image.new('RGB', (512, 512), 'white')
|
| 235 |
|
| 236 |
+
|
| 237 |
def adjust_size_to_multiple_of_8(width: int, height: int) -> tuple[int, int]:
|
| 238 |
"""์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ 8์ ๋ฐฐ์๋ก ์กฐ์ """
|
| 239 |
new_width = max(8, ((width + 7) // 8) * 8) # ์ต์ 8ํฝ์
๋ณด์ฅ
|