0notexist0 commited on
Commit
e985524
·
verified ·
1 Parent(s): 61c9d1f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import trimesh
4
+
5
+ # Assicurati di aver installato i pacchetti:
6
+ # pip install get3d gradio trimesh
7
+
8
+ from get3d.models import get_model
9
+ from get3d.configs import MODEL_CONFIGS
10
+
11
+ # Configurazione: scegli un modello pretrained (es: 'chair', 'car', 'table')
12
+ MODEL_NAME = 'chair'
13
+ config = MODEL_CONFIGS[MODEL_NAME]
14
+
15
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
16
+ # Carica il modello GET3D pretrained
17
+ model = get_model(config).to(device)
18
+ model.eval()
19
+
20
+
21
+ def generate_mesh(prompt: str) -> str:
22
+ """
23
+ Genera una mesh 3D casuale basata sul prompt (demo non testuale) e la salva come .obj
24
+ """
25
+ # GET3D non supporta direttamente text-to-3D; qui usiamo un vettore latente casuale
26
+ z = torch.randn(1, config.z_dim, device=device)
27
+ with torch.no_grad():
28
+ verts, faces = model.generate_mesh(z)
29
+
30
+ # Costruisci l'oggetto Trimesh e salvalo
31
+ mesh = trimesh.Trimesh(vertices=verts[0].cpu().numpy(), faces=faces[0].cpu().numpy())
32
+ output_path = f"/mnt/data/mesh_{torch.randint(0,1e6,(1,)).item()}.obj"
33
+ mesh.export(output_path)
34
+ return output_path
35
+
36
+
37
+ def respond(message, history):
38
+ # history è una lista di tuple (user, bot)
39
+ mesh_file = generate_mesh(message)
40
+ bot_msg = f"Mesh generata! Scarica qui: {{}}".format(mesh_file)
41
+ history.append((message, bot_msg))
42
+ return history
43
+
44
+ with gr.Blocks() as demo:
45
+ chatbot = gr.Chatbot(label="Chatbot GET3D")
46
+ txt = gr.Textbox(placeholder="Inserisci un prompt per la mesh...")
47
+ txt.submit(respond, [txt, chatbot], chatbot)
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch() # Imposta share=True se vuoi ottenere un link pubblico