Spaces:
Running
on
L40S
Running
on
L40S
Update app.py
Browse files
app.py
CHANGED
@@ -36,38 +36,38 @@ g = GlobalVars()
|
|
36 |
def initialize_models(device):
|
37 |
try:
|
38 |
print("Initializing models...")
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
except Exception as e:
|
73 |
print(f"Error during model initialization: {str(e)}")
|
@@ -78,6 +78,7 @@ torch.cuda.empty_cache()
|
|
78 |
torch.backends.cuda.matmul.allow_tf32 = True
|
79 |
torch.backends.cudnn.benchmark = True
|
80 |
|
|
|
81 |
# ํ๊ฒฝ ๋ณ์ ์ค์
|
82 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
|
83 |
os.environ['SPCONV_ALGO'] = 'native'
|
@@ -85,6 +86,7 @@ os.environ['SPARSE_BACKEND'] = 'native'
|
|
85 |
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
86 |
os.environ['TORCH_USE_CUDA_DSA'] = '1'
|
87 |
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
|
|
88 |
|
89 |
# CUDA ์ด๊ธฐํ ๋ฐฉ์ง
|
90 |
torch.set_grad_enabled(False)
|
@@ -215,36 +217,46 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
|
|
215 |
image = Image.open(image_path)
|
216 |
print(f"Successfully loaded image with size: {image.size}")
|
217 |
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
|
|
|
|
|
|
|
|
243 |
torch.cuda.empty_cache()
|
244 |
-
return state, video_path
|
245 |
|
246 |
except Exception as e:
|
247 |
print(f"Error in image_to_3d: {str(e)}")
|
|
|
|
|
248 |
torch.cuda.empty_cache()
|
249 |
return None, None
|
250 |
|
|
|
36 |
def initialize_models(device):
|
37 |
try:
|
38 |
print("Initializing models...")
|
39 |
+
# 3D ์์ฑ ํ์ดํ๋ผ์ธ
|
40 |
+
g.trellis_pipeline = TrellisImageTo3DPipeline.from_pretrained(
|
41 |
+
"JeffreyXiang/TRELLIS-image-large",
|
42 |
+
torch_dtype=torch.float32 # ๋ช
์์ ์ผ๋ก dtype ์ง์
|
43 |
+
)
|
44 |
+
|
45 |
+
# ์ด๋ฏธ์ง ์์ฑ ํ์ดํ๋ผ์ธ
|
46 |
+
print("Loading flux_pipe...")
|
47 |
+
g.flux_pipe = FluxPipeline.from_pretrained(
|
48 |
+
"black-forest-labs/FLUX.1-dev",
|
49 |
+
torch_dtype=torch.bfloat16,
|
50 |
+
device_map="balanced"
|
51 |
+
)
|
52 |
+
|
53 |
+
# Hyper-SD LoRA ๋ก๋
|
54 |
+
print("Loading LoRA weights...")
|
55 |
+
lora_path = hf_hub_download(
|
56 |
+
"ByteDance/Hyper-SD",
|
57 |
+
"Hyper-FLUX.1-dev-8steps-lora.safetensors",
|
58 |
+
use_auth_token=HF_TOKEN
|
59 |
+
)
|
60 |
+
g.flux_pipe.load_lora_weights(lora_path)
|
61 |
+
g.flux_pipe.fuse_lora(lora_scale=0.125)
|
62 |
+
|
63 |
+
# ๋ฒ์ญ๊ธฐ ์ด๊ธฐํ
|
64 |
+
print("Initializing translator...")
|
65 |
+
g.translator = transformers_pipeline(
|
66 |
+
"translation",
|
67 |
+
model="Helsinki-NLP/opus-mt-ko-en",
|
68 |
+
device=device
|
69 |
+
)
|
70 |
+
print("Model initialization completed successfully")
|
71 |
|
72 |
except Exception as e:
|
73 |
print(f"Error during model initialization: {str(e)}")
|
|
|
78 |
torch.backends.cuda.matmul.allow_tf32 = True
|
79 |
torch.backends.cudnn.benchmark = True
|
80 |
|
81 |
+
# ํ๊ฒฝ ๋ณ์ ์ค์
|
82 |
# ํ๊ฒฝ ๋ณ์ ์ค์
|
83 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
|
84 |
os.environ['SPCONV_ALGO'] = 'native'
|
|
|
86 |
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
87 |
os.environ['TORCH_USE_CUDA_DSA'] = '1'
|
88 |
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
89 |
+
os.environ['XFORMERS_FORCE_DISABLE_TRITON'] = '1' # xformers ๊ด๋ จ ์ค์ ์ถ๊ฐ
|
90 |
|
91 |
# CUDA ์ด๊ธฐํ ๋ฐฉ์ง
|
92 |
torch.set_grad_enabled(False)
|
|
|
217 |
image = Image.open(image_path)
|
218 |
print(f"Successfully loaded image with size: {image.size}")
|
219 |
|
220 |
+
# Move model to GPU
|
221 |
+
g.trellis_pipeline.to("cuda")
|
222 |
+
|
223 |
+
try:
|
224 |
+
with torch.inference_mode():
|
225 |
+
with torch.cuda.amp.autocast():
|
226 |
+
outputs = g.trellis_pipeline.run(
|
227 |
+
image,
|
228 |
+
seed=seed,
|
229 |
+
formats=["gaussian", "mesh"],
|
230 |
+
preprocess_image=False,
|
231 |
+
sparse_structure_sampler_params={
|
232 |
+
"steps": ss_sampling_steps,
|
233 |
+
"cfg_strength": ss_guidance_strength,
|
234 |
+
},
|
235 |
+
slat_sampler_params={
|
236 |
+
"steps": slat_sampling_steps,
|
237 |
+
"cfg_strength": slat_guidance_strength,
|
238 |
+
},
|
239 |
+
)
|
240 |
|
241 |
+
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
242 |
+
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
243 |
+
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
244 |
+
new_trial_id = str(uuid.uuid4())
|
245 |
+
video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
|
246 |
+
os.makedirs(os.path.dirname(video_path), exist_ok=True)
|
247 |
+
imageio.mimsave(video_path, video, fps=15)
|
248 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
|
249 |
+
|
250 |
+
return state, video_path
|
251 |
+
finally:
|
252 |
+
# Move model back to CPU
|
253 |
+
g.trellis_pipeline.to("cpu")
|
254 |
torch.cuda.empty_cache()
|
|
|
255 |
|
256 |
except Exception as e:
|
257 |
print(f"Error in image_to_3d: {str(e)}")
|
258 |
+
if hasattr(g.trellis_pipeline, 'device') and g.trellis_pipeline.device.type == "cuda":
|
259 |
+
g.trellis_pipeline.to("cpu")
|
260 |
torch.cuda.empty_cache()
|
261 |
return None, None
|
262 |
|