aiqtech commited on
Commit
d6e9951
ยท
verified ยท
1 Parent(s): 856934e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -57
app.py CHANGED
@@ -36,38 +36,38 @@ g = GlobalVars()
36
  def initialize_models(device):
37
  try:
38
  print("Initializing models...")
39
- with torch.no_grad():
40
- # 3D ์ƒ์„ฑ ํŒŒ์ดํ”„๋ผ์ธ
41
- g.trellis_pipeline = TrellisImageTo3DPipeline.from_pretrained(
42
- "JeffreyXiang/TRELLIS-image-large"
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,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
- with torch.cuda.device(0):
219
- outputs = g.trellis_pipeline.run(
220
- image,
221
- seed=seed,
222
- formats=["gaussian", "mesh"],
223
- preprocess_image=False,
224
- sparse_structure_sampler_params={
225
- "steps": ss_sampling_steps,
226
- "cfg_strength": ss_guidance_strength,
227
- },
228
- slat_sampler_params={
229
- "steps": slat_sampling_steps,
230
- "cfg_strength": slat_guidance_strength,
231
- },
232
- )
 
 
 
 
 
233
 
234
- video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
235
- video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
236
- video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
237
- new_trial_id = str(uuid.uuid4())
238
- video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
239
- os.makedirs(os.path.dirname(video_path), exist_ok=True)
240
- imageio.mimsave(video_path, video, fps=15)
241
- state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
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