Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -22,9 +22,19 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
22 |
import gc
|
23 |
from PIL import Image, ImageDraw, ImageFont
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def initialize_models():
|
27 |
-
|
|
|
28 |
|
29 |
try:
|
30 |
# GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
@@ -39,14 +49,12 @@ def initialize_models():
|
|
39 |
gd_processor = GroundingDinoProcessor.from_pretrained(gd_model_path)
|
40 |
gd_model = GroundingDinoForObjectDetection.from_pretrained(
|
41 |
gd_model_path,
|
42 |
-
torch_dtype=torch.float16,
|
43 |
-
device_map=
|
44 |
)
|
45 |
|
46 |
# Segmenter ์ด๊ธฐํ
|
47 |
-
segmenter = BoxSegmenter(device=
|
48 |
-
if torch.cuda.is_available():
|
49 |
-
segmenter.to(device)
|
50 |
|
51 |
# FLUX ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
52 |
pipe = FluxPipeline.from_pretrained(
|
@@ -61,27 +69,17 @@ def initialize_models():
|
|
61 |
hf_hub_download(
|
62 |
"ByteDance/Hyper-SD",
|
63 |
"Hyper-FLUX.1-dev-8steps-lora.safetensors",
|
64 |
-
|
65 |
)
|
66 |
)
|
67 |
pipe.fuse_lora(lora_scale=0.125)
|
68 |
|
69 |
if torch.cuda.is_available():
|
70 |
-
pipe = pipe.to(
|
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 |
|
@@ -605,8 +603,20 @@ def update_process_button(img, prompt):
|
|
605 |
interactive=bool(img and prompt),
|
606 |
variant="primary" if bool(img and prompt) else "secondary"
|
607 |
)
|
608 |
-
|
609 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
610 |
# ๋ชจ๋ธ ์ด๊ธฐํ
|
611 |
initialize_models()
|
612 |
|
@@ -814,7 +824,6 @@ if __name__ == "__main__":
|
|
814 |
queue=True
|
815 |
)
|
816 |
|
817 |
-
# Gradio ์ฑ ์คํ
|
818 |
demo.queue(max_size=3)
|
819 |
demo.launch(
|
820 |
server_name="0.0.0.0",
|
|
|
22 |
import gc
|
23 |
from PIL import Image, ImageDraw, ImageFont
|
24 |
|
25 |
+
def clear_memory():
|
26 |
+
"""๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ํจ์ - Spaces GPU ํ๊ฒฝ์ ๋ง๊ฒ ์์ """
|
27 |
+
gc.collect()
|
28 |
+
if torch.cuda.is_available():
|
29 |
+
try:
|
30 |
+
with torch.cuda.device('cuda:0'): # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
31 |
+
torch.cuda.empty_cache()
|
32 |
+
except Exception as e:
|
33 |
+
print(f"GPU memory management warning: {e}")
|
34 |
+
|
35 |
def initialize_models():
|
36 |
+
"""๋ชจ๋ธ ์ด๊ธฐํ ํจ์ - Spaces GPU ํ๊ฒฝ์ ๋ง๊ฒ ์์ """
|
37 |
+
global segmenter, gd_model, gd_processor, pipe, translator
|
38 |
|
39 |
try:
|
40 |
# GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
|
|
49 |
gd_processor = GroundingDinoProcessor.from_pretrained(gd_model_path)
|
50 |
gd_model = GroundingDinoForObjectDetection.from_pretrained(
|
51 |
gd_model_path,
|
52 |
+
torch_dtype=torch.float16,
|
53 |
+
device_map='cuda:0' # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
54 |
)
|
55 |
|
56 |
# Segmenter ์ด๊ธฐํ
|
57 |
+
segmenter = BoxSegmenter(device='cuda:0') # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
|
|
|
|
58 |
|
59 |
# FLUX ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
60 |
pipe = FluxPipeline.from_pretrained(
|
|
|
69 |
hf_hub_download(
|
70 |
"ByteDance/Hyper-SD",
|
71 |
"Hyper-FLUX.1-dev-8steps-lora.safetensors",
|
72 |
+
token=HF_TOKEN
|
73 |
)
|
74 |
)
|
75 |
pipe.fuse_lora(lora_scale=0.125)
|
76 |
|
77 |
if torch.cuda.is_available():
|
78 |
+
pipe = pipe.to('cuda:0') # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
79 |
|
80 |
except Exception as e:
|
81 |
print(f"Model initialization error: {str(e)}")
|
82 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# GPU ์ค์
|
84 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช
์์ ์ผ๋ก cuda:0 ์ง์
|
85 |
|
|
|
603 |
interactive=bool(img and prompt),
|
604 |
variant="primary" if bool(img and prompt) else "secondary"
|
605 |
)
|
|
|
606 |
if __name__ == "__main__":
|
607 |
+
# CUDA ์ค์
|
608 |
+
if torch.cuda.is_available():
|
609 |
+
try:
|
610 |
+
torch.cuda.set_device('cuda:0') # ๋ช
์์ ์ผ๋ก cuda:0 ์ค์
|
611 |
+
torch.backends.cudnn.benchmark = True
|
612 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
613 |
+
except Exception as e:
|
614 |
+
print(f"CUDA setup warning: {e}")
|
615 |
+
|
616 |
+
# HF ํ ํฐ ์ค์
|
617 |
+
if HF_TOKEN:
|
618 |
+
login(token=HF_TOKEN, add_to_git_credential=False)
|
619 |
+
|
620 |
# ๋ชจ๋ธ ์ด๊ธฐํ
|
621 |
initialize_models()
|
622 |
|
|
|
824 |
queue=True
|
825 |
)
|
826 |
|
|
|
827 |
demo.queue(max_size=3)
|
828 |
demo.launch(
|
829 |
server_name="0.0.0.0",
|