frogleo commited on
Commit
27fc85d
·
1 Parent(s): b613119

着手全新的UI界面和交互

Browse files
Files changed (3) hide show
  1. README.md +6 -7
  2. app.py +9 -0
  3. gradio_app.py +0 -751
README.md CHANGED
@@ -1,12 +1,11 @@
1
  ---
2
- title: Hunyuan3D 2mini Turbo
3
- emoji: 🔥
4
- colorFrom: purple
5
  colorTo: red
6
  sdk: gradio
7
  sdk_version: 4.44.1
8
- app_file: gradio_app.py
9
  pinned: false
10
- short_description: Fast Images-to-3D Generation within 1 Second
11
- ---
12
-
 
1
  ---
2
+ title: Image to 3D
3
+ emoji: 🖼️➡️📦
4
+ colorFrom: yellow
5
  colorTo: red
6
  sdk: gradio
7
  sdk_version: 4.44.1
8
+ app_file: app.py
9
  pinned: false
10
+ short_description: A lightweight image to 3D converter
11
+ ---
 
app.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ title = "## Image to 3D"
4
+ description = "A lightweight image to 3D converter"
5
+
6
+
7
+ with gr.Blocks().queue() as demo:
8
+ gr.Markdown(title)
9
+ gr.Markdown(description)
gradio_app.py DELETED
@@ -1,751 +0,0 @@
1
- # Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
2
- # except for the third-party components listed below.
3
- # Hunyuan 3D does not impose any additional limitations beyond what is outlined
4
- # in the repsective licenses of these third-party components.
5
- # Users must comply with all terms and conditions of original licenses of these third-party
6
- # components and must ensure that the usage of the third party components adheres to
7
- # all relevant laws and regulations.
8
-
9
- # For avoidance of doubts, Hunyuan 3D means the large language models and
10
- # their software and algorithms, including trained model weights, parameters (including
11
- # optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
12
- # fine-tuning enabling code and other elements of the foregoing made publicly available
13
- # by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
14
-
15
- import os
16
- import subprocess
17
- import random
18
- import shutil
19
- import time
20
- from glob import glob
21
- from pathlib import Path
22
-
23
- import gradio as gr
24
- import torch
25
- import trimesh
26
- import uvicorn
27
- from fastapi import FastAPI
28
- from fastapi.staticfiles import StaticFiles
29
- import uuid
30
-
31
-
32
- from hy3dgen.shapegen.utils import logger
33
-
34
- MAX_SEED = 1e7
35
-
36
- if True:
37
- import os
38
- import spaces
39
- import subprocess
40
- import sys
41
- import shlex
42
- print("cd /home/user/app/hy3dgen/texgen/differentiable_renderer/ && bash compile_mesh_painter.sh")
43
- os.system("cd /home/user/app/hy3dgen/texgen/differentiable_renderer/ && bash compile_mesh_painter.sh")
44
- print('install custom')
45
- subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"), check=True)
46
-
47
-
48
- def get_example_img_list():
49
- print('Loading example img list ...')
50
- return sorted(glob('./assets/example_images/**/*.png', recursive=True))
51
-
52
-
53
- def get_example_txt_list():
54
- print('Loading example txt list ...')
55
- txt_list = list()
56
- for line in open('./assets/example_prompts.txt', encoding='utf-8'):
57
- txt_list.append(line.strip())
58
- return txt_list
59
-
60
-
61
- def gen_save_folder(max_size=200):
62
- os.makedirs(SAVE_DIR, exist_ok=True)
63
-
64
- # 获取所有文件夹路径
65
- dirs = [f for f in Path(SAVE_DIR).iterdir() if f.is_dir()]
66
-
67
- # 如果文件夹数量超过 max_size,删除创建时间最久的文件夹
68
- if len(dirs) >= max_size:
69
- # 按创建时间排序,最久的排在前面
70
- oldest_dir = min(dirs, key=lambda x: x.stat().st_ctime)
71
- shutil.rmtree(oldest_dir)
72
- print(f"Removed the oldest folder: {oldest_dir}")
73
-
74
- # 生成一个新的 uuid 文件夹名称
75
- new_folder = os.path.join(SAVE_DIR, str(uuid.uuid4()))
76
- os.makedirs(new_folder, exist_ok=True)
77
- print(f"Created new folder: {new_folder}")
78
-
79
- return new_folder
80
-
81
-
82
- def export_mesh(mesh, save_folder, textured=False, type='glb'):
83
- if textured:
84
- path = os.path.join(save_folder, f'textured_mesh.{type}')
85
- else:
86
- path = os.path.join(save_folder, f'white_mesh.{type}')
87
- if type not in ['glb', 'obj']:
88
- mesh.export(path)
89
- else:
90
- mesh.export(path, include_normals=textured)
91
- return path
92
-
93
-
94
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
95
- if randomize_seed:
96
- seed = random.randint(0, MAX_SEED)
97
- return seed
98
-
99
-
100
- def build_model_viewer_html(save_folder, height=660, width=790, textured=False):
101
- # Remove first folder from path to make relative path
102
- if textured:
103
- related_path = f"./textured_mesh.glb"
104
- template_name = './assets/modelviewer-textured-template.html'
105
- output_html_path = os.path.join(save_folder, f'textured_mesh.html')
106
- else:
107
- related_path = f"./white_mesh.glb"
108
- template_name = './assets/modelviewer-template.html'
109
- output_html_path = os.path.join(save_folder, f'white_mesh.html')
110
- offset = 50 if textured else 10
111
- with open(os.path.join(CURRENT_DIR, template_name), 'r', encoding='utf-8') as f:
112
- template_html = f.read()
113
-
114
- with open(output_html_path, 'w', encoding='utf-8') as f:
115
- template_html = template_html.replace('#height#', f'{height - offset}')
116
- template_html = template_html.replace('#width#', f'{width}')
117
- template_html = template_html.replace('#src#', f'{related_path}/')
118
- f.write(template_html)
119
-
120
- rel_path = os.path.relpath(output_html_path, SAVE_DIR)
121
- iframe_tag = f'<iframe src="/static/{rel_path}" height="{height}" width="100%" frameborder="0"></iframe>'
122
- print(
123
- f'Find html file {output_html_path}, {os.path.exists(output_html_path)}, relative HTML path is /static/{rel_path}')
124
-
125
- return f"""
126
- <div style='height: {height}; width: 100%;'>
127
- {iframe_tag}
128
- </div>
129
- """
130
-
131
- @spaces.GPU(duration=60)
132
- def _gen_shape(
133
- caption=None,
134
- image=None,
135
- mv_image_front=None,
136
- mv_image_back=None,
137
- mv_image_left=None,
138
- mv_image_right=None,
139
- steps=50,
140
- guidance_scale=7.5,
141
- seed=1234,
142
- octree_resolution=256,
143
- check_box_rembg=False,
144
- num_chunks=200000,
145
- randomize_seed: bool = False,
146
- ):
147
- if not MV_MODE and image is None and caption is None:
148
- raise gr.Error("Please provide either a caption or an image.")
149
- if MV_MODE:
150
- if mv_image_front is None and mv_image_back is None and mv_image_left is None and mv_image_right is None:
151
- raise gr.Error("Please provide at least one view image.")
152
- image = {}
153
- if mv_image_front:
154
- image['front'] = mv_image_front
155
- if mv_image_back:
156
- image['back'] = mv_image_back
157
- if mv_image_left:
158
- image['left'] = mv_image_left
159
- if mv_image_right:
160
- image['right'] = mv_image_right
161
-
162
- seed = int(randomize_seed_fn(seed, randomize_seed))
163
-
164
- octree_resolution = int(octree_resolution)
165
- if caption: print('prompt is', caption)
166
- save_folder = gen_save_folder()
167
- stats = {
168
- 'model': {
169
- 'shapegen': f'{args.model_path}/{args.subfolder}',
170
- 'texgen': f'{args.texgen_model_path}',
171
- },
172
- 'params': {
173
- 'caption': caption,
174
- 'steps': steps,
175
- 'guidance_scale': guidance_scale,
176
- 'seed': seed,
177
- 'octree_resolution': octree_resolution,
178
- 'check_box_rembg': check_box_rembg,
179
- 'num_chunks': num_chunks,
180
- }
181
- }
182
- time_meta = {}
183
-
184
- if image is None:
185
- start_time = time.time()
186
- try:
187
- image = t2i_worker(caption)
188
- except Exception as e:
189
- raise gr.Error(f"Text to 3D is disable. Please enable it by `python gradio_app.py --enable_t23d`.")
190
- time_meta['text2image'] = time.time() - start_time
191
-
192
- # remove disk io to make responding faster, uncomment at your will.
193
- # image.save(os.path.join(save_folder, 'input.png'))
194
- if MV_MODE:
195
- start_time = time.time()
196
- for k, v in image.items():
197
- if check_box_rembg or v.mode == "RGB":
198
- img = rmbg_worker(v.convert('RGB'))
199
- image[k] = img
200
- time_meta['remove background'] = time.time() - start_time
201
- else:
202
- if check_box_rembg or image.mode == "RGB":
203
- start_time = time.time()
204
- image = rmbg_worker(image.convert('RGB'))
205
- time_meta['remove background'] = time.time() - start_time
206
-
207
- # remove disk io to make responding faster, uncomment at your will.
208
- # image.save(os.path.join(save_folder, 'rembg.png'))
209
-
210
- # image to white model
211
- start_time = time.time()
212
-
213
- generator = torch.Generator()
214
- generator = generator.manual_seed(int(seed))
215
- outputs = i23d_worker(
216
- image=image,
217
- num_inference_steps=steps,
218
- guidance_scale=guidance_scale,
219
- generator=generator,
220
- octree_resolution=octree_resolution,
221
- num_chunks=num_chunks,
222
- output_type='mesh'
223
- )
224
- time_meta['shape generation'] = time.time() - start_time
225
- logger.info("---Shape generation takes %s seconds ---" % (time.time() - start_time))
226
-
227
- tmp_start = time.time()
228
- mesh = export_to_trimesh(outputs)[0]
229
- time_meta['export to trimesh'] = time.time() - tmp_start
230
-
231
- stats['number_of_faces'] = mesh.faces.shape[0]
232
- stats['number_of_vertices'] = mesh.vertices.shape[0]
233
-
234
- stats['time'] = time_meta
235
- main_image = image if not MV_MODE else image['front']
236
- return mesh, main_image, save_folder, stats, seed
237
-
238
- @spaces.GPU(duration=60)
239
- def generation_all(
240
- caption=None,
241
- image=None,
242
- mv_image_front=None,
243
- mv_image_back=None,
244
- mv_image_left=None,
245
- mv_image_right=None,
246
- steps=50,
247
- guidance_scale=7.5,
248
- seed=1234,
249
- octree_resolution=256,
250
- check_box_rembg=False,
251
- num_chunks=200000,
252
- randomize_seed: bool = False,
253
- ):
254
- start_time_0 = time.time()
255
- mesh, image, save_folder, stats, seed = _gen_shape(
256
- caption,
257
- image,
258
- mv_image_front=mv_image_front,
259
- mv_image_back=mv_image_back,
260
- mv_image_left=mv_image_left,
261
- mv_image_right=mv_image_right,
262
- steps=steps,
263
- guidance_scale=guidance_scale,
264
- seed=seed,
265
- octree_resolution=octree_resolution,
266
- check_box_rembg=check_box_rembg,
267
- num_chunks=num_chunks,
268
- randomize_seed=randomize_seed,
269
- )
270
- path = export_mesh(mesh, save_folder, textured=False)
271
-
272
- # tmp_time = time.time()
273
- # mesh = floater_remove_worker(mesh)
274
- # mesh = degenerate_face_remove_worker(mesh)
275
- # logger.info("---Postprocessing takes %s seconds ---" % (time.time() - tmp_time))
276
- # stats['time']['postprocessing'] = time.time() - tmp_time
277
-
278
- tmp_time = time.time()
279
- mesh = face_reduce_worker(mesh)
280
- logger.info("---Face Reduction takes %s seconds ---" % (time.time() - tmp_time))
281
- stats['time']['face reduction'] = time.time() - tmp_time
282
-
283
- tmp_time = time.time()
284
- textured_mesh = texgen_worker(mesh, image)
285
- logger.info("---Texture Generation takes %s seconds ---" % (time.time() - tmp_time))
286
- stats['time']['texture generation'] = time.time() - tmp_time
287
- stats['time']['total'] = time.time() - start_time_0
288
-
289
- textured_mesh.metadata['extras'] = stats
290
- path_textured = export_mesh(textured_mesh, save_folder, textured=True)
291
- model_viewer_html_textured = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH,
292
- textured=True)
293
- if args.low_vram_mode:
294
- torch.cuda.empty_cache()
295
- return (
296
- gr.update(value=path),
297
- gr.update(value=path_textured),
298
- model_viewer_html_textured,
299
- stats,
300
- seed,
301
- )
302
-
303
- @spaces.GPU(duration=60)
304
- def shape_generation(
305
- caption=None,
306
- image=None,
307
- mv_image_front=None,
308
- mv_image_back=None,
309
- mv_image_left=None,
310
- mv_image_right=None,
311
- steps=50,
312
- guidance_scale=7.5,
313
- seed=1234,
314
- octree_resolution=256,
315
- check_box_rembg=False,
316
- num_chunks=200000,
317
- randomize_seed: bool = False,
318
- ):
319
- start_time_0 = time.time()
320
- mesh, image, save_folder, stats, seed = _gen_shape(
321
- caption,
322
- image,
323
- mv_image_front=mv_image_front,
324
- mv_image_back=mv_image_back,
325
- mv_image_left=mv_image_left,
326
- mv_image_right=mv_image_right,
327
- steps=steps,
328
- guidance_scale=guidance_scale,
329
- seed=seed,
330
- octree_resolution=octree_resolution,
331
- check_box_rembg=check_box_rembg,
332
- num_chunks=num_chunks,
333
- randomize_seed=randomize_seed,
334
- )
335
- stats['time']['total'] = time.time() - start_time_0
336
- mesh.metadata['extras'] = stats
337
-
338
- path = export_mesh(mesh, save_folder, textured=False)
339
- model_viewer_html = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH)
340
- if args.low_vram_mode:
341
- torch.cuda.empty_cache()
342
- return (
343
- gr.update(value=path),
344
- model_viewer_html,
345
- stats,
346
- seed,
347
- )
348
-
349
-
350
- def build_app():
351
- title = 'Hunyuan3D-2: High Resolution Textured 3D Assets Generation'
352
- if MV_MODE:
353
- title = 'Hunyuan3D-2mv: Image to 3D Generation with 1-4 Views'
354
- if 'mini' in args.subfolder:
355
- title = 'Hunyuan3D-2mini: Strong 0.6B Image to Shape Generator'
356
- if TURBO_MODE:
357
- title = title.replace(':', '-Turbo: Fast ')
358
-
359
- title_html = f"""
360
- <div style="font-size: 2em; font-weight: bold; text-align: center; margin-bottom: 5px">
361
-
362
- {title}
363
- </div>
364
- <div align="center">
365
- Tencent Hunyuan3D Team
366
- </div>
367
- <div align="center">
368
- <a href="https://github.com/tencent/FlashVDM">Github</a> &ensp;
369
- <a href="https://3d.hunyuan.tencent.com">Hunyuan3D Studio</a> &ensp;
370
- <a href="https://arxiv.org/abs/2503.16302">Technical Report</a> &ensp;
371
- <a href="https://huggingface.co/tencent/Hunyuan3D-2mini/tree/main/hunyuan3d-dit-v2-mini-turbo"> Pretrained Models</a> &ensp;
372
- </div>
373
- """
374
- custom_css = """
375
- .app.svelte-wpkpf6.svelte-wpkpf6:not(.fill_width) {
376
- max-width: 1480px;
377
- }
378
- .mv-image button .wrap {
379
- font-size: 10px;
380
- }
381
-
382
- .mv-image .icon-wrap {
383
- width: 20px;
384
- }
385
-
386
- """
387
-
388
- with gr.Blocks(theme=gr.themes.Base(), title='Hunyuan-3D-2.0', analytics_enabled=False, css=custom_css) as demo:
389
- gr.HTML(title_html)
390
-
391
- with gr.Row():
392
- with gr.Column(scale=3):
393
- with gr.Tabs(selected='tab_img_prompt') as tabs_prompt:
394
- with gr.Tab('Image Prompt', id='tab_img_prompt', visible=not MV_MODE) as tab_ip:
395
- image = gr.Image(label='Image', type='pil', image_mode='RGBA', height=290)
396
-
397
- with gr.Tab('Text Prompt', id='tab_txt_prompt', visible=HAS_T2I and not MV_MODE) as tab_tp:
398
- caption = gr.Textbox(label='Text Prompt',
399
- placeholder='HunyuanDiT will be used to generate image.',
400
- info='Example: A 3D model of a cute cat, white background')
401
- with gr.Tab('MultiView Prompt', visible=MV_MODE) as tab_mv:
402
- # gr.Label('Please upload at least one front image.')
403
- with gr.Row():
404
- mv_image_front = gr.Image(label='Front', type='pil', image_mode='RGBA', height=140,
405
- min_width=100, elem_classes='mv-image')
406
- mv_image_back = gr.Image(label='Back', type='pil', image_mode='RGBA', height=140,
407
- min_width=100, elem_classes='mv-image')
408
- with gr.Row():
409
- mv_image_left = gr.Image(label='Left', type='pil', image_mode='RGBA', height=140,
410
- min_width=100, elem_classes='mv-image')
411
- mv_image_right = gr.Image(label='Right', type='pil', image_mode='RGBA', height=140,
412
- min_width=100, elem_classes='mv-image')
413
-
414
- with gr.Row():
415
- btn = gr.Button(value='Gen Shape', variant='primary', min_width=100)
416
- btn_all = gr.Button(value='Gen Textured Shape',
417
- variant='primary',
418
- visible=HAS_TEXTUREGEN,
419
- min_width=100)
420
-
421
- with gr.Group():
422
- file_out = gr.File(label="File", visible=False)
423
- file_out2 = gr.File(label="File", visible=False)
424
-
425
- with gr.Tabs(selected='tab_options' if TURBO_MODE else 'tab_export'):
426
- with gr.Tab("Options", id='tab_options', visible=TURBO_MODE):
427
- gen_mode = gr.Radio(label='Generation Mode',
428
- info='Recommendation: Turbo for most cases, Fast for very complex cases, Standard seldom use.',
429
- choices=['Turbo', 'Fast', 'Standard'], value='Turbo')
430
- decode_mode = gr.Radio(label='Decoding Mode',
431
- info='The resolution for exporting mesh from generated vectset',
432
- choices=['Low', 'Standard', 'High'],
433
- value='Standard')
434
- with gr.Tab('Advanced Options', id='tab_advanced_options'):
435
- with gr.Row():
436
- check_box_rembg = gr.Checkbox(value=True, label='Remove Background', min_width=100)
437
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True, min_width=100)
438
- seed = gr.Slider(
439
- label="Seed",
440
- minimum=0,
441
- maximum=MAX_SEED,
442
- step=1,
443
- value=1234,
444
- min_width=100,
445
- )
446
- with gr.Row():
447
- num_steps = gr.Slider(maximum=100,
448
- minimum=1,
449
- value=5 if 'turbo' in args.subfolder else 30,
450
- step=1, label='Inference Steps')
451
- octree_resolution = gr.Slider(maximum=512, minimum=16, value=256, label='Octree Resolution')
452
- with gr.Row():
453
- cfg_scale = gr.Number(value=5.0, label='Guidance Scale', min_width=100)
454
- num_chunks = gr.Slider(maximum=5000000, minimum=1000, value=8000,
455
- label='Number of Chunks', min_width=100)
456
- with gr.Tab("Export", id='tab_export'):
457
- with gr.Row():
458
- file_type = gr.Dropdown(label='File Type', choices=SUPPORTED_FORMATS,
459
- value='glb', min_width=100)
460
- reduce_face = gr.Checkbox(label='Simplify Mesh', value=False, min_width=100)
461
- export_texture = gr.Checkbox(label='Include Texture', value=False,
462
- visible=False, min_width=100)
463
- target_face_num = gr.Slider(maximum=1000000, minimum=100, value=10000,
464
- label='Target Face Number')
465
- with gr.Row():
466
- confirm_export = gr.Button(value="Transform", min_width=100)
467
- file_export = gr.DownloadButton(label="Download", variant='primary',
468
- interactive=False, min_width=100)
469
-
470
- with gr.Column(scale=6):
471
- with gr.Tabs(selected='gen_mesh_panel') as tabs_output:
472
- with gr.Tab('Generated Mesh', id='gen_mesh_panel'):
473
- html_gen_mesh = gr.HTML(HTML_OUTPUT_PLACEHOLDER, label='Output')
474
- with gr.Tab('Exporting Mesh', id='export_mesh_panel'):
475
- html_export_mesh = gr.HTML(HTML_OUTPUT_PLACEHOLDER, label='Output')
476
- with gr.Tab('Mesh Statistic', id='stats_panel'):
477
- stats = gr.Json({}, label='Mesh Stats')
478
-
479
- with gr.Column(scale=3 if MV_MODE else 2):
480
- with gr.Tabs(selected='tab_img_gallery') as gallery:
481
- with gr.Tab('Image to 3D Gallery', id='tab_img_gallery', visible=not MV_MODE) as tab_gi:
482
- with gr.Row():
483
- gr.Examples(examples=example_is, inputs=[image],
484
- label=None, examples_per_page=18)
485
-
486
- with gr.Tab('Text to 3D Gallery', id='tab_txt_gallery', visible=HAS_T2I and not MV_MODE) as tab_gt:
487
- with gr.Row():
488
- gr.Examples(examples=example_ts, inputs=[caption],
489
- label=None, examples_per_page=18)
490
-
491
-
492
- gr.HTML(f"""
493
- <div align="center">
494
- Activated Model - Shape Generation ({args.model_path}/{args.subfolder}) ; Texture Generation ({'Hunyuan3D-2' if HAS_TEXTUREGEN else 'Unavailable'})
495
- </div>
496
- """)
497
- if not HAS_TEXTUREGEN:
498
- gr.HTML("""
499
- <div style="margin-top: 5px;" align="center">
500
- <b>Warning: </b>
501
- Texture synthesis is disable due to missing requirements,
502
- please install requirements following <a href="https://github.com/Tencent/Hunyuan3D-2?tab=readme-ov-file#install-requirements">README.md</a>to activate it.
503
- </div>
504
- """)
505
- if not args.enable_t23d:
506
- gr.HTML("""
507
- <div style="margin-top: 5px;" align="center">
508
- <b>Warning: </b>
509
- Text to 3D is disable. To activate it, please run `python gradio_app.py --enable_t23d`.
510
- </div>
511
- """)
512
-
513
- tab_ip.select(fn=lambda: gr.update(selected='tab_img_gallery'), outputs=gallery)
514
- if HAS_T2I:
515
- tab_tp.select(fn=lambda: gr.update(selected='tab_txt_gallery'), outputs=gallery)
516
-
517
- btn.click(
518
- shape_generation,
519
- inputs=[
520
- caption,
521
- image,
522
- mv_image_front,
523
- mv_image_back,
524
- mv_image_left,
525
- mv_image_right,
526
- num_steps,
527
- cfg_scale,
528
- seed,
529
- octree_resolution,
530
- check_box_rembg,
531
- num_chunks,
532
- randomize_seed,
533
- ],
534
- outputs=[file_out, html_gen_mesh, stats, seed]
535
- ).then(
536
- lambda: (gr.update(visible=False, value=False), gr.update(interactive=True), gr.update(interactive=True),
537
- gr.update(interactive=False)),
538
- outputs=[export_texture, reduce_face, confirm_export, file_export],
539
- ).then(
540
- lambda: gr.update(selected='gen_mesh_panel'),
541
- outputs=[tabs_output],
542
- )
543
-
544
- btn_all.click(
545
- generation_all,
546
- inputs=[
547
- caption,
548
- image,
549
- mv_image_front,
550
- mv_image_back,
551
- mv_image_left,
552
- mv_image_right,
553
- num_steps,
554
- cfg_scale,
555
- seed,
556
- octree_resolution,
557
- check_box_rembg,
558
- num_chunks,
559
- randomize_seed,
560
- ],
561
- outputs=[file_out, file_out2, html_gen_mesh, stats, seed]
562
- ).then(
563
- lambda: (gr.update(visible=True, value=True), gr.update(interactive=False), gr.update(interactive=True),
564
- gr.update(interactive=False)),
565
- outputs=[export_texture, reduce_face, confirm_export, file_export],
566
- ).then(
567
- lambda: gr.update(selected='gen_mesh_panel'),
568
- outputs=[tabs_output],
569
- )
570
-
571
- def on_gen_mode_change(value):
572
- if value == 'Turbo':
573
- return gr.update(value=5)
574
- elif value == 'Fast':
575
- return gr.update(value=10)
576
- else:
577
- return gr.update(value=30)
578
-
579
- gen_mode.change(on_gen_mode_change, inputs=[gen_mode], outputs=[num_steps])
580
-
581
- def on_decode_mode_change(value):
582
- if value == 'Low':
583
- return gr.update(value=196)
584
- elif value == 'Standard':
585
- return gr.update(value=256)
586
- else:
587
- return gr.update(value=384)
588
-
589
- decode_mode.change(on_decode_mode_change, inputs=[decode_mode], outputs=[octree_resolution])
590
-
591
- def on_export_click(file_out, file_out2, file_type, reduce_face, export_texture, target_face_num):
592
- if file_out is None:
593
- raise gr.Error('Please generate a mesh first.')
594
-
595
- print(f'exporting {file_out}')
596
- print(f'reduce face to {target_face_num}')
597
- if export_texture:
598
- mesh = trimesh.load(file_out2)
599
- save_folder = gen_save_folder()
600
- path = export_mesh(mesh, save_folder, textured=True, type=file_type)
601
-
602
- # for preview
603
- save_folder = gen_save_folder()
604
- _ = export_mesh(mesh, save_folder, textured=True)
605
- model_viewer_html = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH,
606
- textured=True)
607
- else:
608
- mesh = trimesh.load(file_out)
609
- mesh = floater_remove_worker(mesh)
610
- mesh = degenerate_face_remove_worker(mesh)
611
- if reduce_face:
612
- mesh = face_reduce_worker(mesh, target_face_num)
613
- save_folder = gen_save_folder()
614
- path = export_mesh(mesh, save_folder, textured=False, type=file_type)
615
-
616
- # for preview
617
- save_folder = gen_save_folder()
618
- _ = export_mesh(mesh, save_folder, textured=False)
619
- model_viewer_html = build_model_viewer_html(save_folder, height=HTML_HEIGHT, width=HTML_WIDTH,
620
- textured=False)
621
- print(f'export to {path}')
622
- return model_viewer_html, gr.update(value=path, interactive=True)
623
-
624
- confirm_export.click(
625
- lambda: gr.update(selected='export_mesh_panel'),
626
- outputs=[tabs_output],
627
- ).then(
628
- on_export_click,
629
- inputs=[file_out, file_out2, file_type, reduce_face, export_texture, target_face_num],
630
- outputs=[html_export_mesh, file_export]
631
- )
632
-
633
- return demo
634
-
635
-
636
- if __name__ == '__main__':
637
- import argparse
638
-
639
- parser = argparse.ArgumentParser()
640
- parser.add_argument("--model_path", type=str, default='tencent/Hunyuan3D-2mini')
641
- parser.add_argument("--subfolder", type=str, default='hunyuan3d-dit-v2-mini-turbo')
642
- parser.add_argument("--texgen_model_path", type=str, default='tencent/Hunyuan3D-2')
643
- parser.add_argument('--port', type=int, default=7860)
644
- parser.add_argument('--host', type=str, default='0.0.0.0')
645
- parser.add_argument('--device', type=str, default='cuda')
646
- parser.add_argument('--mc_algo', type=str, default='mc')
647
- parser.add_argument('--cache-path', type=str, default='gradio_cache')
648
- parser.add_argument('--enable_t23d', action='store_true')
649
- parser.add_argument('--disable_tex', action='store_true')
650
- parser.add_argument('--enable_flashvdm', action='store_true')
651
- parser.add_argument('--compile', action='store_true')
652
- parser.add_argument('--low_vram_mode', action='store_true')
653
- args = parser.parse_args()
654
- args.enable_flashvdm = True
655
-
656
- SAVE_DIR = args.cache_path
657
- os.makedirs(SAVE_DIR, exist_ok=True)
658
-
659
- CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
660
- MV_MODE = 'mv' in args.model_path
661
- TURBO_MODE = 'turbo' in args.subfolder
662
-
663
- HTML_HEIGHT = 690 if MV_MODE else 650
664
- HTML_WIDTH = 500
665
- HTML_OUTPUT_PLACEHOLDER = f"""
666
- <div style='height: {650}px; width: 100%; border-radius: 8px; border-color: #e5e7eb; border-style: solid; border-width: 1px; display: flex; justify-content: center; align-items: center;'>
667
- <div style='text-align: center; font-size: 16px; color: #6b7280;'>
668
- <p style="color: #8d8d8d;">Welcome to Hunyuan3D!</p>
669
- <p style="color: #8d8d8d;">No mesh here.</p>
670
- </div>
671
- </div>
672
- """
673
-
674
- INPUT_MESH_HTML = """
675
- <div style='height: 490px; width: 100%; border-radius: 8px;
676
- border-color: #e5e7eb; order-style: solid; border-width: 1px;'>
677
- </div>
678
- """
679
- example_is = get_example_img_list()
680
- example_ts = get_example_txt_list()
681
-
682
- SUPPORTED_FORMATS = ['glb', 'obj', 'ply', 'stl']
683
-
684
- HAS_TEXTUREGEN = False
685
- if not args.disable_tex:
686
- try:
687
- from hy3dgen.texgen import Hunyuan3DPaintPipeline
688
- print("着手加载贴图Pipeline")
689
- print(args.texgen_model_path)
690
- texgen_worker = Hunyuan3DPaintPipeline.from_pretrained(args.texgen_model_path)
691
- if args.low_vram_mode:
692
- texgen_worker.enable_model_cpu_offload()
693
- # Not help much, ignore for now.
694
- # if args.compile:
695
- # texgen_worker.models['delight_model'].pipeline.unet.compile()
696
- # texgen_worker.models['delight_model'].pipeline.vae.compile()
697
- # texgen_worker.models['multiview_model'].pipeline.unet.compile()
698
- # texgen_worker.models['multiview_model'].pipeline.vae.compile()
699
- HAS_TEXTUREGEN = True
700
- except Exception as e:
701
- print(e)
702
- print("Failed to load texture generator.")
703
- print('Please try to install requirements by following README.md')
704
- HAS_TEXTUREGEN = False
705
-
706
- HAS_T2I = True
707
- if args.enable_t23d:
708
- from hy3dgen.text2image import HunyuanDiTPipeline
709
-
710
- t2i_worker = HunyuanDiTPipeline('Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers-Distilled')
711
- HAS_T2I = True
712
-
713
- from hy3dgen.shapegen import FaceReducer, FloaterRemover, DegenerateFaceRemover, MeshSimplifier, \
714
- Hunyuan3DDiTFlowMatchingPipeline
715
- from hy3dgen.shapegen.pipelines import export_to_trimesh
716
- from hy3dgen.rembg import BackgroundRemover
717
-
718
- rmbg_worker = BackgroundRemover()
719
- print("着手加载模型Pipeline")
720
- print(args.model_path)
721
- print(args.subfolder)
722
- i23d_worker = Hunyuan3DDiTFlowMatchingPipeline.from_pretrained(
723
- args.model_path,
724
- subfolder=args.subfolder,
725
- use_safetensors=True,
726
- device=args.device,
727
- )
728
- if args.enable_flashvdm:
729
- mc_algo = 'mc' if args.device in ['cpu', 'mps'] else args.mc_algo
730
- i23d_worker.enable_flashvdm(mc_algo=mc_algo)
731
- if args.compile:
732
- i23d_worker.compile()
733
-
734
- floater_remove_worker = FloaterRemover()
735
- degenerate_face_remove_worker = DegenerateFaceRemover()
736
- face_reduce_worker = FaceReducer()
737
-
738
- # https://discuss.huggingface.co/t/how-to-serve-an-html-file/33921/2
739
- # create a FastAPI app
740
- app = FastAPI()
741
- # create a static directory to store the static files
742
- static_dir = Path(SAVE_DIR).absolute()
743
- static_dir.mkdir(parents=True, exist_ok=True)
744
- app.mount("/static", StaticFiles(directory=static_dir, html=True), name="static")
745
- shutil.copytree('./assets/env_maps', os.path.join(static_dir, 'env_maps'), dirs_exist_ok=True)
746
-
747
- if args.low_vram_mode:
748
- torch.cuda.empty_cache()
749
- demo = build_app()
750
- app = gr.mount_gradio_app(app, demo, path="/")
751
- uvicorn.run(app, host=args.host, port=args.port)