Spaces:
Running
on
Zero
Running
on
Zero
Add support to MCP
Browse files- gradio_app.py +146 -1
gradio_app.py
CHANGED
|
@@ -285,6 +285,38 @@ def _gen_shape(
|
|
| 285 |
num_chunks=200000,
|
| 286 |
randomize_seed: bool = False,
|
| 287 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
if not MV_MODE and image is None and caption is None:
|
| 289 |
raise gr.Error("Please provide either a caption or an image.")
|
| 290 |
if MV_MODE:
|
|
@@ -394,6 +426,36 @@ def generation_all(
|
|
| 394 |
num_chunks=200000,
|
| 395 |
randomize_seed: bool = False,
|
| 396 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
start_time_0 = time.time()
|
| 398 |
mesh, image, save_folder, stats, seed = _gen_shape(
|
| 399 |
caption,
|
|
@@ -476,6 +538,34 @@ def shape_generation(
|
|
| 476 |
num_chunks=200000,
|
| 477 |
randomize_seed: bool = False,
|
| 478 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
start_time_0 = time.time()
|
| 480 |
mesh, image, save_folder, stats, seed = _gen_shape(
|
| 481 |
caption,
|
|
@@ -716,6 +806,22 @@ Fast for very complex cases, Standard seldom use.',
|
|
| 716 |
)
|
| 717 |
|
| 718 |
def on_gen_mode_change(value):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 719 |
if value == 'Turbo':
|
| 720 |
return gr.update(value=5)
|
| 721 |
elif value == 'Fast':
|
|
@@ -726,6 +832,22 @@ Fast for very complex cases, Standard seldom use.',
|
|
| 726 |
gen_mode.change(on_gen_mode_change, inputs=[gen_mode], outputs=[num_steps])
|
| 727 |
|
| 728 |
def on_decode_mode_change(value):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
if value == 'Low':
|
| 730 |
return gr.update(value=196)
|
| 731 |
elif value == 'Standard':
|
|
@@ -738,6 +860,29 @@ Fast for very complex cases, Standard seldom use.',
|
|
| 738 |
|
| 739 |
def on_export_click(file_out, file_out2, file_type,
|
| 740 |
reduce_face, export_texture, target_face_num):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
if file_out is None:
|
| 742 |
raise gr.Error('Please generate a mesh first.')
|
| 743 |
|
|
@@ -917,7 +1062,7 @@ if __name__ == '__main__':
|
|
| 917 |
torch.cuda.empty_cache()
|
| 918 |
|
| 919 |
demo = build_app()
|
| 920 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 921 |
|
| 922 |
if ENV == 'Huggingface':
|
| 923 |
# for Zerogpu
|
|
|
|
| 285 |
num_chunks=200000,
|
| 286 |
randomize_seed: bool = False,
|
| 287 |
):
|
| 288 |
+
"""
|
| 289 |
+
Generate a 3D shape from text caption or image input using Hunyuan3D pipeline.
|
| 290 |
+
|
| 291 |
+
This function handles the core 3D shape generation process, including image preprocessing,
|
| 292 |
+
background removal, and 3D mesh generation from various input modalities.
|
| 293 |
+
|
| 294 |
+
Args:
|
| 295 |
+
caption (str, optional): Text description for shape generation. Defaults to None.
|
| 296 |
+
image (PIL.Image, optional): Input image for image-to-3D generation. Defaults to None.
|
| 297 |
+
mv_image_front (PIL.Image, optional): Front view image for multi-view mode. Defaults to None.
|
| 298 |
+
mv_image_back (PIL.Image, optional): Back view image for multi-view mode. Defaults to None.
|
| 299 |
+
mv_image_left (PIL.Image, optional): Left view image for multi-view mode. Defaults to None.
|
| 300 |
+
mv_image_right (PIL.Image, optional): Right view image for multi-view mode. Defaults to None.
|
| 301 |
+
steps (int, optional): Number of inference steps. Defaults to 50.
|
| 302 |
+
guidance_scale (float, optional): Guidance scale for diffusion process. Defaults to 7.5.
|
| 303 |
+
seed (int, optional): Random seed for reproducibility. Defaults to 1234.
|
| 304 |
+
octree_resolution (int, optional): Resolution for octree representation. Defaults to 256.
|
| 305 |
+
check_box_rembg (bool, optional): Whether to remove background from input images. Defaults to False.
|
| 306 |
+
num_chunks (int, optional): Number of chunks for processing. Defaults to 200000.
|
| 307 |
+
randomize_seed (bool, optional): Whether to randomize the seed. Defaults to False.
|
| 308 |
+
|
| 309 |
+
Returns:
|
| 310 |
+
tuple: Contains (mesh, main_image, save_folder, stats, seed)
|
| 311 |
+
- mesh: Generated 3D mesh object
|
| 312 |
+
- main_image: Primary input image used for generation
|
| 313 |
+
- save_folder: Directory where outputs are saved
|
| 314 |
+
- stats: Generation statistics and metadata
|
| 315 |
+
- seed: Final seed value used for generation
|
| 316 |
+
|
| 317 |
+
Raises:
|
| 318 |
+
gr.Error: If no input (caption or image) is provided in single-view mode, or no view images in multi-view mode.
|
| 319 |
+
"""
|
| 320 |
if not MV_MODE and image is None and caption is None:
|
| 321 |
raise gr.Error("Please provide either a caption or an image.")
|
| 322 |
if MV_MODE:
|
|
|
|
| 426 |
num_chunks=200000,
|
| 427 |
randomize_seed: bool = False,
|
| 428 |
):
|
| 429 |
+
"""
|
| 430 |
+
Complete pipeline for generating both 3D shape and textured mesh from user inputs.
|
| 431 |
+
|
| 432 |
+
This function orchestrates the entire generation process including shape generation,
|
| 433 |
+
face reduction, texture painting, and GLB export. It's triggered when users click
|
| 434 |
+
the "Gen Textured Shape" button.
|
| 435 |
+
|
| 436 |
+
Args:
|
| 437 |
+
caption (str, optional): Text description for shape generation. Defaults to None.
|
| 438 |
+
image (PIL.Image, optional): Input image for image-to-3D generation. Defaults to None.
|
| 439 |
+
mv_image_front (PIL.Image, optional): Front view image for multi-view mode. Defaults to None.
|
| 440 |
+
mv_image_back (PIL.Image, optional): Back view image for multi-view mode. Defaults to None.
|
| 441 |
+
mv_image_left (PIL.Image, optional): Left view image for multi-view mode. Defaults to None.
|
| 442 |
+
mv_image_right (PIL.Image, optional): Right view image for multi-view mode. Defaults to None.
|
| 443 |
+
steps (int, optional): Number of inference steps. Defaults to 50.
|
| 444 |
+
guidance_scale (float, optional): Guidance scale for diffusion process. Defaults to 7.5.
|
| 445 |
+
seed (int, optional): Random seed for reproducibility. Defaults to 1234.
|
| 446 |
+
octree_resolution (int, optional): Resolution for octree representation. Defaults to 256.
|
| 447 |
+
check_box_rembg (bool, optional): Whether to remove background from input images. Defaults to False.
|
| 448 |
+
num_chunks (int, optional): Number of chunks for processing. Defaults to 200000.
|
| 449 |
+
randomize_seed (bool, optional): Whether to randomize the seed. Defaults to False.
|
| 450 |
+
|
| 451 |
+
Returns:
|
| 452 |
+
tuple: Contains (file_out, glb_path_textured, model_viewer_html_textured, stats, seed)
|
| 453 |
+
- file_out: Path to the generated white mesh file
|
| 454 |
+
- glb_path_textured: Path to the textured GLB file
|
| 455 |
+
- model_viewer_html_textured: HTML content for 3D model viewer
|
| 456 |
+
- stats: Generation statistics and timing information
|
| 457 |
+
- seed: Final seed value used for generation
|
| 458 |
+
"""
|
| 459 |
start_time_0 = time.time()
|
| 460 |
mesh, image, save_folder, stats, seed = _gen_shape(
|
| 461 |
caption,
|
|
|
|
| 538 |
num_chunks=200000,
|
| 539 |
randomize_seed: bool = False,
|
| 540 |
):
|
| 541 |
+
"""
|
| 542 |
+
Generate a 3D shape without texturing from user inputs.
|
| 543 |
+
|
| 544 |
+
This function handles shape generation only, creating a white/untextured 3D mesh.
|
| 545 |
+
It's triggered when users click the "Gen Shape" button.
|
| 546 |
+
|
| 547 |
+
Args:
|
| 548 |
+
caption (str, optional): Text description for shape generation. Defaults to None.
|
| 549 |
+
image (PIL.Image, optional): Input image for image-to-3D generation. Defaults to None.
|
| 550 |
+
mv_image_front (PIL.Image, optional): Front view image for multi-view mode. Defaults to None.
|
| 551 |
+
mv_image_back (PIL.Image, optional): Back view image for multi-view mode. Defaults to None.
|
| 552 |
+
mv_image_left (PIL.Image, optional): Left view image for multi-view mode. Defaults to None.
|
| 553 |
+
mv_image_right (PIL.Image, optional): Right view image for multi-view mode. Defaults to None.
|
| 554 |
+
steps (int, optional): Number of inference steps. Defaults to 50.
|
| 555 |
+
guidance_scale (float, optional): Guidance scale for diffusion process. Defaults to 7.5.
|
| 556 |
+
seed (int, optional): Random seed for reproducibility. Defaults to 1234.
|
| 557 |
+
octree_resolution (int, optional): Resolution for octree representation. Defaults to 256.
|
| 558 |
+
check_box_rembg (bool, optional): Whether to remove background from input images. Defaults to False.
|
| 559 |
+
num_chunks (int, optional): Number of chunks for processing. Defaults to 200000.
|
| 560 |
+
randomize_seed (bool, optional): Whether to randomize the seed. Defaults to False.
|
| 561 |
+
|
| 562 |
+
Returns:
|
| 563 |
+
tuple: Contains (file_out, model_viewer_html, stats, seed)
|
| 564 |
+
- file_out: Gradio update with path to the generated mesh file
|
| 565 |
+
- model_viewer_html: HTML content for 3D model viewer
|
| 566 |
+
- stats: Generation statistics and timing information
|
| 567 |
+
- seed: Final seed value used for generation
|
| 568 |
+
"""
|
| 569 |
start_time_0 = time.time()
|
| 570 |
mesh, image, save_folder, stats, seed = _gen_shape(
|
| 571 |
caption,
|
|
|
|
| 806 |
)
|
| 807 |
|
| 808 |
def on_gen_mode_change(value):
|
| 809 |
+
"""
|
| 810 |
+
Update the number of inference steps based on the selected generation mode.
|
| 811 |
+
|
| 812 |
+
This function is triggered when the user changes the generation mode radio button
|
| 813 |
+
in the Options tab. It automatically adjusts the number of steps for optimal
|
| 814 |
+
performance based on the selected mode.
|
| 815 |
+
|
| 816 |
+
Args:
|
| 817 |
+
value (str): The selected generation mode ('Turbo', 'Fast', or 'Standard')
|
| 818 |
+
|
| 819 |
+
Returns:
|
| 820 |
+
gr.update: Gradio update object with the new number of steps value
|
| 821 |
+
- Turbo: 5 steps (fastest)
|
| 822 |
+
- Fast: 10 steps (balanced)
|
| 823 |
+
- Standard: 30 steps (highest quality)
|
| 824 |
+
"""
|
| 825 |
if value == 'Turbo':
|
| 826 |
return gr.update(value=5)
|
| 827 |
elif value == 'Fast':
|
|
|
|
| 832 |
gen_mode.change(on_gen_mode_change, inputs=[gen_mode], outputs=[num_steps])
|
| 833 |
|
| 834 |
def on_decode_mode_change(value):
|
| 835 |
+
"""
|
| 836 |
+
Update the octree resolution based on the selected decoding mode.
|
| 837 |
+
|
| 838 |
+
This function is triggered when the user changes the decoding mode radio button
|
| 839 |
+
in the Options tab. It automatically adjusts the octree resolution to match
|
| 840 |
+
the selected quality level for mesh export.
|
| 841 |
+
|
| 842 |
+
Args:
|
| 843 |
+
value (str): The selected decoding mode ('Low', 'Standard', or 'High')
|
| 844 |
+
|
| 845 |
+
Returns:
|
| 846 |
+
gr.update: Gradio update object with the new octree resolution value
|
| 847 |
+
- Low: 196 resolution (faster processing, lower quality)
|
| 848 |
+
- Standard: 256 resolution (balanced)
|
| 849 |
+
- High: 384 resolution (slower processing, higher quality)
|
| 850 |
+
"""
|
| 851 |
if value == 'Low':
|
| 852 |
return gr.update(value=196)
|
| 853 |
elif value == 'Standard':
|
|
|
|
| 860 |
|
| 861 |
def on_export_click(file_out, file_out2, file_type,
|
| 862 |
reduce_face, export_texture, target_face_num):
|
| 863 |
+
"""
|
| 864 |
+
Handle mesh export with user-specified options and format conversion.
|
| 865 |
+
|
| 866 |
+
This function is triggered when the user clicks the "Transform" button in the Export tab.
|
| 867 |
+
It processes the generated mesh according to user preferences, applies post-processing
|
| 868 |
+
operations, and exports to the desired format.
|
| 869 |
+
|
| 870 |
+
Args:
|
| 871 |
+
file_out (str): Path to the base generated mesh file
|
| 872 |
+
file_out2 (str): Path to the textured mesh file (if available)
|
| 873 |
+
file_type (str): Desired export format ('glb', 'obj', 'ply', 'stl')
|
| 874 |
+
reduce_face (bool): Whether to apply mesh simplification
|
| 875 |
+
export_texture (bool): Whether to export textured version
|
| 876 |
+
target_face_num (int): Target number of faces for mesh simplification
|
| 877 |
+
|
| 878 |
+
Returns:
|
| 879 |
+
tuple: Contains (model_viewer_html, file_export)
|
| 880 |
+
- model_viewer_html: Updated HTML for 3D model preview
|
| 881 |
+
- file_export: Gradio update with path to the exported file
|
| 882 |
+
|
| 883 |
+
Raises:
|
| 884 |
+
gr.Error: If no mesh has been generated yet
|
| 885 |
+
"""
|
| 886 |
if file_out is None:
|
| 887 |
raise gr.Error('Please generate a mesh first.')
|
| 888 |
|
|
|
|
| 1062 |
torch.cuda.empty_cache()
|
| 1063 |
|
| 1064 |
demo = build_app()
|
| 1065 |
+
app = gr.mount_gradio_app(app, demo, mcp_server=True, path="/")
|
| 1066 |
|
| 1067 |
if ENV == 'Huggingface':
|
| 1068 |
# for Zerogpu
|