Nuno Martins
02/12/2025, 3:03 PMJian Shen Yap
02/12/2025, 5:15 PMbentoml.models.create to save any arbitrary object/files into a bentoml modelNuno Martins
02/13/2025, 9:21 AMpath_of method/property of BentoModel seems to not exist, should I use model.stored.path instead?Jian Shen Yap
02/13/2025, 9:22 AMBentoModel?Nuno Martins
02/13/2025, 9:30 AMbentomodel = bentoml.models.BentoModel("model:local")Nuno Martins
02/13/2025, 9:30 AMbentomodel.path_of does not existJian Shen Yap
02/13/2025, 9:33 AM.resolve() to get the model.
if you are just testing it in a python shell, you would need to do
bentomodel = bentoml.models.BentoModel("model:local").resolve()
bentomodel.path_of ...Nuno Martins
02/13/2025, 9:35 AMmodel.resolve().path_of("model.pickle")Jian Shen Yap
02/13/2025, 9:36 AMbentoml.service you dont want to call .resolve() manually as it will be done internallyNuno Martins
02/13/2025, 9:37 AM___init__ of the service,_ so I think I need to right?Jian Shen Yap
02/13/2025, 9:38 AMNuno Martins
02/13/2025, 10:57 AMToke Emil Heldbo Reines
02/18/2025, 8:44 AMNuno Martins
02/18/2025, 10:33 AMbuild_bento.py that is responsible for building the Bento and packaging the model with it, it looks something like this:
# Download model from S3 to BentoML Model Store
with bentoml.models.create(name=model_store_tag) as model_ref:
S3Utils.download(model_path, model_ref.path)
# Build Bento
bentoml.build(
service=bento_service, # Name of Service
include=build_settings.BENTOFILE_INCLUDE_FILES, # List of files to include
models=[model_store_tag], # List of Models to Package
python={"packages": build_settings.ADDITIONAL_PACKAGES},
docker={"base_image": base_image},
envs=envs,
build_ctx=bento_build_ctx,
version=bento_tag,
)Toke Emil Heldbo Reines
02/18/2025, 10:40 AMNuno Martins
02/18/2025, 10:40 AMToke Emil Heldbo Reines
02/18/2025, 10:58 AMclass CombinedModel(mlflow.pyfunc.PythonModel):
"""
Combined for both the scaler and the pyro VAE
"""
def __init__(self, scaler_uri, vae_uri):
self.scaler_uri = scaler_uri
self.vae_uri = vae_uri
self.scaler: RobustScaler = None
self.anomaly_detection: AnomalyDetectionVAE = None
def load_context(self, context):
"""
Loads artifacts from the specified :class:`~PythonModelContext` that can be used by
:func:`~PythonModel.predict` when evaluating inputs. When loading an MLflow model with
:func:`~load_model`, this method is called as soon as the :class:`~PythonModel` is
constructed.
The same :class:`~PythonModelContext` will also be available during calls to
:func:`~PythonModel.predict`, but it may be more efficient to override this method
and load artifacts from the context at model load time.
Args:
context: A :class:`~PythonModelContext` instance containing artifacts that the model
can use to perform inference.
"""
self.scaler = mlflow.sklearn.load_model(self.scaler_uri)
self.anomaly_detection = mlflow.pytorch.load_model(self.vae_uri)
def predict(self, context, model_input):
scaled_data = self.scaler.transform(model_input)
input_tensor = torch.tensor(scaled_data, dtype=torch.float32)
output = self.anomaly_detection.predict(input_tensor)
return outputJian Shen Yap
02/18/2025, 4:52 PMwith bentoml.models.create(
name='my-local-model', # Name of the model in the Model Store
) as model_ref:
# 1st. pickle your object
# 2nd save your object to model_ref.path
https://docs.bentoml.com/en/latest/build-with-bentoml/model-loading-and-management.html#save-a-model