Spaces:
mashroo
/
Runtime error

YoussefAnso commited on
Commit
4ce632b
·
1 Parent(s): a50db4b

Refactor 3D mesh generation in inference.py to streamline the process. Introduced ensure_dir function for output directory management, updated generate3d function to accept model and image inputs, and improved mesh export and conversion to GLB format. This enhances code clarity and maintains compatibility with Gradio display.

Browse files
Files changed (1) hide show
  1. inference.py +15 -29
inference.py CHANGED
@@ -1,39 +1,25 @@
1
  import os
2
  import torch
3
- import numpy as np
4
- from PIL import Image
5
  from mesh import Mesh
6
- from pipelines.pipeline_text_to_3d import TextTo3D
7
 
 
 
 
8
 
9
- # === Load Model (assumes this is done once at startup, not per request) ===
10
- model = TextTo3D.from_pretrained("./checkpoints/zeroscope_v1_5")
11
- model.to(torch.device("cpu"))
12
- model.eval()
13
-
14
- def generate3d(prompt: str, guidance_scale: float = 15.0, steps: int = 50) -> str:
15
- # === Set up paths ===
16
  output_dir = "outputs"
17
- os.makedirs(output_dir, exist_ok=True)
18
- base_name = prompt.replace(" ", "_").lower()
19
- mesh_path_base = os.path.join(output_dir, base_name)
20
-
21
- # === Generate 3D Mesh ===
22
- mesh = model(prompt, guidance_scale=guidance_scale, steps=steps)
23
- obj_path = mesh_path_base + ".obj"
24
- mesh.export_mesh_wt_uv(obj_path)
25
 
26
- # === Convert to GLB with textures ===
27
- mesh_loaded = Mesh.load(obj_path, device=torch.device("cpu"))
28
- glb_path = mesh_path_base + ".glb"
29
- mesh_loaded.write(glb_path)
30
 
31
- # === Return GLB path for Gradio display ===
32
- return glb_path
33
 
 
 
 
34
 
35
- if __name__ == "__main__":
36
- # Example run
37
- prompt = "a modern wooden chair"
38
- output_glb = generate3d(prompt)
39
- print(f"Generated GLB: {output_glb}")
 
1
  import os
2
  import torch
 
 
3
  from mesh import Mesh
 
4
 
5
+ # Ensure the output directory exists
6
+ def ensure_dir(path):
7
+ os.makedirs(path, exist_ok=True)
8
 
9
+ def generate3d(model, rgb_image, xyz_image, device="cpu"):
 
 
 
 
 
 
10
  output_dir = "outputs"
11
+ ensure_dir(output_dir)
 
 
 
 
 
 
 
12
 
13
+ prompt_id = "mesh_output"
14
+ base_path = os.path.join(output_dir, prompt_id)
15
+ obj_path = base_path + ".obj"
16
+ glb_path = base_path + ".glb"
17
 
18
+ # Export mesh using CRM model (generates .obj and textures)
19
+ model.export_mesh_wt_uv(obj_path, rgb=rgb_image, xyz=xyz_image, device=device)
20
 
21
+ # Load and convert to GLB
22
+ mesh = Mesh.load(obj_path, device=torch.device("cpu"))
23
+ mesh.write(glb_path)
24
 
25
+ return glb_path