Spaces:
Running
on
L40S
Running
on
L40S
Update app.py
Browse files
app.py
CHANGED
@@ -23,45 +23,39 @@ os.makedirs(TMP_DIR, exist_ok=True)
|
|
23 |
def initialize_models():
|
24 |
global pipeline, translator, flux_pipe
|
25 |
|
26 |
-
# CUDA ๋ฉ๋ชจ๋ฆฌ ์ด๊ธฐํ
|
27 |
-
if torch.cuda.is_available():
|
28 |
-
torch.cuda.empty_cache()
|
29 |
-
|
30 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Trellis ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
32 |
-
pipeline = TrellisImageTo3DPipeline.from_pretrained(
|
33 |
-
|
34 |
-
device_map="auto" # Zero GPU ํ๊ฒฝ์ ๋ง๊ฒ ์๋ device ๋งคํ
|
35 |
-
)
|
36 |
|
37 |
-
# ๋ฒ์ญ๊ธฐ ์ด๊ธฐํ
|
38 |
translator = translation_pipeline(
|
39 |
"translation",
|
40 |
model="Helsinki-NLP/opus-mt-ko-en",
|
41 |
-
|
42 |
)
|
43 |
|
44 |
-
# Flux ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
45 |
flux_pipe = FluxPipeline.from_pretrained(
|
46 |
"black-forest-labs/FLUX.1-dev",
|
47 |
-
torch_dtype=torch.float16
|
48 |
-
device_map="auto"
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
)
|
56 |
-
flux_pipe.fuse_lora(lora_scale=1.0)
|
57 |
|
58 |
except Exception as e:
|
59 |
-
print(f"
|
60 |
-
|
61 |
-
|
62 |
-
raise e
|
63 |
-
|
64 |
-
|
65 |
|
66 |
def translate_if_korean(text):
|
67 |
if any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text):
|
@@ -116,33 +110,32 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
|
116 |
return gs, mesh, state['trial_id']
|
117 |
|
118 |
@spaces.GPU
|
119 |
-
def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float,
|
120 |
ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int):
|
121 |
try:
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
if randomize_seed:
|
126 |
seed = np.random.randint(0, MAX_SEED)
|
127 |
|
128 |
input_image = Image.open(f"{TMP_DIR}/{trial_id}.png")
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
|
147 |
# ๋น๋์ค ๋ ๋๋ง
|
148 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
@@ -163,8 +156,7 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
|
|
163 |
|
164 |
except Exception as e:
|
165 |
print(f"Error in image_to_3d: {str(e)}")
|
166 |
-
|
167 |
-
torch.cuda.empty_cache()
|
168 |
raise e
|
169 |
|
170 |
@spaces.GPU
|
@@ -334,17 +326,25 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
334 |
)
|
335 |
|
336 |
if __name__ == "__main__":
|
337 |
-
#
|
338 |
-
torch.cuda.
|
|
|
339 |
|
340 |
-
# ๋ชจ๋ธ ์ด๊ธฐํ
|
341 |
-
initialize_models()
|
|
|
|
|
342 |
|
343 |
try:
|
344 |
-
# rembg ์ฌ์ ๋ก๋
|
345 |
-
|
346 |
-
|
347 |
-
|
|
|
348 |
|
349 |
# Gradio ์ฑ ์คํ
|
350 |
-
demo.
|
|
|
|
|
|
|
|
|
|
23 |
def initialize_models():
|
24 |
global pipeline, translator, flux_pipe
|
25 |
|
|
|
|
|
|
|
|
|
26 |
try:
|
27 |
+
# GPU ๋ฉ๋ชจ๋ฆฌ ์ด๊ธฐํ
|
28 |
+
torch.cuda.empty_cache()
|
29 |
+
|
30 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
|
31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
32 |
+
|
33 |
# Trellis ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
34 |
+
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
35 |
+
pipeline.to(device)
|
|
|
|
|
36 |
|
37 |
+
# ๋ฒ์ญ๊ธฐ ์ด๊ธฐํ
|
38 |
translator = translation_pipeline(
|
39 |
"translation",
|
40 |
model="Helsinki-NLP/opus-mt-ko-en",
|
41 |
+
device=0 if device=="cuda" else -1
|
42 |
)
|
43 |
|
44 |
+
# Flux ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
45 |
flux_pipe = FluxPipeline.from_pretrained(
|
46 |
"black-forest-labs/FLUX.1-dev",
|
47 |
+
torch_dtype=torch.float16 if device=="cuda" else torch.float32
|
|
|
48 |
)
|
49 |
|
50 |
+
if device == "cuda":
|
51 |
+
flux_pipe.enable_model_cpu_offload()
|
52 |
+
|
53 |
+
return True
|
|
|
|
|
54 |
|
55 |
except Exception as e:
|
56 |
+
print(f"Model initialization error: {str(e)}")
|
57 |
+
torch.cuda.empty_cache()
|
58 |
+
return False
|
|
|
|
|
|
|
59 |
|
60 |
def translate_if_korean(text):
|
61 |
if any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text):
|
|
|
110 |
return gs, mesh, state['trial_id']
|
111 |
|
112 |
@spaces.GPU
|
113 |
+
def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float,
|
114 |
ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int):
|
115 |
try:
|
116 |
+
torch.cuda.empty_cache()
|
117 |
+
|
|
|
118 |
if randomize_seed:
|
119 |
seed = np.random.randint(0, MAX_SEED)
|
120 |
|
121 |
input_image = Image.open(f"{TMP_DIR}/{trial_id}.png")
|
122 |
|
123 |
+
with torch.cuda.amp.autocast(enabled=torch.cuda.is_available()):
|
124 |
+
with torch.no_grad():
|
125 |
+
outputs = pipeline.run(
|
126 |
+
input_image,
|
127 |
+
seed=seed,
|
128 |
+
formats=["gaussian", "mesh"],
|
129 |
+
preprocess_image=False,
|
130 |
+
sparse_structure_sampler_params={
|
131 |
+
"steps": ss_sampling_steps,
|
132 |
+
"cfg_strength": ss_guidance_strength,
|
133 |
+
},
|
134 |
+
slat_sampler_params={
|
135 |
+
"steps": slat_sampling_steps,
|
136 |
+
"cfg_strength": slat_guidance_strength,
|
137 |
+
}
|
138 |
+
)
|
139 |
|
140 |
# ๋น๋์ค ๋ ๋๋ง
|
141 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
|
|
156 |
|
157 |
except Exception as e:
|
158 |
print(f"Error in image_to_3d: {str(e)}")
|
159 |
+
torch.cuda.empty_cache()
|
|
|
160 |
raise e
|
161 |
|
162 |
@spaces.GPU
|
|
|
326 |
)
|
327 |
|
328 |
if __name__ == "__main__":
|
329 |
+
# ์ด๊ธฐ GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
330 |
+
if torch.cuda.is_available():
|
331 |
+
torch.cuda.empty_cache()
|
332 |
|
333 |
+
# ๋ชจ๋ธ ์ด๊ธฐํ ํ์ธ
|
334 |
+
if not initialize_models():
|
335 |
+
print("Failed to initialize models")
|
336 |
+
exit(1)
|
337 |
|
338 |
try:
|
339 |
+
# rembg ์ฌ์ ๋ก๋ ์๋
|
340 |
+
test_image = Image.fromarray(np.zeros((256, 256, 3), dtype=np.uint8))
|
341 |
+
pipeline.preprocess_image(test_image)
|
342 |
+
except Exception as e:
|
343 |
+
print(f"Warning: Failed to preload rembg: {str(e)}")
|
344 |
|
345 |
# Gradio ์ฑ ์คํ
|
346 |
+
demo.queue(concurrency_count=1).launch(
|
347 |
+
share=True,
|
348 |
+
enable_queue=True,
|
349 |
+
max_threads=1
|
350 |
+
)
|