| import torch |
| import base64 |
| import io |
| import tempfile |
| from PIL import Image |
| |
| from trellis2.pipelines import Trellis2ImageTo3DPipeline |
|
|
| class EndpointHandler: |
| def __init__(self, path=""): |
| |
| print("Loading Trellis 2 (4B) Model...") |
| self.pipeline = Trellis2ImageTo3DPipeline.from_pretrained( |
| "microsoft/TRELLIS.2-4B", |
| torch_dtype=torch.float16, |
| use_safetensors=True |
| ) |
| self.pipeline.cuda() |
| print("Trellis 2 Loaded!") |
|
|
| def __call__(self, data): |
| """ |
| Input: {"inputs": "base64_string"} |
| Output: {"glb": "base64_string"} |
| """ |
| |
| inputs = data.pop("inputs", data) |
| if isinstance(inputs, dict) and "image" in inputs: |
| inputs = inputs["image"] |
| |
| if isinstance(inputs, str): |
| image_data = base64.b64decode(inputs) |
| image = Image.open(io.BytesIO(image_data)).convert("RGB") |
| else: |
| image = inputs |
|
|
| |
| |
| outputs = self.pipeline.run(image, seed=42) |
| mesh_result = outputs[0] |
| |
| |
| |
| with tempfile.NamedTemporaryFile(suffix=".glb", delete=False) as tmp: |
| |
| mesh_result.export(tmp.name) |
| |
| |
| with open(tmp.name, "rb") as f: |
| glb_bytes = f.read() |
|
|
| |
| out_b64 = base64.b64encode(glb_bytes).decode('utf-8') |
| return {"glb": out_b64} |