ifire's picture
Update app.py
325a91b verified
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()