Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ initialization_message = "๋ชจ๋ธ ๋ก๋ฉ ์ค... ์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์."
|
|
17 |
# ๊ฐ๋จํ ์ธ์ฉ ์ ๋ณด ์ถ๊ฐ
|
18 |
_CITE_ = """PuLID: Person-under-Language Image Diffusion Model"""
|
19 |
|
20 |
-
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ ๋ฐ ์ฅ์น ์ค์
|
21 |
def get_device():
|
22 |
if torch.cuda.is_available():
|
23 |
return torch.device('cuda')
|
@@ -44,8 +44,8 @@ def get_models(name: str, device, offload: bool):
|
|
44 |
|
45 |
class FluxGenerator:
|
46 |
def __init__(self):
|
47 |
-
# GPU
|
48 |
-
self.device =
|
49 |
self.offload = False
|
50 |
self.model_name = 'flux-dev'
|
51 |
self.initialized = False
|
@@ -63,6 +63,9 @@ class FluxGenerator:
|
|
63 |
from pulid.pipeline_flux import PuLIDPipeline
|
64 |
from flux.sampling import prepare
|
65 |
|
|
|
|
|
|
|
66 |
print("๋ชจ๋ธ ์ด๊ธฐํ ์์...")
|
67 |
self.model, self.ae, self.t5, self.clip_model = get_models(
|
68 |
self.model_name,
|
@@ -98,11 +101,12 @@ class FluxGenerator:
|
|
98 |
initialization_message = f"๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {str(e)}"
|
99 |
|
100 |
|
101 |
-
# ์ง์ฐ ๋ก๋ฉ์ ์ํ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ด๊ธฐํ ํจ์
|
102 |
-
|
|
|
103 |
global flux_generator, model_initialized, initialization_message
|
104 |
|
105 |
-
print("
|
106 |
|
107 |
try:
|
108 |
# ์ง์ฐ ์ํฌํธ
|
@@ -112,19 +116,18 @@ def initialize_models_in_background():
|
|
112 |
|
113 |
# ๋ชจ๋ธ ์ด๊ธฐํ
|
114 |
flux_generator = FluxGenerator()
|
115 |
-
|
116 |
-
# 30์ด ํ์ ์ด๊ธฐํ ์์ (UI๊ฐ ๋จผ์ ๋ก๋๋๋๋ก)
|
117 |
-
time.sleep(30)
|
118 |
flux_generator.initialize()
|
119 |
|
120 |
model_initialized = flux_generator.initialized
|
121 |
|
122 |
except Exception as e:
|
123 |
import traceback
|
124 |
-
error_msg = f"
|
125 |
print(error_msg)
|
126 |
model_initialized = False
|
127 |
initialization_message = f"๋ชจ๋ธ ์ด๊ธฐํ ์ค๋ฅ: {str(e)}"
|
|
|
|
|
128 |
|
129 |
|
130 |
# ๋ชจ๋ธ ์ํ ํ์ธ ํจ์
|
@@ -151,7 +154,7 @@ def generate_image(
|
|
151 |
|
152 |
# ๋ชจ๋ธ์ด ์ด๊ธฐํ๋์ง ์์์ผ๋ฉด ์ค๋ฅ ๋ฉ์์ง ๋ฐํ
|
153 |
if not model_initialized:
|
154 |
-
return None, "๋ชจ๋ธ ์ด๊ธฐํ๊ฐ ์๋ฃ๋์ง ์์์ต๋๋ค.
|
155 |
|
156 |
# ID ์ด๋ฏธ์ง๊ฐ ์์ผ๋ฉด ์คํ ๋ถ๊ฐ
|
157 |
if id_image is None:
|
@@ -322,6 +325,11 @@ def create_demo():
|
|
322 |
|
323 |
# ๋ชจ๋ธ ์ํ ํ์
|
324 |
status_box = gr.Textbox(label="๋ชจ๋ธ ์ํ", value=initialization_message)
|
|
|
|
|
|
|
|
|
|
|
325 |
refresh_btn = gr.Button("์ํ ์๋ก๊ณ ์นจ")
|
326 |
refresh_btn.click(fn=check_model_status, inputs=[], outputs=[status_box])
|
327 |
|
@@ -369,9 +377,6 @@ def create_demo():
|
|
369 |
id_weight, neg_prompt, true_cfg, gamma, eta]
|
370 |
)
|
371 |
|
372 |
-
# ์ฃผ๊ธฐ์ ์ํ ์
๋ฐ์ดํธ ์ค์
|
373 |
-
demo.load(fn=check_model_status, inputs=[], outputs=[status_box], every=5) # 5์ด๋ง๋ค ์
๋ฐ์ดํธ
|
374 |
-
|
375 |
# Gradio ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
376 |
generate_btn.click(
|
377 |
fn=generate_image,
|
@@ -396,18 +401,9 @@ if __name__ == "__main__":
|
|
396 |
|
397 |
print("Hugging Face Spaces ํ๊ฒฝ์์ ์คํ ์ค์
๋๋ค. GPU ํ ๋น์ ์์ฒญํฉ๋๋ค.")
|
398 |
|
399 |
-
#
|
400 |
-
|
401 |
-
|
402 |
-
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ ํ ์ค์ (ํ๊ฒฝ์ ๋ฐ๋ผ ์กฐ์ ํ์)
|
403 |
-
try:
|
404 |
-
import torch.cuda
|
405 |
-
if torch.cuda.is_available():
|
406 |
-
# GPU ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ์ ํ (ํ์์ ์กฐ์ )
|
407 |
-
torch.cuda.set_per_process_memory_fraction(0.8) # ์ต๋ 80% GPU ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ
|
408 |
-
except Exception as e:
|
409 |
-
print(f"๋ฉ๋ชจ๋ฆฌ ์ ํ ์ค์ ์ค ์ค๋ฅ: {e}")
|
410 |
|
411 |
demo = create_demo()
|
412 |
# ๋๋ฒ๊ทธ ๋ชจ๋ ํ์ฑํ
|
413 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=args.port
|
|
|
17 |
# ๊ฐ๋จํ ์ธ์ฉ ์ ๋ณด ์ถ๊ฐ
|
18 |
_CITE_ = """PuLID: Person-under-Language Image Diffusion Model"""
|
19 |
|
20 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ ๋ฐ ์ฅ์น ์ค์ - ๋ฉ์ธ ํ๋ก์ธ์ค์์๋ ํธ์ถํ์ง ์์
|
21 |
def get_device():
|
22 |
if torch.cuda.is_available():
|
23 |
return torch.device('cuda')
|
|
|
44 |
|
45 |
class FluxGenerator:
|
46 |
def __init__(self):
|
47 |
+
# GPU ์ด๊ธฐํ๋ Spaces GPU ๋ฐ์ฝ๋ ์ดํฐ ์์์๋ง ์ํ
|
48 |
+
self.device = None # ์ด๊ธฐํ ์์ ์๋ device๋ฅผ ํ ๋นํ์ง ์์
|
49 |
self.offload = False
|
50 |
self.model_name = 'flux-dev'
|
51 |
self.initialized = False
|
|
|
63 |
from pulid.pipeline_flux import PuLIDPipeline
|
64 |
from flux.sampling import prepare
|
65 |
|
66 |
+
# ์ด ์์ ์์ ์ฅ์น ์ค์ (GPU ๋ฐ์ฝ๋ ์ดํฐ ๋ด์์๋ง ํธ์ถ๋จ)
|
67 |
+
self.device = get_device()
|
68 |
+
|
69 |
print("๋ชจ๋ธ ์ด๊ธฐํ ์์...")
|
70 |
self.model, self.ae, self.t5, self.clip_model = get_models(
|
71 |
self.model_name,
|
|
|
101 |
initialization_message = f"๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {str(e)}"
|
102 |
|
103 |
|
104 |
+
# ์ง์ฐ ๋ก๋ฉ์ ์ํ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ด๊ธฐํ ํจ์ - GPU ๋ฐ์ฝ๋ ์ดํฐ๋ก ๋ณ๊ฒฝ
|
105 |
+
@spaces.GPU(duration=60)
|
106 |
+
def initialize_models():
|
107 |
global flux_generator, model_initialized, initialization_message
|
108 |
|
109 |
+
print("GPU ๋ฐ์ฝ๋ ์ดํฐ ๋ด์์ ๋ชจ๋ธ ์ด๊ธฐํ ์์...")
|
110 |
|
111 |
try:
|
112 |
# ์ง์ฐ ์ํฌํธ
|
|
|
116 |
|
117 |
# ๋ชจ๋ธ ์ด๊ธฐํ
|
118 |
flux_generator = FluxGenerator()
|
|
|
|
|
|
|
119 |
flux_generator.initialize()
|
120 |
|
121 |
model_initialized = flux_generator.initialized
|
122 |
|
123 |
except Exception as e:
|
124 |
import traceback
|
125 |
+
error_msg = f"์ด๊ธฐํ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}\n{traceback.format_exc()}"
|
126 |
print(error_msg)
|
127 |
model_initialized = False
|
128 |
initialization_message = f"๋ชจ๋ธ ์ด๊ธฐํ ์ค๋ฅ: {str(e)}"
|
129 |
+
|
130 |
+
return initialization_message
|
131 |
|
132 |
|
133 |
# ๋ชจ๋ธ ์ํ ํ์ธ ํจ์
|
|
|
154 |
|
155 |
# ๋ชจ๋ธ์ด ์ด๊ธฐํ๋์ง ์์์ผ๋ฉด ์ค๋ฅ ๋ฉ์์ง ๋ฐํ
|
156 |
if not model_initialized:
|
157 |
+
return None, "๋ชจ๋ธ ์ด๊ธฐํ๊ฐ ์๋ฃ๋์ง ์์์ต๋๋ค. ๋ชจ๋ธ ์ด๊ธฐํ ๋ฒํผ์ ๋๋ฌ์ฃผ์ธ์."
|
158 |
|
159 |
# ID ์ด๋ฏธ์ง๊ฐ ์์ผ๋ฉด ์คํ ๋ถ๊ฐ
|
160 |
if id_image is None:
|
|
|
325 |
|
326 |
# ๋ชจ๋ธ ์ํ ํ์
|
327 |
status_box = gr.Textbox(label="๋ชจ๋ธ ์ํ", value=initialization_message)
|
328 |
+
|
329 |
+
# ์ด๊ธฐํ ๋ฒํผ ์ถ๊ฐ (๋ฐฑ๊ทธ๋ผ์ด๋ ์ด๊ธฐํ ๋์ ๋ช
์์ ์ด๊ธฐํ ๋ฒํผ ์ฌ์ฉ)
|
330 |
+
init_btn = gr.Button("๋ชจ๋ธ ์ด๊ธฐํ")
|
331 |
+
init_btn.click(fn=initialize_models, inputs=[], outputs=[status_box])
|
332 |
+
|
333 |
refresh_btn = gr.Button("์ํ ์๋ก๊ณ ์นจ")
|
334 |
refresh_btn.click(fn=check_model_status, inputs=[], outputs=[status_box])
|
335 |
|
|
|
377 |
id_weight, neg_prompt, true_cfg, gamma, eta]
|
378 |
)
|
379 |
|
|
|
|
|
|
|
380 |
# Gradio ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
381 |
generate_btn.click(
|
382 |
fn=generate_image,
|
|
|
401 |
|
402 |
print("Hugging Face Spaces ํ๊ฒฝ์์ ์คํ ์ค์
๋๋ค. GPU ํ ๋น์ ์์ฒญํฉ๋๋ค.")
|
403 |
|
404 |
+
# ๋ฉ์ธ ํ๋ก์ธ์ค์์๋ CUDA ์ด๊ธฐํํ์ง ์์
|
405 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋ ์ค๋ ๋ ๋์ ๋ช
์์ ๋ฒํผ์ผ๋ก ์ด๊ธฐํ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
406 |
|
407 |
demo = create_demo()
|
408 |
# ๋๋ฒ๊ทธ ๋ชจ๋ ํ์ฑํ
|
409 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=args.port)
|