File size: 1,822 Bytes
32da876
 
 
 
 
 
ac02a11
0ad58c4
 
 
ac9fb9d
32da876
 
 
 
 
 
 
 
 
 
 
ac02a11
0ad58c4
 
 
 
325a91b
32da876
 
 
 
 
 
 
 
5e3add1
ac02a11
 
 
0ad58c4
 
 
 
32da876
 
 
 
 
 
 
 
 
84fce7c
32da876
 
 
00b1d4a
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
from meshgpt_pytorch import ( 
    MeshTransformer,
    mesh_render
)
import igl
import gradio as gr
import tempfile
import os
import threading
import time
import spaces

transformer = MeshTransformer.from_pretrained("MarcusLoren/MeshGPT-preview")

def save_as_obj(file_path):
    v, f = igl.read_triangle_mesh(file_path)
    v, f, _, _ = igl.remove_unreferenced(v, f)
    c, _ = igl.orientable_patches(f)
    f, _ = igl.orient_outward(v, f, c)
    igl.write_triangle_mesh(file_path, v, f)
    return file_path


def delete_file_after_ten_minutes(filename):
    time.sleep(600)  # Wait for 10 minutes
    os.remove(filename)

@spaces.GPU()
def predict(text, num_input, num_temp):
    transformer.eval()
    labels = [label.strip() for label in text.split(',')] 
    output = []
    if num_input > 1:
        for label in labels:
            output.append((transformer.generate(texts = [label ] * num_input, temperature = num_temp)))   
    else:
        output.append((transformer.generate(texts = labels  , temperature = num_temp)))   

    with tempfile.NamedTemporaryFile(suffix=".obj", delete=False) as temp_file:
        mesh_render.save_rendering(temp_file.name, output)
        result = save_as_obj(temp_file.name)
    threading.Thread(target=delete_file_after_ten_minutes, args=(temp_file.name,)).start()

    return result

gradio_app = gr.Interface(
    predict, 
    inputs=[
        gr.Textbox(label="Enter labels, separated by commas"),
        gr.Number(value=1, label="Number of examples per input"),
        gr.Slider(minimum=0, maximum=1, value=0, label="Temperature (0 to 1)")
    ],
    outputs=gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model"),
    title="MeshGPT Inference - (Rendering doesn't work, please download for best result)",
)

if __name__ == "__main__":
    gradio_app.launch()