andre-2112 commited on
Commit
4911116
·
verified ·
1 Parent(s): a2f3c19

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +23 -24
handler.py CHANGED
@@ -3,16 +3,14 @@ import base64
3
  import io
4
  import tempfile
5
  from PIL import Image
6
- # Note: The import might change to 'trellis.2' or stay 'trellis' depending on how they packaged the V2 repo.
7
- # Standard import usually resolves to the installed package name 'trellis' even for v2.
8
- from trellis.pipelines import TrellisImageTo3DPipeline
9
- from trellis.utils import postprocessing_utils
10
 
11
  class EndpointHandler:
12
  def __init__(self, path=""):
13
- print("Loading Trellis 2 Model...")
14
- # The CORRECT Model ID for V2 (4 Billion parameters)
15
- self.pipeline = TrellisImageTo3DPipeline.from_pretrained(
16
  "microsoft/TRELLIS.2-4B",
17
  torch_dtype=torch.float16,
18
  use_safetensors=True
@@ -21,6 +19,10 @@ class EndpointHandler:
21
  print("Trellis 2 Loaded!")
22
 
23
  def __call__(self, data):
 
 
 
 
24
  # 1. Parse Input
25
  inputs = data.pop("inputs", data)
26
  if isinstance(inputs, dict) and "image" in inputs:
@@ -33,23 +35,20 @@ class EndpointHandler:
33
  image = inputs
34
 
35
  # 2. Inference
36
- outputs = self.pipeline.run(image, seed=42, formats=["mesh"])
 
 
37
 
38
  # 3. Export to GLB
39
- video_content = postprocessing_utils.to_glb(
40
- outputs['mesh'][0],
41
- simplify=0.95,
42
- texture_size=1024
43
- )
44
-
45
- # 4. Handle Output
46
- if isinstance(video_content, str):
47
- with open(video_content, "rb") as f: glb_bytes = f.read()
48
- elif isinstance(video_content, bytes):
49
- glb_bytes = video_content
50
- else:
51
- with tempfile.NamedTemporaryFile(suffix=".glb", delete=False) as tmp:
52
- outputs['mesh'][0].export(tmp.name, file_type='glb')
53
- with open(tmp.name, "rb") as f: glb_bytes = f.read()
54
 
55
- return {"glb": base64.b64encode(glb_bytes).decode('utf-8')}
 
 
 
3
  import io
4
  import tempfile
5
  from PIL import Image
6
+ # CORRECTED IMPORT: Package is 'trellis2', Class is 'Trellis2...'
7
+ from trellis2.pipelines import Trellis2ImageTo3DPipeline
 
 
8
 
9
  class EndpointHandler:
10
  def __init__(self, path=""):
11
+ # Load the official Microsoft Trellis 2 Model
12
+ print("Loading Trellis 2 (4B) Model...")
13
+ self.pipeline = Trellis2ImageTo3DPipeline.from_pretrained(
14
  "microsoft/TRELLIS.2-4B",
15
  torch_dtype=torch.float16,
16
  use_safetensors=True
 
19
  print("Trellis 2 Loaded!")
20
 
21
  def __call__(self, data):
22
+ """
23
+ Input: {"inputs": "base64_string"}
24
+ Output: {"glb": "base64_string"}
25
+ """
26
  # 1. Parse Input
27
  inputs = data.pop("inputs", data)
28
  if isinstance(inputs, dict) and "image" in inputs:
 
35
  image = inputs
36
 
37
  # 2. Inference
38
+ # Trellis 2 returns a list of Mesh objects directly
39
+ outputs = self.pipeline.run(image, seed=42)
40
+ mesh_result = outputs[0]
41
 
42
  # 3. Export to GLB
43
+ # The V2 mesh object has a direct export method
44
+ with tempfile.NamedTemporaryFile(suffix=".glb", delete=False) as tmp:
45
+ # export() takes a filepath string
46
+ mesh_result.export(tmp.name)
47
+
48
+ # Read back the bytes
49
+ with open(tmp.name, "rb") as f:
50
+ glb_bytes = f.read()
 
 
 
 
 
 
 
51
 
52
+ # 4. Return Base64
53
+ out_b64 = base64.b64encode(glb_bytes).decode('utf-8')
54
+ return {"glb": out_b64}