Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 197 |
return state, video_path
|
| 198 |
|
| 199 |
except Exception as e:
|
| 200 |
print(f"Error in image_to_3d: {str(e)}")
|
| 201 |
-
|
| 202 |
raise e
|
| 203 |
|
| 204 |
@spaces.GPU
|
|
@@ -256,23 +255,7 @@ footer {
|
|
| 256 |
visibility: hidden;
|
| 257 |
}
|
| 258 |
"""
|
| 259 |
-
|
| 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 |
-
#
|
| 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 |
)
|