File size: 1,970 Bytes
a4819f0
 
 
 
 
 
e985524
 
a4819f0
 
 
 
e985524
a4819f0
e985524
a4819f0
 
 
e985524
a4819f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e985524
a4819f0
 
 
 
 
e985524
a4819f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e985524
 
a4819f0
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
"""
Gradio demo per Shap-E (text-to-3D) – Hugging Face Spaces
Autore: tu
"""

import os
import gradio as gr
import torch
from shap_e.diffusion.sample import sample_latents
from shap_e.diffusion.gaussian_diffusion import diffusion_from_config
from shap_e.models.download import load_model, load_config
from shap_e.util.notebooks import decode_latent_mesh

# ---------- caricamento modelli ----------
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
xm = load_model('transmitter', device=device)
model = load_model('text300M', device=device)
diffusion = diffusion_from_config(load_config('diffusion'))

# ---------- logica ----------
def generate(prompt: str,
             guidance: float = 15.0,
             steps: int = 64):
    """Genera la mesh e restituisce il file .ply scaricabile."""
    latents = sample_latents(
        batch_size=1,
        model=model,
        diffusion=diffusion,
        guidance_scale=guidance,
        model_kwargs=dict(texts=[prompt]),
        progress=True,
        clip_denoised=True,
        use_fp16=True,
        use_karras=True,
        karras_steps=steps,
        sigma_min=1e-3,
        sigma_max=160,
        s_churn=0,
    )

    t = decode_latent_mesh(xm, latents[0]).tri_mesh()
    out_path = "output.ply"
    with open(out_path, "wb") as f:
        t.write_ply(f)
    return out_path

# ---------- interfaccia Gradio ----------
iface = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Prompt"),
        gr.Slider(1, 30, value=15, label="Guidance scale"),
        gr.Slider(32, 128, value=64, step=16, label="Karras steps")
    ],
    outputs=gr.File(label="Scarica mesh .ply"),
    title="Shap-E Text-to-3D",
    description="Genera una mesh 3D da una descrizione testuale con Shap-E.",
    examples=[["a high–quality red sports car"],
              ["a cute low-poly cat"]],
    cache_examples=False   # vogliamo sempre generare
)

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