diff --git a/examples/monitoring/task_classification/README.md b/examples/monitoring/task_classification/README.md index 2a67918a97..6e9357b141 100644 --- a/examples/monitoring/task_classification/README.md +++ b/examples/monitoring/task_classification/README.md @@ -34,7 +34,59 @@ bentoml.sklearn.save_model("iris_clf", clf) ``` ### Serving the model -Copy this to a `service.py` file, and run your service with Bento Server locally: +Draft a `service.py` file with monitoring data collection lines, and run your service with Bento Server locally: + +```python +import numpy as np + +import bentoml +from bentoml.io import NumpyNdarray + +CLASS_NAMES = ["setosa", "versicolor", "virginica"] + +iris_clf_runner = bentoml.sklearn.get("iris_clf:latest").to_runner() +svc = bentoml.Service("iris_classifier", runners=[iris_clf_runner]) + + +@svc.api( + input=NumpyNdarray.from_sample(np.array([[4.9, 3.0, 1.4, 0.2]], dtype=np.double)), + output=NumpyNdarray(), +) +async def classify(input_series: np.ndarray) -> np.ndarray: + with bentoml.monitor("iris_classifier_prediction") as mon: + mon.log( + data=input_series[0][0], + name="sepal length", + role="feature", + data_type="numerical", + ) + mon.log( + input_series[0][1], + name="sepal width", + role="feature", + data_type="numerical", + ) + mon.log( + input_series[0][2], + name="petal length", + role="feature", + data_type="numerical", + ) + mon.log( + input_series[0][3], + name="petal width", + role="feature", + data_type="numerical", + ) + result = await iris_clf_runner.predict.async_run(input_series) + mon.log( + CLASS_NAMES[result[0]], + name="pred", + role="prediction", + data_type="categorical", + ) + return result +``` ```bash bentoml serve service.py:svc --reload diff --git a/examples/monitoring/task_classification/service.py b/examples/monitoring/task_classification/service.py index bdb6221284..fbd6102d6a 100644 --- a/examples/monitoring/task_classification/service.py +++ b/examples/monitoring/task_classification/service.py @@ -3,8 +3,9 @@ import bentoml from bentoml.io import NumpyNdarray -iris_clf_runner = bentoml.sklearn.get("iris_clf:latest").to_runner() +CLASS_NAMES = ["setosa", "versicolor", "virginica"] +iris_clf_runner = bentoml.sklearn.get("iris_clf:latest").to_runner() svc = bentoml.Service("iris_classifier", runners=[iris_clf_runner]) @@ -39,5 +40,10 @@ async def classify(input_series: np.ndarray) -> np.ndarray: data_type="numerical", ) result = await iris_clf_runner.predict.async_run(input_series) - mon.log(result[0], name="pred", role="prediction", data_type="categorical") + mon.log( + CLASS_NAMES[result[0]], + name="pred", + role="prediction", + data_type="categorical", + ) return result