File size: 3,490 Bytes
0436f12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr
from gradio_client import Client, handle_file

# Create a client for the external 3D generation API hosted on "tencent/Hunyuan3D-2"
api_client = Client("tencent/Hunyuan3D-2")

def generate_3d(caption: str, image, steps: float, guidance_scale: float, seed: float, octree_resolution: str, check_box_rembg: bool):
    """
    Calls the external /generation_all API endpoint to generate 3D outputs.
    Returns a tuple of:
      [0] White Mesh file (filepath),
      [1] Textured Mesh file (filepath),
      [2] HTML output string,
      [3] Additional HTML output string.
    """
    # If no image is provided, use a default image URL
    if image is None:
        image = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
    
    image_path = handle_file(image)
    
    result = api_client.predict(
        caption=caption,
        image=image_path,
        steps=steps,
        guidance_scale=guidance_scale,
        seed=seed,
        octree_resolution=octree_resolution,
        check_box_rembg=check_box_rembg,
        api_name="/generation_all"
    )
    return result

# Build the UI using Gradio Blocks for a more organized layout.
with gr.Blocks(title="3D Generator") as demo:
    gr.Markdown("# 3D Generator")
    gr.Markdown("Generate 3D models using the external `/generation_all` API from `tencent/Hunyuan3D-2`.")
    
    with gr.Row():
        with gr.Column():
            caption_input = gr.Textbox(
                label="Text Prompt (Caption)",
                placeholder="Enter your caption here",
                lines=2
            )
            image_input = gr.Image(
                label="Input Image",
                type="filepath"
            )
        with gr.Column():
            steps_input = gr.Slider(
                minimum=1, maximum=100, step=1, value=50,
                label="Inference Steps"
            )
            guidance_scale_input = gr.Slider(
                minimum=1.0, maximum=10.0, step=0.1, value=5.5,
                label="Guidance Scale"
            )
            seed_input = gr.Slider(
                minimum=0, maximum=10000, step=1, value=1234,
                label="Seed"
            )
            octree_resolution_input = gr.Dropdown(
                choices=["256", "384", "512"],
                value="256",
                label="Octree Resolution"
            )
            check_box_rembg_input = gr.Checkbox(
                label="Remove Background",
                value=True
            )
    
    generate_button = gr.Button("Generate 3D Model")
    
    gr.Markdown("### Generated Outputs")
    with gr.Row():
        white_mesh_output = gr.File(label="Download White Mesh")
        textured_mesh_output = gr.File(label="Download Textured Mesh")
    output_html1 = gr.HTML(label="Output HTML 1")
    output_html2 = gr.HTML(label="Output HTML 2")
    
    # Connect the button click event to the generate_3d function.
    generate_button.click(
        fn=generate_3d,
        inputs=[
            caption_input, image_input, steps_input,
            guidance_scale_input, seed_input,
            octree_resolution_input, check_box_rembg_input
        ],
        outputs=[
            white_mesh_output, textured_mesh_output,
            output_html1, output_html2
        ]
    )
    
    gr.Markdown("## Enjoy your generated 3D models!")

if __name__ == "__main__":
    # Launch the app with a public share link (if desired)
    demo.launch(share=True)