Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,30 +23,45 @@ os.makedirs(TMP_DIR, exist_ok=True)
|
|
| 23 |
def initialize_models():
|
| 24 |
global pipeline, translator, flux_pipe
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
raise ValueError("HF_TOKEN environment variable is not set. Please set your Hugging Face token.")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def translate_if_korean(text):
|
| 52 |
if any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text):
|
|
@@ -101,32 +116,56 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
|
| 101 |
return gs, mesh, state['trial_id']
|
| 102 |
|
| 103 |
@spaces.GPU
|
| 104 |
-
def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_strength: float,
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
@spaces.GPU
|
| 132 |
def generate_image_from_text(prompt, height, width, guidance_scale, num_steps):
|
|
@@ -165,9 +204,16 @@ def deactivate_button() -> gr.Button:
|
|
| 165 |
return gr.Button(interactive=False)
|
| 166 |
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
gr.Markdown("""
|
| 170 |
-
# 3D Asset Creation & Text-to-Image Generation
|
| 171 |
""")
|
| 172 |
|
| 173 |
with gr.Tabs():
|
|
@@ -287,11 +333,18 @@ with gr.Blocks() as demo:
|
|
| 287 |
outputs=[txt2img_output]
|
| 288 |
)
|
| 289 |
|
| 290 |
-
# Launch the Gradio app
|
| 291 |
if __name__ == "__main__":
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
try:
|
| 294 |
-
|
|
|
|
| 295 |
except:
|
| 296 |
pass
|
| 297 |
-
|
|
|
|
|
|
|
|
|
| 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 |
+
"JeffreyXiang/TRELLIS-image-large",
|
| 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 |
+
device_map="auto"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Flux ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
| 45 |
+
flux_pipe = FluxPipeline.from_pretrained(
|
| 46 |
+
"black-forest-labs/FLUX.1-dev",
|
| 47 |
+
torch_dtype=torch.float16, # bfloat16 ๋์ float16 ์ฌ์ฉ
|
| 48 |
+
device_map="auto"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# LoRA ๊ฐ์ค์น ๋ก๋
|
| 52 |
+
flux_pipe.load_lora_weights(
|
| 53 |
+
"gokaygokay/Flux-Game-Assets-LoRA-v2",
|
| 54 |
+
device_map="auto"
|
| 55 |
+
)
|
| 56 |
+
flux_pipe.fuse_lora(lora_scale=1.0)
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error initializing models: {str(e)}")
|
| 60 |
+
if torch.cuda.is_available():
|
| 61 |
+
torch.cuda.empty_cache()
|
| 62 |
+
raise e
|
| 63 |
+
|
| 64 |
+
|
| 65 |
|
| 66 |
def translate_if_korean(text):
|
| 67 |
if any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text):
|
|
|
|
| 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 |
+
if torch.cuda.is_available():
|
| 123 |
+
torch.cuda.empty_cache()
|
| 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 |
+
with torch.cuda.amp.autocast(enabled=True):
|
| 132 |
+
outputs = pipeline.run(
|
| 133 |
+
input_image,
|
| 134 |
+
seed=seed,
|
| 135 |
+
formats=["gaussian", "mesh"],
|
| 136 |
+
preprocess_image=False,
|
| 137 |
+
sparse_structure_sampler_params={
|
| 138 |
+
"steps": ss_sampling_steps,
|
| 139 |
+
"cfg_strength": ss_guidance_strength,
|
| 140 |
+
},
|
| 141 |
+
slat_sampler_params={
|
| 142 |
+
"steps": slat_sampling_steps,
|
| 143 |
+
"cfg_strength": slat_guidance_strength,
|
| 144 |
+
}
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# ๋น๋์ค ๋ ๋๋ง
|
| 148 |
+
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 149 |
+
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 150 |
+
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
| 151 |
+
|
| 152 |
+
trial_id = str(uuid.uuid4())
|
| 153 |
+
video_path = f"{TMP_DIR}/{trial_id}.mp4"
|
| 154 |
+
os.makedirs(os.path.dirname(video_path), exist_ok=True)
|
| 155 |
+
imageio.mimsave(video_path, video, fps=15)
|
| 156 |
+
|
| 157 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
|
| 158 |
+
|
| 159 |
+
if torch.cuda.is_available():
|
| 160 |
+
torch.cuda.empty_cache()
|
| 161 |
+
|
| 162 |
+
return state, video_path
|
| 163 |
+
|
| 164 |
+
except Exception as e:
|
| 165 |
+
print(f"Error in image_to_3d: {str(e)}")
|
| 166 |
+
if torch.cuda.is_available():
|
| 167 |
+
torch.cuda.empty_cache()
|
| 168 |
+
raise e
|
| 169 |
|
| 170 |
@spaces.GPU
|
| 171 |
def generate_image_from_text(prompt, height, width, guidance_scale, num_steps):
|
|
|
|
| 204 |
return gr.Button(interactive=False)
|
| 205 |
|
| 206 |
|
| 207 |
+
css = """
|
| 208 |
+
footer {
|
| 209 |
+
visibility: hidden;
|
| 210 |
+
}
|
| 211 |
+
"""
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
| 215 |
gr.Markdown("""
|
| 216 |
+
# Craft3D : 3D Asset Creation & Text-to-Image Generation
|
| 217 |
""")
|
| 218 |
|
| 219 |
with gr.Tabs():
|
|
|
|
| 333 |
outputs=[txt2img_output]
|
| 334 |
)
|
| 335 |
|
|
|
|
| 336 |
if __name__ == "__main__":
|
| 337 |
+
# CUDA ๋ฉ๋ชจ๋ฆฌ ์บ์ ์ด๊ธฐํ
|
| 338 |
+
torch.cuda.empty_cache()
|
| 339 |
+
|
| 340 |
+
# ๋ชจ๋ธ ์ด๊ธฐํ
|
| 341 |
+
initialize_models()
|
| 342 |
+
|
| 343 |
try:
|
| 344 |
+
# rembg ์ฌ์ ๋ก๋
|
| 345 |
+
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)))
|
| 346 |
except:
|
| 347 |
pass
|
| 348 |
+
|
| 349 |
+
# Gradio ์ฑ ์คํ
|
| 350 |
+
demo.launch(share=True) # share=True ์ถ๊ฐ
|