Mohamed Meftah
05/20/2025, 6:38 PMNone. Do I need to bundle the models in my bento?
My workflow is that I have a script for saving the models. I run that and push them to BentoML Cloud. If I do bentoml serve, it works locally, but when I push the service, importing the model with BentoModel("tag") fails, returning None.Jian Shen Yap
05/21/2025, 5:08 AMBentoModel as a class attribute, the models will be automatically referenced / and bunlded into your bentoMohamed Meftah
05/21/2025, 7:07 AMimport os
import sys
import numpy as np
import torch
import trimesh
import bentoml
from pathlib import Path
from typing import Annotated
from pydantic import Field
from bentoml.validators import ContentType
TRIPOSG_CODE_DIR = "./triposg"
sys.path.append(TRIPOSG_CODE_DIR)
sys.path.append(os.path.join(TRIPOSG_CODE_DIR, "scripts"))
from triposg.scripts.briarmbg import BriaRMBG
from triposg.scripts.image_process import prepare_image
from triposg.pipelines.pipeline_triposg import TripoSGPipeline
WHITE = np.array([1.0, 1.0, 1.0])
image = (
bentoml.images.Image(python_version="3.10")
.requirements_file("./requirements.txt")
.system_packages(
"ninja-build",
"libffi-dev",
"libgl1-mesa-glx",
"libgles2-mesa-dev",
"mesa-utils",
"libglib2.0-dev",
)
)
RMBG_PRETRAINED_MODEL = "checkpoints/RMBG-1.4"
TRIPOSG_PRETRAINED_MODEL = "checkpoints/TripoSG"
@bentoml.service(image=image, resources={"gpu": 1})
class Mesh:
device = "cuda"
dtype = torch.float16
rmbg_net = BriaRMBG.from_pretrained(RMBG_PRETRAINED_MODEL).to(device)
triposg_pipe = TripoSGPipeline.from_pretrained(TRIPOSG_PRETRAINED_MODEL).to(device, dtype)
def __init__(self):
self.rmbg_net.eval()
@bentoml.task
@torch.no_grad()
def generate(
self,
context: bentoml.Context,
image_path: Annotated[Path, ContentType("image/*")],
seed: int = Field(default=1234, description="Seed for reproducibility"),
num_inference_steps: int = Field(
default=50, description="Number of inference steps"
),
guidance_scale: float = Field(
default=7.5, description="Guidance scale for the model"
),
) -> Annotated[Path, ContentType("application/octet-stream")]:
image = prepare_image(image_path, bg_color=WHITE, rmbg_net=self.rmbg_net)
generated = self.triposg_pipe(
image=image,
generator=torch.Generator(device=self.device).manual_seed(seed),
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
)
vertices, faces = generated.samples[0]
mesh = trimesh.Trimesh(vertices.astype(np.float32), np.ascontiguousarray(faces))
mesh_path = Path(context.temp_dir) / "mesh.glb"
mesh.export(mesh_path)
torch.cuda.empty_cache()
return mesh_path
this is the version i have working, in this case i have copied the models to the root folder manuallyMohamed Meftah
05/21/2025, 7:09 AMimport os
import sys
from bentoml.models import BentoModel
import numpy as np
import torch
import trimesh
import bentoml
from pathlib import Path
from typing import Annotated
from pydantic import Field
from bentoml.validators import ContentType
TRIPOSG_CODE_DIR = "./triposg"
sys.path.append(TRIPOSG_CODE_DIR)
sys.path.append(os.path.join(TRIPOSG_CODE_DIR, "scripts"))
from triposg.scripts.briarmbg import BriaRMBG
from triposg.scripts.image_process import prepare_image
from triposg.pipelines.pipeline_triposg import TripoSGPipeline
WHITE = np.array([1.0, 1.0, 1.0])
image = (
bentoml.images.Image(python_version="3.10")
.requirements_file("./requirements.txt")
.system_packages(
"ninja-build",
"libffi-dev",
"libgl1-mesa-glx",
"libgles2-mesa-dev",
"mesa-utils",
"libglib2.0-dev",
)
)
@bentoml.service(image=image, resources={"gpu": 1})
class Mesh:
device = "cuda"
dtype = torch.float16
rmbg_net = BriaRMBG.from_pretrained(
BentoModel("briarmbg:latest").resolve().path
).to(device)
triposg_pipe = TripoSGPipeline.from_pretrained(
BentoModel("triposg:latest").resolve().path
).to(device, dtype)
def __init__(self):
self.rmbg_net.eval()
@bentoml.task
@torch.no_grad()
def generate(
self,
context: bentoml.Context,
image_path: Annotated[Path, ContentType("image/*")],
seed: int = Field(default=1234, description="Seed for reproducibility"),
num_inference_steps: int = Field(
default=50, description="Number of inference steps"
),
guidance_scale: float = Field(
default=7.5, description="Guidance scale for the model"
),
) -> Annotated[Path, ContentType("application/octet-stream")]:
image = prepare_image(image_path, bg_color=WHITE, rmbg_net=self.rmbg_net)
generated = self.triposg_pipe(
image=image,
generator=torch.Generator(device=self.device).manual_seed(seed),
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
)
vertices, faces = generated.samples[0]
mesh = trimesh.Trimesh(vertices.astype(np.float32), np.ascontiguousarray(faces))
mesh_path = Path(context.temp_dir) / "mesh.glb"
mesh.export(mesh_path)
torch.cuda.empty_cache()
return mesh_path
this is the one that's not working
2025-05-21T07:00:28Z
[Service: Mesh][Replica: jwhsz]
$PORT is set! Overriding $BENTOML_PORT with $PORT (3000)
2025-05-21T07:00:30Z
[Service: Mesh][Replica: jwhsz]
DeprecationWarning: The parameter '--production' is deprecated and will be
2025-05-21T07:00:30Z
[Service: Mesh][Replica: jwhsz]
removed in the future. (Current behaviour: This is enabled by default. To run in
2025-05-21T07:00:30Z
[Service: Mesh][Replica: jwhsz]
development mode, use '--development'.)
2025-05-21T07:00:39Z
[Service: Mesh][Replica: jwhsz]
2025-05-21T07:00:38+0000 [INFO] [cli] Adding system packages using distro debian's package manager
2025-05-21T07:00:39Z
[Service: Mesh][Replica: jwhsz]
Error: [serve] `serve` failed: No cloud context default foundJian Shen Yap
05/22/2025, 7:15 AMBentoModel("briarmbg:latest") as a class attribute i.e.
class Mesh:
model = BentoModel("briarmbg:latest")
def __init__(self):
rmbg_net = BriaRMBG.from_pretrained(model.path).to(device)Mohamed Meftah
05/22/2025, 12:10 PMJian Shen Yap
05/22/2025, 3:39 PM