aiqtech commited on
Commit
36dc32d
ยท
verified ยท
1 Parent(s): b209823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -46
app.py CHANGED
@@ -32,47 +32,46 @@ MAX_SEED = np.iinfo(np.int32).max
32
  TMP_DIR = "/tmp/Trellis-demo"
33
  os.makedirs(TMP_DIR, exist_ok=True)
34
 
 
 
 
 
 
 
 
 
 
 
 
35
  def initialize_models():
36
  global pipeline, translator, flux_pipe
37
 
38
  try:
39
- # Trellis ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™” (๋” ๊ฐ•ํ™”๋œ ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™”)
40
  pipeline = TrellisImageTo3DPipeline.from_pretrained(
41
  "JeffreyXiang/TRELLIS-image-large",
42
- device_map="auto",
43
- low_cpu_mem_usage=True,
44
- torch_dtype=torch.float16 # ๋ฐ˜์ •๋ฐ€๋„ ์‚ฌ์šฉ
45
  )
46
 
47
- # ๋ฒˆ์—ญ๊ธฐ ์ดˆ๊ธฐํ™” (๋” ์ž‘์€ ๋ชจ๋ธ ์‚ฌ์šฉ)
48
  translator = translation_pipeline(
49
  "translation",
50
  model="Helsinki-NLP/opus-mt-ko-en",
51
  device="cpu",
52
- model_kwargs={
53
- "low_cpu_mem_usage": True,
54
- "torch_dtype": torch.float16
55
- }
56
  )
57
 
58
- # Flux ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™” (๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™”)
59
  flux_pipe = FluxPipeline.from_pretrained(
60
  "black-forest-labs/FLUX.1-dev",
61
- device_map="auto",
62
- low_cpu_mem_usage=True,
63
- torch_dtype=torch.float16,
64
- variant="fp16"
65
  )
66
 
67
- # ๋ถˆํ•„์š”ํ•œ ์บ์‹œ ์ •๋ฆฌ
68
- free_memory()
69
-
70
  print("Models initialized successfully")
71
  return True
72
 
73
  except Exception as e:
74
  print(f"Model initialization error: {str(e)}")
75
- free_memory()
76
  return False
77
 
78
  def translate_if_korean(text):
@@ -144,16 +143,15 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
144
  def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float,
145
  ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int):
146
  try:
147
- free_memory()
 
 
148
 
149
  if randomize_seed:
150
  seed = np.random.randint(0, MAX_SEED)
151
 
152
  input_image = Image.open(f"{TMP_DIR}/{trial_id}.png")
153
 
154
- # GPU ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ์ œํ•œ
155
- torch.cuda.set_per_process_memory_fraction(0.6)
156
-
157
  # ๋” ์ž‘์€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์‚ฌ์šฉ
158
  max_size = 512
159
  if max(input_image.size) > max_size:
@@ -181,7 +179,6 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
181
  }
182
  )
183
 
184
- # ๋” ์ ์€ ํ”„๋ ˆ์ž„์œผ๋กœ ๋น„๋””์˜ค ์ƒ์„ฑ
185
  video = render_utils.render_video(outputs['gaussian'][0], num_frames=30)['color']
186
  video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=30)['normal']
187
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
@@ -193,12 +190,14 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
193
 
194
  state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
195
 
196
- free_memory()
 
 
197
  return state, video_path
198
 
199
  except Exception as e:
200
  print(f"Error in image_to_3d: {str(e)}")
201
- free_memory()
202
  raise e
203
 
204
  @spaces.GPU
@@ -256,23 +255,7 @@ footer {
256
  visibility: hidden;
257
  }
258
  """
259
- def free_memory():
260
- """๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ๊ฐ•ํ™”๋œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"""
261
- import gc
262
- import psutil
263
-
264
- # Python ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ๊ฐ•์ œ ์‹คํ–‰
265
- gc.collect()
266
-
267
- # CUDA ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ
268
- if torch.cuda.is_available():
269
- torch.cuda.empty_cache()
270
- torch.cuda.synchronize()
271
-
272
- # RAM ์บ์‹œ ์ •๋ฆฌ ์‹œ๋„
273
- if psutil.POSIX:
274
- import os
275
- os.system('sync')
276
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
277
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
278
  gr.Markdown("""
@@ -390,6 +373,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
390
  )
391
 
392
  if __name__ == "__main__":
 
393
  free_memory()
394
 
395
  # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
@@ -398,7 +382,7 @@ if __name__ == "__main__":
398
  exit(1)
399
 
400
  try:
401
- # ์ตœ์†Œ ํฌ๊ธฐ ์ด๋ฏธ์ง€๋กœ rembg ํ…Œ์ŠคํŠธ
402
  test_image = Image.fromarray(np.ones((64, 64, 3), dtype=np.uint8) * 255)
403
  pipeline.preprocess_image(test_image)
404
  except Exception as e:
@@ -410,7 +394,5 @@ if __name__ == "__main__":
410
  max_threads=2,
411
  show_error=True,
412
  cache_examples=False,
413
- enable_queue=True,
414
- server_port=7860,
415
- server_name="0.0.0.0"
416
  )
 
32
  TMP_DIR = "/tmp/Trellis-demo"
33
  os.makedirs(TMP_DIR, exist_ok=True)
34
 
35
+ def free_memory():
36
+ """๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"""
37
+ import gc
38
+ gc.collect()
39
+
40
+ @spaces.GPU
41
+ def free_gpu_memory():
42
+ """GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"""
43
+ if torch.cuda.is_available():
44
+ torch.cuda.empty_cache()
45
+
46
  def initialize_models():
47
  global pipeline, translator, flux_pipe
48
 
49
  try:
50
+ # Trellis ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™” (CPU ๋ชจ๋“œ๋กœ)
51
  pipeline = TrellisImageTo3DPipeline.from_pretrained(
52
  "JeffreyXiang/TRELLIS-image-large",
53
+ low_cpu_mem_usage=True
 
 
54
  )
55
 
56
+ # ๋ฒˆ์—ญ๊ธฐ ์ดˆ๊ธฐํ™”
57
  translator = translation_pipeline(
58
  "translation",
59
  model="Helsinki-NLP/opus-mt-ko-en",
60
  device="cpu",
61
+ model_kwargs={"low_cpu_mem_usage": True}
 
 
 
62
  )
63
 
64
+ # Flux ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™”
65
  flux_pipe = FluxPipeline.from_pretrained(
66
  "black-forest-labs/FLUX.1-dev",
67
+ low_cpu_mem_usage=True
 
 
 
68
  )
69
 
 
 
 
70
  print("Models initialized successfully")
71
  return True
72
 
73
  except Exception as e:
74
  print(f"Model initialization error: {str(e)}")
 
75
  return False
76
 
77
  def translate_if_korean(text):
 
143
  def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float,
144
  ss_sampling_steps: int, slat_guidance_strength: float, slat_sampling_steps: int):
145
  try:
146
+ if torch.cuda.is_available():
147
+ pipeline.to("cuda")
148
+ pipeline.to(torch.float16)
149
 
150
  if randomize_seed:
151
  seed = np.random.randint(0, MAX_SEED)
152
 
153
  input_image = Image.open(f"{TMP_DIR}/{trial_id}.png")
154
 
 
 
 
155
  # ๋” ์ž‘์€ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์‚ฌ์šฉ
156
  max_size = 512
157
  if max(input_image.size) > max_size:
 
179
  }
180
  )
181
 
 
182
  video = render_utils.render_video(outputs['gaussian'][0], num_frames=30)['color']
183
  video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=30)['normal']
184
  video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
 
190
 
191
  state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
192
 
193
+ # CPU ๋ชจ๋“œ๋กœ ๋Œ์•„๊ฐ€๊ธฐ
194
+ pipeline.to("cpu")
195
+
196
  return state, video_path
197
 
198
  except Exception as e:
199
  print(f"Error in image_to_3d: {str(e)}")
200
+ pipeline.to("cpu")
201
  raise e
202
 
203
  @spaces.GPU
 
255
  visibility: hidden;
256
  }
257
  """
258
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
260
  with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
261
  gr.Markdown("""
 
373
  )
374
 
375
  if __name__ == "__main__":
376
+ # CPU ๋ฉ”๋ชจ๋ฆฌ๋งŒ ์ •๋ฆฌ
377
  free_memory()
378
 
379
  # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
 
382
  exit(1)
383
 
384
  try:
385
+ # rembg ์‚ฌ์ „ ๋กœ๋“œ ์‹œ๋„ (์ž‘์€ ์ด๋ฏธ์ง€๋กœ)
386
  test_image = Image.fromarray(np.ones((64, 64, 3), dtype=np.uint8) * 255)
387
  pipeline.preprocess_image(test_image)
388
  except Exception as e:
 
394
  max_threads=2,
395
  show_error=True,
396
  cache_examples=False,
397
+ enable_queue=True
 
 
398
  )