File size: 4,402 Bytes
5620279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cca6b60
5620279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cca6b60
5620279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af10aca
f9c7241
 
 
 
 
 
 
 
 
 
 
 
 
 
cef3a18
f9c7241
 
 
 
 
 
cef3a18
f9c7241
 
 
cca6b60
5620279
 
 
 
 
 
 
 
 
 
cca6b60
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import re
import subprocess
import gradio as gr
import trimesh
import tempfile
import time
import pathlib
import os

os.system("pip install -e ./simple-knn")
os.system("pip install -e ./diff-gaussian-rasterization")

TEMP_DIR = "/tmp/dreamgaussian"
os.makedirs(TEMP_DIR, exist_ok=True)


def create_from_text(prompt):
    temp_dir = tempfile.mkdtemp(dir=TEMP_DIR)
    sanitized_prompt = re.sub("[^0-9a-zA-Z]+", "_", prompt)
    cmd1 = [
        "python",
        "main.py",
        "--config",
        "configs/text.yaml",
        f"prompt={prompt}",
        f"outdir={temp_dir}",
        f"save_path={sanitized_prompt}",
        "force_cuda_rast=True",
        "mesh_format=glb",
    ]
    cmd2 = [
        "python",
        "main2.py",
        "--config",
        "configs/text.yaml",
        f"prompt={prompt}",
        f"outdir={temp_dir}",
        f"save_path={sanitized_prompt}",
        "force_cuda_rast=True",
        "mesh_format=glb",
    ]
    subprocess.run(cmd1)
    subprocess.run(cmd2)
    glb_path = f"{temp_dir}/{sanitized_prompt}.glb"
    return [get_html_model(glb_path), glb_path]


def create_from_image(image):
    temp_dir = tempfile.mkdtemp(dir=TEMP_DIR)
    sanitized_prompt = "image"
    image.save(f"{temp_dir}/{sanitized_prompt}.png")
    cmd1 = [
        "python",
        "process.py",
        f"{temp_dir}/{sanitized_prompt}.png",
        "--size",
        "512",
    ]  # <<< 256 may be a good default
    cmd2 = [
        "python",
        "main.py",
        "--config",
        "configs/image.yaml",
        f"outdir={temp_dir}",
        f"input={temp_dir}/{sanitized_prompt}_rgba.png",
        f"save_path={sanitized_prompt}",
        "force_cuda_rast=True",
        "mesh_format=glb",
    ]
    cmd3 = [
        "python",
        "main2.py",
        "--config",
        "configs/image.yaml",
        f"outdir={temp_dir}",
        f"input={temp_dir}/{sanitized_prompt}_rgba.png",
        f"save_path={sanitized_prompt}",
        "force_cuda_rast=True",
        "mesh_format=glb",
    ]
    print(cmd1)
    print(cmd2)
    print(cmd3)
    subprocess.run(cmd1)
    subprocess.run(cmd2)
    subprocess.run(cmd3)
    glb_path = f"{temp_dir}/{sanitized_prompt}.glb"
    return [get_html_model(glb_path), glb_path]


def get_html_model(f_path):
    iframe = f"""<iframe src="file=model.html" model-url="file={f_path}" width="100%" height="500px"></iframe>"""

    return iframe


def generate(prompt, image):
    if prompt:
        gr.Info("Generating from prompt")
        return create_from_text(prompt)
    elif image:
        gr.Info("Generating from image")
        return create_from_image(image)
    raise gr.Error("Please enter a prompt or upload an image.")


with gr.Blocks() as demo:
    gr.Markdown(
        """
# DreamGaussian
source: https://github.com/dreamgaussian/dreamgaussian
"""
    )
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(lines=2, placeholder="Enter Prompt...")
            image = gr.Image(type="pil", label="Image")
            with gr.Row():
                clear = gr.Button("Clear")
                btn = gr.Button("Generate")
        with gr.Column():
            with gr.Box():
                model_3d = gr.HTML(
                    value="<div style='min-height: 240px;'>",
                    label="3D Model Viewer",
                    show_label=True,
                )
            download = gr.File(label="Download 3D file")
    with gr.Row():
        with gr.Column():
          with gr.Row():
            gr.Examples(
                [["pikachu pokemon"]],
                fn=lambda x: generate(x, None),
                inputs=[prompt],
                outputs=[model_3d, download],
                cache_examples=False,
            )
            gr.Examples(
                [["./examples/b.jpg"]],
                fn=lambda x: generate(None, x),
                inputs=[image],
                outputs=[model_3d, download],
                cache_examples=False,
            )
        with gr.Column():  
          pass
    btn.click(generate, inputs=[prompt, image], outputs=[model_3d, download])
    clear.click(
        lambda x: (gr.update(value=None), gr.update(value=None)),
        None,
        [prompt, image],
        queue=False,
    )

demo.queue(api_open=False, concurrency_count=1)
demo.launch(
    debug=True, show_api=False, inline=False, share=True, allowed_paths=[TEMP_DIR]
)