Spaces:
Running
Running
Delete app-backup.py
Browse files- app-backup.py +0 -275
app-backup.py
DELETED
@@ -1,275 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
# ZeroGPU 환경 설정 - 가장 먼저 실행되어야 함!
|
3 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
4 |
-
os.environ['ZEROGPU'] = '1' # ZeroGPU 환경임을 표시
|
5 |
-
|
6 |
-
import spaces # spaces import는 환경 설정 후에
|
7 |
-
import shlex
|
8 |
-
import subprocess
|
9 |
-
|
10 |
-
import os
|
11 |
-
|
12 |
-
# ZeroGPU 환경 설정
|
13 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = '' # 초기화 시점에는 GPU 비활성화
|
14 |
-
|
15 |
-
subprocess.run(shlex.split("pip install pip==24.0"), check=True)
|
16 |
-
subprocess.run(
|
17 |
-
shlex.split(
|
18 |
-
"pip install package/onnxruntime_gpu-1.17.0-cp310-cp310-manylinux_2_28_x86_64.whl --force-reinstall --no-deps"
|
19 |
-
), check=True
|
20 |
-
)
|
21 |
-
subprocess.run(
|
22 |
-
shlex.split(
|
23 |
-
"pip install package/nvdiffrast-0.3.1.torch-cp310-cp310-linux_x86_64.whl --force-reinstall --no-deps"
|
24 |
-
), check=True
|
25 |
-
)
|
26 |
-
|
27 |
-
# 모델 체크포인트 다운로드 및 torch 설정
|
28 |
-
if __name__ == "__main__":
|
29 |
-
from huggingface_hub import snapshot_download
|
30 |
-
|
31 |
-
snapshot_download("public-data/Unique3D", repo_type="model", local_dir="./ckpt")
|
32 |
-
|
33 |
-
import os
|
34 |
-
import sys
|
35 |
-
sys.path.append(os.curdir)
|
36 |
-
import torch
|
37 |
-
torch.set_float32_matmul_precision('medium')
|
38 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
39 |
-
torch.set_grad_enabled(False)
|
40 |
-
|
41 |
-
import fire
|
42 |
-
import gradio as gr
|
43 |
-
from gradio_app.gradio_3dgen import create_ui as create_3d_ui
|
44 |
-
from gradio_app.all_models import model_zoo
|
45 |
-
|
46 |
-
# ===============================
|
47 |
-
# Text-to-IMAGE 관련 API 함수 정의
|
48 |
-
# ===============================
|
49 |
-
@spaces.GPU(duration=60) # GPU 사용 시간 60초로 설정
|
50 |
-
def text_to_image(height, width, steps, scales, prompt, seed):
|
51 |
-
"""
|
52 |
-
주어진 파라미터를 이용해 외부 API의 /process_and_save_image 엔드포인트를 호출하여 이미지를 생성한다.
|
53 |
-
"""
|
54 |
-
# GPU가 할당된 상태에서 실행
|
55 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
56 |
-
|
57 |
-
from gradio_client import Client
|
58 |
-
client = Client(os.getenv("CLIENT_API"))
|
59 |
-
result = client.predict(
|
60 |
-
height,
|
61 |
-
width,
|
62 |
-
steps,
|
63 |
-
scales,
|
64 |
-
prompt,
|
65 |
-
seed,
|
66 |
-
api_name="/process_and_save_image"
|
67 |
-
)
|
68 |
-
if isinstance(result, dict):
|
69 |
-
return result.get("url", None)
|
70 |
-
else:
|
71 |
-
return result
|
72 |
-
|
73 |
-
def update_random_seed():
|
74 |
-
"""
|
75 |
-
외부 API의 /update_random_seed 엔드포인트를 호출하여 새로운 랜덤 시드 값을 가져온다.
|
76 |
-
"""
|
77 |
-
from gradio_client import Client
|
78 |
-
client = Client(os.getenv("CLIENT_API"))
|
79 |
-
return client.predict(api_name="/update_random_seed")
|
80 |
-
|
81 |
-
# 3D 생성 함수를 위한 래퍼 (GPU 데코레이터 적용)
|
82 |
-
@spaces.GPU(duration=120) # 3D 생성은 더 많은 시간 필요
|
83 |
-
def generate_3d_wrapper(*args, **kwargs):
|
84 |
-
"""3D 생성 함수를 GPU 환경에서 실행"""
|
85 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
86 |
-
# 실제 3D 생성 로직이 여기서 실행됨
|
87 |
-
# model_zoo의 함수들이 여기서 호출될 것임
|
88 |
-
return model_zoo.generate_3d(*args, **kwargs)
|
89 |
-
|
90 |
-
_TITLE = '''✨ 3D LLAMA Studio'''
|
91 |
-
_DESCRIPTION = '''
|
92 |
-
### Welcome to 3D Llama Studio - Your Advanced 3D Generation Platform
|
93 |
-
|
94 |
-
This platform offers two powerful features:
|
95 |
-
1. **Text/Image to 3D**: Generate detailed 3D models from text descriptions or reference images
|
96 |
-
2. **Text to Styled Image**: Create artistic images that can be used for 3D generation
|
97 |
-
|
98 |
-
*Note: Both English and Korean prompts are supported (영어와 한글 프롬프트 모두 지원됩니다)*
|
99 |
-
|
100 |
-
**Running on ZeroGPU** 🚀
|
101 |
-
'''
|
102 |
-
|
103 |
-
# CSS 스타일 밝은 테마로 수정
|
104 |
-
custom_css = """
|
105 |
-
.gradio-container {
|
106 |
-
background-color: #ffffff;
|
107 |
-
color: #333333;
|
108 |
-
}
|
109 |
-
.tabs {
|
110 |
-
background-color: #f8f9fa;
|
111 |
-
border-radius: 10px;
|
112 |
-
padding: 10px;
|
113 |
-
margin: 10px 0;
|
114 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
115 |
-
}
|
116 |
-
.input-box {
|
117 |
-
background-color: #ffffff;
|
118 |
-
border: 1px solid #e0e0e0;
|
119 |
-
border-radius: 8px;
|
120 |
-
padding: 15px;
|
121 |
-
margin: 10px 0;
|
122 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
|
123 |
-
}
|
124 |
-
.button-primary {
|
125 |
-
background-color: #4a90e2 !important;
|
126 |
-
border: none !important;
|
127 |
-
color: white !important;
|
128 |
-
transition: all 0.3s ease;
|
129 |
-
}
|
130 |
-
.button-primary:hover {
|
131 |
-
background-color: #357abd !important;
|
132 |
-
transform: translateY(-1px);
|
133 |
-
}
|
134 |
-
.button-secondary {
|
135 |
-
background-color: #f0f0f0 !important;
|
136 |
-
border: 1px solid #e0e0e0 !important;
|
137 |
-
color: #333333 !important;
|
138 |
-
transition: all 0.3s ease;
|
139 |
-
}
|
140 |
-
.button-secondary:hover {
|
141 |
-
background-color: #e0e0e0 !important;
|
142 |
-
}
|
143 |
-
.main-title {
|
144 |
-
color: #2c3e50;
|
145 |
-
font-weight: bold;
|
146 |
-
margin-bottom: 20px;
|
147 |
-
}
|
148 |
-
.slider-label {
|
149 |
-
color: #2c3e50;
|
150 |
-
font-weight: 500;
|
151 |
-
}
|
152 |
-
.textbox-input {
|
153 |
-
border: 1px solid #e0e0e0 !important;
|
154 |
-
background-color: #ffffff !important;
|
155 |
-
}
|
156 |
-
.zerogpu-badge {
|
157 |
-
background-color: #4CAF50;
|
158 |
-
color: white;
|
159 |
-
padding: 5px 10px;
|
160 |
-
border-radius: 5px;
|
161 |
-
font-size: 14px;
|
162 |
-
margin-left: 10px;
|
163 |
-
}
|
164 |
-
"""
|
165 |
-
|
166 |
-
# Gradio 테마 설정 수정
|
167 |
-
def launch():
|
168 |
-
# CPU 모드로 모델 초기화
|
169 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
170 |
-
model_zoo.init_models()
|
171 |
-
|
172 |
-
with gr.Blocks(
|
173 |
-
title=_TITLE,
|
174 |
-
css=custom_css,
|
175 |
-
theme=gr.themes.Soft(
|
176 |
-
primary_hue="blue",
|
177 |
-
secondary_hue="slate",
|
178 |
-
neutral_hue="slate",
|
179 |
-
font=["Inter", "Arial", "sans-serif"]
|
180 |
-
)
|
181 |
-
) as demo:
|
182 |
-
|
183 |
-
with gr.Row():
|
184 |
-
with gr.Column():
|
185 |
-
gr.Markdown('# ' + _TITLE, elem_classes="main-title")
|
186 |
-
with gr.Column():
|
187 |
-
gr.HTML('<span class="zerogpu-badge">ZeroGPU Enabled</span>')
|
188 |
-
gr.Markdown(_DESCRIPTION)
|
189 |
-
|
190 |
-
with gr.Tabs() as tabs:
|
191 |
-
with gr.Tab("🎨 Text to Styled Image", elem_classes="tab"):
|
192 |
-
with gr.Group(elem_classes="input-box"):
|
193 |
-
gr.Markdown("### Image Generation Settings")
|
194 |
-
with gr.Row():
|
195 |
-
with gr.Column():
|
196 |
-
height_slider = gr.Slider(
|
197 |
-
label="Image Height",
|
198 |
-
minimum=256,
|
199 |
-
maximum=2048,
|
200 |
-
step=64,
|
201 |
-
value=1024,
|
202 |
-
info="Select image height (pixels)"
|
203 |
-
)
|
204 |
-
width_slider = gr.Slider(
|
205 |
-
label="Image Width",
|
206 |
-
minimum=256,
|
207 |
-
maximum=2048,
|
208 |
-
step=64,
|
209 |
-
value=1024,
|
210 |
-
info="Select image width (pixels)"
|
211 |
-
)
|
212 |
-
with gr.Column():
|
213 |
-
steps_slider = gr.Slider(
|
214 |
-
label="Generation Steps",
|
215 |
-
minimum=1,
|
216 |
-
maximum=100,
|
217 |
-
step=1,
|
218 |
-
value=8,
|
219 |
-
info="More steps = higher quality but slower"
|
220 |
-
)
|
221 |
-
scales_slider = gr.Slider(
|
222 |
-
label="Guidance Scale",
|
223 |
-
minimum=1.0,
|
224 |
-
maximum=10.0,
|
225 |
-
step=0.1,
|
226 |
-
value=3.5,
|
227 |
-
info="How closely to follow the prompt"
|
228 |
-
)
|
229 |
-
|
230 |
-
prompt_text = gr.Textbox(
|
231 |
-
label="Image Description",
|
232 |
-
placeholder="Enter your prompt here (English or Korean)",
|
233 |
-
lines=3,
|
234 |
-
elem_classes="input-box"
|
235 |
-
)
|
236 |
-
|
237 |
-
with gr.Row():
|
238 |
-
seed_number = gr.Number(
|
239 |
-
label="Seed (Empty = Random)",
|
240 |
-
value=None,
|
241 |
-
elem_classes="input-box"
|
242 |
-
)
|
243 |
-
update_seed_button = gr.Button(
|
244 |
-
"🎲 Random Seed",
|
245 |
-
elem_classes="button-secondary"
|
246 |
-
)
|
247 |
-
|
248 |
-
generate_button = gr.Button(
|
249 |
-
"🚀 Generate Image",
|
250 |
-
elem_classes="button-primary"
|
251 |
-
)
|
252 |
-
|
253 |
-
with gr.Group(elem_classes="input-box"):
|
254 |
-
gr.Markdown("### Generated Result")
|
255 |
-
image_output = gr.Image(label="Output Image")
|
256 |
-
|
257 |
-
update_seed_button.click(
|
258 |
-
fn=update_random_seed,
|
259 |
-
inputs=[],
|
260 |
-
outputs=seed_number
|
261 |
-
)
|
262 |
-
|
263 |
-
generate_button.click(
|
264 |
-
fn=text_to_image,
|
265 |
-
inputs=[height_slider, width_slider, steps_slider, scales_slider, prompt_text, seed_number],
|
266 |
-
outputs=image_output
|
267 |
-
)
|
268 |
-
|
269 |
-
with gr.Tab("🎯 Image to 3D", elem_classes="tab"):
|
270 |
-
create_3d_ui("wkl")
|
271 |
-
|
272 |
-
demo.queue().launch(share=True)
|
273 |
-
|
274 |
-
if __name__ == '__main__':
|
275 |
-
fire.Fire(launch)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|