Skip to content

Commit

Permalink
add service code to README
Browse files Browse the repository at this point in the history
  • Loading branch information
bojiang committed Nov 3, 2022
1 parent 3edeac8 commit fce8ecc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
54 changes: 53 additions & 1 deletion examples/monitoring/task_classification/README.md
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions examples/monitoring/task_classification/service.py
Expand Up @@ -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])


Expand Down Expand Up @@ -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

0 comments on commit fce8ecc

Please sign in to comment.