Spaces:
Running
on
Zero
Running
on
Zero
完成简单版本的逻辑
Browse files
app.py
CHANGED
@@ -8,9 +8,11 @@ from pathlib import Path
|
|
8 |
import uuid
|
9 |
import argparse
|
10 |
import torch
|
|
|
|
|
|
|
11 |
import trimesh
|
12 |
|
13 |
-
|
14 |
parser = argparse.ArgumentParser()
|
15 |
parser.add_argument("--model_path", type=str, default='tencent/Hunyuan3D-2mini')
|
16 |
parser.add_argument("--subfolder", type=str, default='hunyuan3d-dit-v2-mini-turbo')
|
@@ -178,39 +180,49 @@ def gen_shape(
|
|
178 |
|
179 |
path = export_mesh(mesh, save_folder, textured=False)
|
180 |
|
181 |
-
model_viewer_html = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH)
|
182 |
|
183 |
-
return model_viewer_html, path
|
184 |
|
185 |
-
|
186 |
-
|
187 |
|
188 |
-
|
189 |
-
|
190 |
|
191 |
-
#
|
192 |
-
|
193 |
-
|
194 |
|
195 |
-
|
196 |
-
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
|
204 |
-
|
205 |
-
|
|
|
|
|
206 |
|
207 |
-
#
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
|
216 |
|
@@ -262,7 +274,8 @@ with gr.Blocks().queue() as demo:
|
|
262 |
with gr.Column(scale=6):
|
263 |
gr.Markdown("#### Generated Mesh")
|
264 |
html_export_mesh = gr.HTML(HTML_OUTPUT_PLACEHOLDER, label='Output')
|
265 |
-
|
|
|
266 |
|
267 |
with gr.Column(scale=3):
|
268 |
gr.Markdown("#### Image Examples")
|
@@ -272,7 +285,21 @@ with gr.Blocks().queue() as demo:
|
|
272 |
gen_button.click(
|
273 |
fn=gen_shape,
|
274 |
inputs=[image,num_steps,cfg_scale,seed,octree_resolution,num_chunks,target_face_num, randomize_seed],
|
275 |
-
outputs=[html_export_mesh,
|
276 |
)
|
277 |
|
278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import uuid
|
9 |
import argparse
|
10 |
import torch
|
11 |
+
import uvicorn
|
12 |
+
from fastapi import FastAPI
|
13 |
+
from fastapi.staticfiles import StaticFiles
|
14 |
import trimesh
|
15 |
|
|
|
16 |
parser = argparse.ArgumentParser()
|
17 |
parser.add_argument("--model_path", type=str, default='tencent/Hunyuan3D-2mini')
|
18 |
parser.add_argument("--subfolder", type=str, default='hunyuan3d-dit-v2-mini-turbo')
|
|
|
180 |
|
181 |
path = export_mesh(mesh, save_folder, textured=False)
|
182 |
|
183 |
+
# model_viewer_html = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH)
|
184 |
|
185 |
+
# return model_viewer_html, path
|
186 |
|
187 |
+
if args.low_vram_mode:
|
188 |
+
torch.cuda.empty_cache()
|
189 |
|
190 |
+
if path is None:
|
191 |
+
raise gr.Error('Please generate a mesh first.')
|
192 |
|
193 |
+
# 简化模型
|
194 |
+
print(f'exporting {path}')
|
195 |
+
print(f'reduce face to {target_face_num}')
|
196 |
|
197 |
+
mesh = trimesh.load(path)
|
198 |
+
progress(0.5,desc="Optimizing mesh")
|
199 |
|
200 |
+
mesh = floater_remove_worker(mesh)
|
201 |
+
mesh = degenerate_face_remove_worker(mesh)
|
202 |
+
progress(0.6,desc="Reducing mesh faces")
|
203 |
+
mesh = face_reduce_worker(mesh, target_face_num)
|
204 |
+
save_folder = gen_save_folder()
|
205 |
|
206 |
+
file_type = "obj"
|
207 |
+
objPath = export_mesh(mesh, save_folder, textured=False, type=file_type)
|
208 |
+
rel_objPath = os.path.relpath(objPath, SAVE_DIR)
|
209 |
+
objPath = "/static/"+rel_objPath
|
210 |
|
211 |
+
# for preview
|
212 |
+
save_folder = gen_save_folder()
|
213 |
+
_ = export_mesh(mesh, save_folder, textured=False)
|
214 |
+
model_viewer_html = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH, textured=False)
|
215 |
|
216 |
+
|
217 |
+
glbPath = os.path.join(save_folder, f'white_mesh.glb')
|
218 |
+
rel_glbPath = os.path.relpath(glbPath, SAVE_DIR)
|
219 |
+
glbPath = "/static/"+rel_glbPath
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
|
224 |
+
progress(1,desc="Complete")
|
225 |
+
return model_viewer_html, glbPath, objPath
|
226 |
|
227 |
|
228 |
|
|
|
274 |
with gr.Column(scale=6):
|
275 |
gr.Markdown("#### Generated Mesh")
|
276 |
html_export_mesh = gr.HTML(HTML_OUTPUT_PLACEHOLDER, label='Output')
|
277 |
+
objPath_output = gr.Textbox(label="Obj Path")
|
278 |
+
glbPath_output = gr.Textbox(label="Glb Path")
|
279 |
|
280 |
with gr.Column(scale=3):
|
281 |
gr.Markdown("#### Image Examples")
|
|
|
285 |
gen_button.click(
|
286 |
fn=gen_shape,
|
287 |
inputs=[image,num_steps,cfg_scale,seed,octree_resolution,num_chunks,target_face_num, randomize_seed],
|
288 |
+
outputs=[html_export_mesh, glbPath_output, objPath_output]
|
289 |
)
|
290 |
|
291 |
+
# https://discuss.huggingface.co/t/how-to-serve-an-html-file/33921/2
|
292 |
+
# create a FastAPI app
|
293 |
+
app = FastAPI()
|
294 |
+
# create a static directory to store the static files
|
295 |
+
static_dir = Path(SAVE_DIR).absolute()
|
296 |
+
static_dir.mkdir(parents=True, exist_ok=True)
|
297 |
+
app.mount("/static", StaticFiles(directory=static_dir, html=True), name="static")
|
298 |
+
shutil.copytree('./assets/env_maps', os.path.join(static_dir, 'env_maps'), dirs_exist_ok=True)
|
299 |
+
|
300 |
+
if args.low_vram_mode:
|
301 |
+
torch.cuda.empty_cache()
|
302 |
+
|
303 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
304 |
+
# demo.launch()
|
305 |
+
uvicorn.run(app, host=args.host, port=args.port)
|