fandimuhammads commited on
Commit
71bcbad
·
verified ·
1 Parent(s): 22b6ae5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -1,38 +1,33 @@
 
1
  import gradio as gr
2
- import torch
3
- from shap_e.diffusion.sample import sample_latents
4
- from shap_e.diffusion.gaussian_diffusion import create_gaussian_diffusion
5
- from shap_e.models.text_to_3d import create_model_and_diffusion
6
- from shap_e.util.notebooks import decode_latent_mesh
7
 
8
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
 
10
- print("Loading model...")
11
- model, diffusion = create_model_and_diffusion(batch_size=1, device=device)
12
- model.eval()
13
 
14
  def generate_3d(prompt):
15
- with torch.no_grad():
16
- latents = sample_latents(
17
- batch_size=1,
18
- model=model,
19
- diffusion=diffusion,
20
- guidance_scale=15.0,
21
- model_kwargs=dict(texts=[prompt]),
22
- progress=True,
23
- device=device,
24
- )
25
- mesh = decode_latent_mesh(latents[0].cpu())
26
- output_path = "/tmp/shape.glb"
27
- with open(output_path, "wb") as f:
28
- mesh.export(output_path)
29
- return output_path
30
 
31
- gr.Interface(
32
  fn=generate_3d,
33
- inputs=gr.Textbox(label="Prompt (contoh: 'a sci-fi turret tower')"),
34
- outputs=gr.Model3D(label="3D Model Output (.glb)"),
35
- title="Text to 3D - SHAP-E Generator",
36
- description="Masukkan prompt dan hasilkan file 3D untuk digunakan di game engine seperti Unity/Unreal"
37
- ).launch()
38
-
 
1
+ import os
2
  import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+ import tempfile
5
+ import trimesh
 
 
6
 
7
+ HF_TOKEN = os.getenv("HF_TOKEN") # token environment
8
 
9
+ client = InferenceClient(token=HF_TOKEN)
 
 
10
 
11
  def generate_3d(prompt):
12
+ result = client.text_to_mesh(
13
+ model="Webaverse/Stable-Dreamfusion",
14
+ inputs={"prompt": prompt, "num_steps": 100},
15
+ )
16
+ mesh_bytes = result["output"] # raw mesh bytes
17
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix=".ply")
18
+ temp.write(mesh_bytes)
19
+ temp.flush()
20
+ # optionally convert to .glb
21
+ mesh = trimesh.load(temp.name)
22
+ glb_path = temp.name.replace(".ply", ".glb")
23
+ mesh.export(glb_path)
24
+ return glb_path
 
 
25
 
26
+ iface = gr.Interface(
27
  fn=generate_3d,
28
+ inputs=gr.Textbox(label="Prompt Game Asset"),
29
+ outputs=gr.Model3D(label="Generated 3D Asset (.glb)"),
30
+ title="Textto3D Game Asset (Dreamfusion)",
31
+ description="Masukkan prompt, hasil akan berupa file .glb yang siap impor ke game engine."
32
+ )
33
+ iface.launch()