Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,16 +22,66 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
| 22 |
import gc
|
| 23 |
from PIL import Image, ImageDraw, ImageFont
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if torch.cuda.is_available():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# GPU ์ค์
|
| 36 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
| 37 |
|
|
@@ -340,51 +390,40 @@ def on_change_bbox(prompts: dict[str, Any] | None):
|
|
| 340 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
| 341 |
return gr.update(interactive=bool(img and prompt))
|
| 342 |
|
|
|
|
|
|
|
| 343 |
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
| 344 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 345 |
scale_percent: float = 100, text_params: dict | None = None) -> tuple[Image.Image, Image.Image]:
|
| 346 |
try:
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
print(f"Processing with position: {position}, scale: {scale_percent}")
|
| 352 |
|
| 353 |
-
|
| 354 |
-
|
|
|
|
| 355 |
if bg_prompt:
|
| 356 |
-
bg_prompt = translate_to_english(bg_prompt)
|
| 357 |
-
except Exception as e:
|
| 358 |
-
print(f"Translation error (continuing with original text): {str(e)}")
|
| 359 |
-
|
| 360 |
-
# ๊ธฐ์กด ์ฒ๋ฆฌ ๋ก์ง...
|
| 361 |
-
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
| 362 |
-
|
| 363 |
-
if bg_prompt:
|
| 364 |
-
try:
|
| 365 |
combined = combine_with_background(
|
| 366 |
foreground=results[2],
|
| 367 |
background=results[1],
|
| 368 |
position=position,
|
| 369 |
scale_percent=scale_percent
|
| 370 |
)
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
combined = add_text_to_image(combined, text_params)
|
| 380 |
-
|
| 381 |
-
return combined, results[2]
|
| 382 |
except Exception as e:
|
| 383 |
print(f"Error in process_prompt: {str(e)}")
|
| 384 |
raise gr.Error(str(e))
|
| 385 |
finally:
|
| 386 |
clear_memory()
|
| 387 |
|
|
|
|
| 388 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
| 389 |
try:
|
| 390 |
if img is None or box_input.strip() == "":
|
|
@@ -423,7 +462,14 @@ def update_box_button(img, box_input):
|
|
| 423 |
except:
|
| 424 |
return gr.update(interactive=False, variant="secondary")
|
| 425 |
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
# CSS ์ ์
|
| 428 |
css = """
|
| 429 |
footer {display: none}
|
|
@@ -559,9 +605,15 @@ def update_process_button(img, prompt):
|
|
| 559 |
interactive=bool(img and prompt),
|
| 560 |
variant="primary" if bool(img and prompt) else "secondary"
|
| 561 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
|
| 563 |
-
|
| 564 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 565 |
gr.HTML("""
|
| 566 |
<div class="main-title">
|
| 567 |
<h1>๐จGiniGen Canvas</h1>
|
|
@@ -769,10 +821,14 @@ pipe = FluxPipeline.from_pretrained(
|
|
| 769 |
token=HF_TOKEN
|
| 770 |
)
|
| 771 |
|
| 772 |
-
demo.queue(max_size=
|
| 773 |
demo.launch(
|
| 774 |
server_name="0.0.0.0",
|
| 775 |
server_port=7860,
|
| 776 |
share=False,
|
| 777 |
-
max_threads=2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 778 |
)
|
|
|
|
| 22 |
import gc
|
| 23 |
from PIL import Image, ImageDraw, ImageFont
|
| 24 |
|
| 25 |
+
# ๋ชจ๋ธ ์ด๊ธฐํ ๋ถ๋ถ ์์
|
| 26 |
+
def initialize_models():
|
| 27 |
+
global segmenter, gd_model, gd_processor, pipe
|
| 28 |
+
|
| 29 |
try:
|
| 30 |
+
# GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
| 31 |
+
clear_memory()
|
| 32 |
+
|
| 33 |
+
# ๋ฒ์ญ ๋ชจ๋ธ์ CPU์์๋ง ์คํ
|
| 34 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to('cpu')
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 36 |
+
translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1)
|
| 37 |
+
|
| 38 |
+
# GroundingDINO ๋ชจ๋ธ ์ด๊ธฐํ
|
| 39 |
+
gd_processor = GroundingDinoProcessor.from_pretrained(gd_model_path)
|
| 40 |
+
gd_model = GroundingDinoForObjectDetection.from_pretrained(
|
| 41 |
+
gd_model_path,
|
| 42 |
+
torch_dtype=torch.float16, # float32 ๋์ float16 ์ฌ์ฉ
|
| 43 |
+
device_map="auto" # ์๋ ๋๋ฐ์ด์ค ๋งคํ
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Segmenter ์ด๊ธฐํ
|
| 47 |
+
segmenter = BoxSegmenter(device="cpu")
|
| 48 |
if torch.cuda.is_available():
|
| 49 |
+
segmenter.to(device)
|
| 50 |
+
|
| 51 |
+
# FLUX ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
| 52 |
+
pipe = FluxPipeline.from_pretrained(
|
| 53 |
+
"black-forest-labs/FLUX.1-dev",
|
| 54 |
+
torch_dtype=torch.float16,
|
| 55 |
+
token=HF_TOKEN
|
| 56 |
+
)
|
| 57 |
+
pipe.enable_attention_slicing(slice_size="auto")
|
| 58 |
+
|
| 59 |
+
# LoRA ๊ฐ์ค์น ๋ก๋
|
| 60 |
+
pipe.load_lora_weights(
|
| 61 |
+
hf_hub_download(
|
| 62 |
+
"ByteDance/Hyper-SD",
|
| 63 |
+
"Hyper-FLUX.1-dev-8steps-lora.safetensors",
|
| 64 |
+
use_auth_token=HF_TOKEN
|
| 65 |
+
)
|
| 66 |
+
)
|
| 67 |
+
pipe.fuse_lora(lora_scale=0.125)
|
| 68 |
+
|
| 69 |
+
if torch.cuda.is_available():
|
| 70 |
+
pipe = pipe.to("cuda:0")
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Model initialization error: {str(e)}")
|
| 74 |
+
raise
|
| 75 |
+
|
| 76 |
+
def clear_memory():
|
| 77 |
+
"""๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ๊ฐํ ํจ์"""
|
| 78 |
+
gc.collect()
|
| 79 |
+
torch.cuda.empty_cache()
|
| 80 |
+
|
| 81 |
+
if torch.cuda.is_available():
|
| 82 |
+
with torch.cuda.device(0):
|
| 83 |
+
torch.cuda.reset_peak_memory_stats()
|
| 84 |
+
torch.cuda.empty_cache()
|
| 85 |
# GPU ์ค์
|
| 86 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
| 87 |
|
|
|
|
| 390 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
| 391 |
return gr.update(interactive=bool(img and prompt))
|
| 392 |
|
| 393 |
+
|
| 394 |
+
@spaces.GPU(duration=20)
|
| 395 |
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
| 396 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
| 397 |
scale_percent: float = 100, text_params: dict | None = None) -> tuple[Image.Image, Image.Image]:
|
| 398 |
try:
|
| 399 |
+
# ์
๋ ฅ ์ด๋ฏธ์ง ์ต์ ํ
|
| 400 |
+
img = process_image(img)
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
with torch.cuda.amp.autocast(): # ์๋ ํผํฉ ์ ๋ฐ๋ ์ฌ์ฉ
|
| 403 |
+
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
| 404 |
+
|
| 405 |
if bg_prompt:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
combined = combine_with_background(
|
| 407 |
foreground=results[2],
|
| 408 |
background=results[1],
|
| 409 |
position=position,
|
| 410 |
scale_percent=scale_percent
|
| 411 |
)
|
| 412 |
+
|
| 413 |
+
if text_params and text_params.get('text'):
|
| 414 |
+
combined = add_text_to_image(combined, text_params)
|
| 415 |
+
|
| 416 |
+
return combined, results[2]
|
| 417 |
+
|
| 418 |
+
return results[1], results[2]
|
| 419 |
+
|
|
|
|
|
|
|
|
|
|
| 420 |
except Exception as e:
|
| 421 |
print(f"Error in process_prompt: {str(e)}")
|
| 422 |
raise gr.Error(str(e))
|
| 423 |
finally:
|
| 424 |
clear_memory()
|
| 425 |
|
| 426 |
+
|
| 427 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
| 428 |
try:
|
| 429 |
if img is None or box_input.strip() == "":
|
|
|
|
| 462 |
except:
|
| 463 |
return gr.update(interactive=False, variant="secondary")
|
| 464 |
|
| 465 |
+
def process_image(img: Image.Image, max_size: int = 1024) -> Image.Image:
|
| 466 |
+
"""์ด๋ฏธ์ง ํฌ๊ธฐ ์ต์ ํ"""
|
| 467 |
+
if img.width > max_size or img.height > max_size:
|
| 468 |
+
ratio = max_size / max(img.width, img.height)
|
| 469 |
+
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 470 |
+
return img.resize(new_size, Image.LANCZOS)
|
| 471 |
+
return img
|
| 472 |
+
|
| 473 |
# CSS ์ ์
|
| 474 |
css = """
|
| 475 |
footer {display: none}
|
|
|
|
| 605 |
interactive=bool(img and prompt),
|
| 606 |
variant="primary" if bool(img and prompt) else "secondary"
|
| 607 |
)
|
| 608 |
+
|
| 609 |
+
demo = gr.Blocks(
|
| 610 |
+
theme=gr.themes.Soft(),
|
| 611 |
+
css=css,
|
| 612 |
+
analytics_enabled=False, # ๋ถ์ ๋นํ์ฑํ
|
| 613 |
+
cache_examples=False # ์์ ์บ์ฑ ๋นํ์ฑํ
|
| 614 |
+
)
|
| 615 |
|
| 616 |
+
|
|
|
|
| 617 |
gr.HTML("""
|
| 618 |
<div class="main-title">
|
| 619 |
<h1>๐จGiniGen Canvas</h1>
|
|
|
|
| 821 |
token=HF_TOKEN
|
| 822 |
)
|
| 823 |
|
| 824 |
+
demo.queue(max_size=3) # ํ ํฌ๊ธฐ ๊ฐ์
|
| 825 |
demo.launch(
|
| 826 |
server_name="0.0.0.0",
|
| 827 |
server_port=7860,
|
| 828 |
share=False,
|
| 829 |
+
max_threads=2,
|
| 830 |
+
enable_queue=True,
|
| 831 |
+
cache_examples=False,
|
| 832 |
+
show_error=True,
|
| 833 |
+
show_tips=False
|
| 834 |
)
|