Matteo Bernardon
04/01/2025, 10:07 AM### model creation
import joblib
import bentoml
import pickle
pickle_file = "breast_cancer_model.pkl"
with open(pickle_file, "rb") as f:
model = pickle.load(f)
with bentoml.models.create("breast_cancer_model") as bento_model:
joblib.dump(model, bento_model.path_of("model.pkl"))
print(f"Model saved: {bento_model}")
### model service
import bentoml
import numpy as np
from pydantic import Field
import joblib
@bentoml.service(
resources={
"cpu": "1",
"memory": "2Gi",
},
)
class BreastCancerClassifier:
bcc_model = bentoml.models.BentoModel("breast_cancer_model:latest")
def __init__(self) -> None:
self.model = joblib.load(self.bcc_model.path_of("model.pkl"))
@bentoml.api
def predict(self, input: np.ndarray = Field(examples=[[7.76, 47.92, 181, 0, 9.456, 59.16, 268.6, 0]])) -> str:
result = self.model.predict(input)
return 'M' if result[0] else 'B'
### bentoml config
# <https://docs.bentoml.com/en/latest/reference/bentoml/bento-build-options.html>
service: "bentoApi.py:BreastCancerClassifier"
labels:
owner: Glorfindel
project: BreastCancerClassifier
include:
- "bentoApi.py"
python:
lock_packages: false
requirements_txt: "requirements.txt"
no_index: false
trusted_host:
- "pypi.python.org"
- "pypi.org"
- "files.pythonhosted.org"
docker:
distro: debian
python_version: "3.11"
When I try to launch docker run I received this error:
docker run -p 3000:3000 cr/breast-cancer-service:breast-cancer-model-1
2025-04-01T09:31:35+0000 [INFO] [cli] Service loaded from Bento directory: <Service name='BreastCancerClassifier'>
2025-04-01T09:31:35+0000 [INFO] [cli] Service loaded from Bento directory: <Service name='BreastCancerClassifier'>
Error: [serve] `serve` failed: No cloud context default found
Can you help me?Jian Shen Yap
04/01/2025, 10:57 AMBentoModelMatteo Bernardon
04/01/2025, 12:06 PMMatteo Bernardon
04/01/2025, 12:33 PMspec:
containers:
- args:
- . /app/.venv/bin/activate && bentoml serve
command:
- /bin/bash
- '-c'
In this way I reproduced the errorMatteo Bernardon
04/01/2025, 12:37 PM2025-04-01T12:36:51+0000 [DEBUG] [cli] Loading bento "breast_cancer_classifier:pp5fduqo46bt47so" from directory: /home/bentoml/bento
2025-04-01T12:36:51+0000 [DEBUG] [cli] Importing service "bentoApi:BreastCancerClassifier" from working dir: "/home/bentoml/bento/src"
2025-04-01T12:36:51+0000 [INFO] [cli] Service loaded from Bento directory: <Service name='BreastCancerClassifier'>
2025-04-01T12:36:51+0000 [DEBUG] [cli] Loading bento "breast_cancer_classifier:pp5fduqo46bt47so" from directory: /home/bentoml/bento
2025-04-01T12:36:51+0000 [DEBUG] [cli] Importing service "bentoApi:BreastCancerClassifier" from working dir: "/home/bentoml/bento/src"
2025-04-01T12:36:51+0000 [INFO] [cli] Service loaded from Bento directory: <Service name='BreastCancerClassifier'>
2025-04-01T12:36:51+0000 [DEBUG] [cli] GPU not detected. Unable to initialize pynvml lib.
2025-04-01T12:36:51+0000 [DEBUG] [cli] Tracking Error: No cloud context default found
Error: [serve] `serve` failed: No cloud context default found
Traceback (most recent call last):
File "/app/.venv/bin/bentoml", line 10, in <module>
sys.exit(cli())
^^^^^
File "/app/.venv/lib/python3.11/site-packages/click/core.py", line 1161, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/click/core.py", line 1082, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/click/core.py", line 1697, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/click/core.py", line 1443, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/click/core.py", line 788, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml_cli/utils.py", line 369, in wrapper
raise err from None
File "/app/.venv/lib/python3.11/site-packages/bentoml_cli/utils.py", line 364, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml_cli/utils.py", line 335, in wrapper
return_value = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml_cli/env_manager.py", line 126, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml_cli/serve.py", line 313, in serve
serve_http(
File "/app/.venv/lib/python3.11/site-packages/simple_di/__init__.py", line 139, in _
return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/_bentoml_impl/server/serving.py", line 228, in serve_http
server_on_deployment(svc)
File "/app/.venv/lib/python3.11/site-packages/simple_di/__init__.py", line 139, in _
return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/_bentoml_impl/server/serving.py", line 141, in server_on_deployment
model.to_model().resolve()
File "/app/.venv/lib/python3.11/site-packages/simple_di/__init__.py", line 139, in _
return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/_bentoml_sdk/models/base.py", line 136, in resolve
cloud_client = BentoMLContainer.bentocloud_client.get()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/simple_di/__init__.py", line 72, in get
return self._provide()
^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/simple_di/providers.py", line 124, in _provide
value = super()._provide()
^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/simple_di/providers.py", line 103, in _provide
return inject(self._func)(
^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/simple_di/__init__.py", line 139, in _
return func(*_inject_args(bind.args), **_inject_kwargs(bind.kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml/_internal/configuration/containers.py", line 510, in bentocloud_client
return BentoCloudClient.for_context(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml/_internal/cloud/__init__.py", line 67, in for_context
ctx = cfg.get_context(context)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/bentoml/_internal/cloud/config.py", line 84, in get_context
raise CloudRESTApiClientError(
bentoml.exceptions.CloudRESTApiClientError: No cloud context default foundFrost Ming
04/01/2025, 1:01 PMMatteo Bernardon
04/01/2025, 1:01 PMFrost Ming
04/01/2025, 2:20 PMbentoml build and bentoml containerize to get the image?Matteo Bernardon
04/01/2025, 2:25 PMbentoml build
echo "------------------------------ Complete bentoml bundle"
CLASS_NAME=$(ls -t ~/bentoml/bentos | head -1)
CLASS_NAME=${CLASS_NAME%/}
echo "CLASS_NAME: $CLASS_NAME"
SAVED_PATH=$(bentoml get $CLASS_NAME:latest -o path)
mkdir /workspace/build-image
echo "bentoml bundle on $SAVED_PATH"
echo "------------------------------ Prepare data"
cp $SAVED_PATH/env/docker/Dockerfile $SAVED_PATH/Dockerfile
cd $SAVED_PATH
tar -cvzf "$CLASS_NAME.tar.gz" .
cp $CLASS_NAME.tar.gz /workspace/build-image/$CLASS_NAME.tar.gz
and then I use buildah to create image:
cd /workspace/build-image
CONTEXT=$(cat /workspace/build-image/CONTEXT)
echo "CONTEXT: $CONTEXT"
mkdir $BENTOML_BUILD_DIR
tar -xzf $CONTEXT -C $BENTOML_BUILD_DIR
IMAGE=$BENTOML_CR_REPO/$(params.image-id)
echo "IMAGE: $IMAGE"
echo "------------------------------ Start Buildah executor with bentoml image"
CERT_DIR_FLAG="--cert-dir /root/ssl/certs"
buildah ${CERT_DIR_FLAG} bud --no-cache --file $BENTOML_BUILD_DIR/Dockerfile --storage-driver vfs --format docker --tag $IMAGE $BENTOML_BUILD_DIR
buildah ${CERT_DIR_FLAG} push --authfile /root/push-destination-auth --storage-driver vfs --digestfile /tmp/image-digest $IMAGE
cat /tmp/image-digest | tee $(results.IMAGE_DIGEST.path)
echo -e -n "\n$IMAGE\n" | tee $(results.IMAGE_URL.path)Matteo Bernardon
04/01/2025, 2:27 PMMatteo Bernardon
04/01/2025, 2:27 PMFrost Ming
04/02/2025, 1:57 AMbentoml containerize you may need to adjust your workflowMatteo Bernardon
04/02/2025, 6:28 AMMatteo Bernardon
04/02/2025, 6:29 AMFrost Ming
04/02/2025, 6:43 AMMatteo Bernardon
04/02/2025, 6:46 AMbuild containerize.. container in a container.. I can specify buildah/kaniko executor instead docker for example?Frost Ming
04/02/2025, 8:34 AM