Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,37 +37,52 @@ def initialize_models():
|
|
| 37 |
global segmenter, gd_model, gd_processor, pipe, translator
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
#
|
| 41 |
-
model =
|
| 42 |
-
model_name,
|
| 43 |
-
low_cpu_mem_usage=True
|
|
|
|
| 44 |
).to('cpu')
|
| 45 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 46 |
translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1)
|
|
|
|
| 47 |
|
| 48 |
-
# GroundingDINO ๋ชจ๋ธ
|
| 49 |
-
gd_processor = GroundingDinoProcessor.from_pretrained(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
torch_dtype=torch.float16,
|
| 53 |
-
device_map=None # device_map์ None์ผ๋ก ์ค์
|
| 54 |
)
|
|
|
|
| 55 |
|
| 56 |
-
# Segmenter
|
| 57 |
-
segmenter =
|
| 58 |
|
| 59 |
-
# FLUX ํ์ดํ๋ผ์ธ
|
| 60 |
pipe = FluxPipeline.from_pretrained(
|
| 61 |
"black-forest-labs/FLUX.1-dev",
|
| 62 |
torch_dtype=torch.float16,
|
| 63 |
-
|
| 64 |
-
|
| 65 |
)
|
| 66 |
-
pipe.enable_attention_slicing()
|
|
|
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Model initialization error: {str(e)}")
|
| 70 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# GPU ์ค์
|
| 73 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
|
@@ -374,26 +389,42 @@ def on_change_bbox(prompts: dict[str, Any] | None):
|
|
| 374 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
| 375 |
return gr.update(interactive=bool(img and prompt))
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
| 380 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 381 |
scale_percent: float = 100, text_params: dict | None = None):
|
| 382 |
try:
|
| 383 |
-
#
|
| 384 |
-
|
| 385 |
-
device = torch.device('cuda')
|
| 386 |
-
# ๋ชจ๋ธ๋ค์ GPU๋ก ์ด๋
|
| 387 |
-
gd_model.to(device)
|
| 388 |
-
segmenter.to(device)
|
| 389 |
-
pipe.to(device)
|
| 390 |
-
else:
|
| 391 |
-
device = torch.device('cpu')
|
| 392 |
|
| 393 |
-
#
|
|
|
|
|
|
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
finally:
|
| 396 |
-
#
|
|
|
|
| 397 |
if torch.cuda.is_available():
|
| 398 |
try:
|
| 399 |
with torch.cuda.device('cuda'):
|
|
@@ -440,13 +471,7 @@ def update_box_button(img, box_input):
|
|
| 440 |
except:
|
| 441 |
return gr.update(interactive=False, variant="secondary")
|
| 442 |
|
| 443 |
-
|
| 444 |
-
"""์ด๋ฏธ์ง ํฌ๊ธฐ ์ต์ ํ"""
|
| 445 |
-
if img.width > max_size or img.height > max_size:
|
| 446 |
-
ratio = max_size / max(img.width, img.height)
|
| 447 |
-
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 448 |
-
return img.resize(new_size, Image.LANCZOS)
|
| 449 |
-
return img
|
| 450 |
|
| 451 |
# CSS ์ ์
|
| 452 |
css = """
|
|
@@ -804,14 +829,15 @@ if __name__ == "__main__":
|
|
| 804 |
queue=True
|
| 805 |
)
|
| 806 |
|
| 807 |
-
#
|
| 808 |
demo.launch(
|
| 809 |
server_name="0.0.0.0",
|
| 810 |
server_port=7860,
|
| 811 |
share=False,
|
| 812 |
-
max_threads=
|
| 813 |
enable_queue=True,
|
| 814 |
cache_examples=False,
|
| 815 |
show_error=True,
|
| 816 |
-
show_tips=False
|
|
|
|
| 817 |
)
|
|
|
|
| 37 |
global segmenter, gd_model, gd_processor, pipe, translator
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# ๋ฒ์ญ ๋ชจ๋ธ - ๊ฐ๋ฒผ์ด ๋ฒ์ ์ฌ์ฉ
|
| 41 |
+
model = AutoModelForSeq2SeqLength.from_pretrained(
|
| 42 |
+
model_name,
|
| 43 |
+
low_cpu_mem_usage=True,
|
| 44 |
+
torch_dtype=torch.float16
|
| 45 |
).to('cpu')
|
| 46 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 47 |
translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1)
|
| 48 |
+
del model # ๋ช
์์ ๋ฉ๋ชจ๋ฆฌ ํด์
|
| 49 |
|
| 50 |
+
# GroundingDINO - ๋ ์์ ๋ชจ๋ธ ์ฌ์ฉ
|
| 51 |
+
gd_processor = GroundingDinoProcessor.from_pretrained(
|
| 52 |
+
"IDEA-Research/grounding-dino-base", # ๋ ์์ base ๋ชจ๋ธ
|
| 53 |
+
torch_dtype=torch.float16
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
+
gd_model = None # ํ์ํ ๋ ๋ก๋
|
| 56 |
|
| 57 |
+
# Segmenter - ๊ธฐ๋ณธ ์ค์
|
| 58 |
+
segmenter = None # ํ์ํ ๋ ๋ก๋
|
| 59 |
|
| 60 |
+
# FLUX ํ์ดํ๋ผ์ธ - ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ค์
|
| 61 |
pipe = FluxPipeline.from_pretrained(
|
| 62 |
"black-forest-labs/FLUX.1-dev",
|
| 63 |
torch_dtype=torch.float16,
|
| 64 |
+
low_cpu_mem_usage=True,
|
| 65 |
+
use_safetensors=True
|
| 66 |
)
|
| 67 |
+
pipe.enable_attention_slicing(slice_size=1)
|
| 68 |
+
pipe.enable_sequential_cpu_offload()
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
print(f"Model initialization error: {str(e)}")
|
| 72 |
raise
|
| 73 |
+
|
| 74 |
+
def load_model_on_demand(model_type: str):
|
| 75 |
+
"""ํ์ํ ๋๋ง ๋ชจ๋ธ์ ๋ก๋ํ๋ ํจ์"""
|
| 76 |
+
global gd_model, segmenter
|
| 77 |
+
|
| 78 |
+
if model_type == "gd" and gd_model is None:
|
| 79 |
+
gd_model = GroundingDinoForObjectDetection.from_pretrained(
|
| 80 |
+
"IDEA-Research/grounding-dino-base",
|
| 81 |
+
torch_dtype=torch.float16,
|
| 82 |
+
low_cpu_mem_usage=True
|
| 83 |
+
)
|
| 84 |
+
elif model_type == "segmenter" and segmenter is None:
|
| 85 |
+
segmenter = BoxSegmenter(device='cpu')
|
| 86 |
|
| 87 |
# GPU ์ค์
|
| 88 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
|
|
|
| 389 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
| 390 |
return gr.update(interactive=bool(img and prompt))
|
| 391 |
|
| 392 |
+
def process_image(img: Image.Image) -> Image.Image:
|
| 393 |
+
"""์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ์ต์ ํ"""
|
| 394 |
+
# ์ต๋ ํฌ๊ธฐ ์ ํ
|
| 395 |
+
max_size = 512 # ๋ ์์ ํฌ๊ธฐ๋ก ์ ํ
|
| 396 |
+
if img.width > max_size or img.height > max_size:
|
| 397 |
+
ratio = max_size / max(img.width, img.height)
|
| 398 |
+
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 399 |
+
img = img.resize(new_size, Image.LANCZOS)
|
| 400 |
+
|
| 401 |
+
# ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ํ ์ด๋ฏธ์ง ๋ชจ๋ ๋ณํ
|
| 402 |
+
if img.mode in ['RGBA', 'LA']:
|
| 403 |
+
background = Image.new('RGB', img.size, (255, 255, 255))
|
| 404 |
+
background.paste(img, mask=img.split()[-1])
|
| 405 |
+
img = background
|
| 406 |
+
|
| 407 |
+
return img
|
| 408 |
+
|
| 409 |
+
@spaces.GPU(duration=15) # ๋ ์งง์ ์๊ฐ ์ ํ
|
| 410 |
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
| 411 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 412 |
scale_percent: float = 100, text_params: dict | None = None):
|
| 413 |
try:
|
| 414 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 415 |
+
img = process_image(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
+
# ํ์ํ ๋ชจ๋ธ๋ง ๋ก๋
|
| 418 |
+
load_model_on_demand("gd")
|
| 419 |
+
load_model_on_demand("segmenter")
|
| 420 |
|
| 421 |
+
with torch.cuda.amp.autocast(): # ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ํ mixed precision
|
| 422 |
+
# ์ฒ๋ฆฌ ๋ก์ง...
|
| 423 |
+
pass
|
| 424 |
+
|
| 425 |
finally:
|
| 426 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
| 427 |
+
clear_memory()
|
| 428 |
if torch.cuda.is_available():
|
| 429 |
try:
|
| 430 |
with torch.cuda.device('cuda'):
|
|
|
|
| 471 |
except:
|
| 472 |
return gr.update(interactive=False, variant="secondary")
|
| 473 |
|
| 474 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
# CSS ์ ์
|
| 477 |
css = """
|
|
|
|
| 829 |
queue=True
|
| 830 |
)
|
| 831 |
|
| 832 |
+
demo.queue(max_size=1) # ํ ํฌ๊ธฐ ์ ํ
|
| 833 |
demo.launch(
|
| 834 |
server_name="0.0.0.0",
|
| 835 |
server_port=7860,
|
| 836 |
share=False,
|
| 837 |
+
max_threads=1, # ์ค๋ ๋ ์ ์ ํ
|
| 838 |
enable_queue=True,
|
| 839 |
cache_examples=False,
|
| 840 |
show_error=True,
|
| 841 |
+
show_tips=False,
|
| 842 |
+
quiet=True
|
| 843 |
)
|