File size: 2,220 Bytes
8fa7e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65122b9
a19c21d
f938c6f
 
 
 
65122b9
 
f938c6f
65122b9
 
8fa7e90
f938c6f
 
 
 
 
 
65122b9
f938c6f
 
65122b9
f938c6f
 
65122b9
 
 
 
 
 
8fa7e90
65122b9
f938c6f
 
65122b9
 
f938c6f
 
 
8fa7e90
f938c6f
65122b9
8fa7e90
 
a19c21d
da03292
f938c6f
 
da03292
65122b9
 
 
 
 
 
a19c21d
f938c6f
65122b9
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
import subprocess
import sys
import os

# Clona TripoSG se non presente (non installabile via pip)
TRIPOSG_PATH = "/app/TripoSG"
if not os.path.exists(TRIPOSG_PATH):
    subprocess.run(
        ["git", "clone", "--depth=1",
         "https://github.com/VAST-AI-Research/TripoSG.git",
         TRIPOSG_PATH],
        check=True
    )
# Aggiungi al path Python
sys.path.insert(0, TRIPOSG_PATH)

import gradio as gr
import torch
import spaces
import tempfile
from PIL import Image
from triposg.pipelines.pipeline_triposg import TripoSGPipeline

print("Caricamento del modello VAST-AI/TripoSG...")
pipe = TripoSGPipeline.from_pretrained(
    "VAST-AI/TripoSG",
    torch_dtype=torch.float16,
)

@spaces.GPU(duration=120)
def generate_3d_mesh(input_image: Image.Image):
    if input_image is None:
        raise gr.Error("Carica un'immagine valida prima di generare il modello.")

    pipe.to("cuda")
    input_image = input_image.convert("RGB")

    generator = torch.Generator(device="cuda").manual_seed(42)
    output = pipe(input_image, generator=generator)

    if hasattr(output, "meshes"):
        mesh = output.meshes[0]
    elif hasattr(output, "mesh"):
        mesh = output.mesh[0]
    else:
        raise ValueError("Nessuna mesh trovata nell'output della pipeline.")

    temp_dir = tempfile.mkdtemp()
    glb_path = os.path.join(temp_dir, "output.glb")
    mesh.export(glb_path)

    return glb_path

with gr.Blocks(title="Generatore 3D - TripoSG", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
        # 🚀 TripoSG: Da Immagine a Modello 3D
        Utilizza **VAST-AI/TripoSG** per generare modelli 3D ad altissima fedeltà.  
        *Consiglio: usa immagini con sfondo bianco o trasparente.*
    """)
    with gr.Row():
        with gr.Column():
            img_input = gr.Image(type="pil", label="Carica la tua Immagine")
            gen_btn = gr.Button("🎨 Genera Modello 3D", variant="primary")
        with gr.Column():
            model_output = gr.Model3D(
                label="Modello Generato",
                clear_color=[0.0, 0.0, 0.0, 0.0]
            )

    gen_btn.click(fn=generate_3d_mesh, inputs=[img_input], outputs=[model_output])

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