-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release-1.16] fix ML example (#6184)
* fix ML example * fix dockerize * type variable df * fix code --------- Co-authored-by: Yasuhiro Matsumoto <[email protected]>
- Loading branch information
1 parent
9de70f0
commit 4e53037
Showing
3 changed files
with
121 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 21 additions & 9 deletions
30
code-samples/community/serving/machinelearning-python-bentoml/iris_classifier.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
from bentoml import env, artifacts, api, BentoService | ||
from bentoml.handlers import DataframeHandler | ||
from bentoml.artifact import SklearnModelArtifact | ||
import numpy as np | ||
import bentoml | ||
from pydantic import Field | ||
from bentoml.validators import Shape | ||
from typing_extensions import Annotated | ||
import joblib | ||
|
||
@env(auto_pip_dependencies=True) | ||
@artifacts([SklearnModelArtifact('model')]) | ||
class IrisClassifier(BentoService): | ||
|
||
@api(DataframeHandler) | ||
def predict(self, df): | ||
return self.artifacts.model.predict(df) | ||
@bentoml.service | ||
class IrisClassifier: | ||
iris_model = bentoml.models.get("iris_sklearn:latest") | ||
|
||
def __init__(self): | ||
self.model = joblib.load(self.iris_model.path_of("model.pkl")) | ||
|
||
@bentoml.api | ||
def predict( | ||
self, | ||
df: Annotated[np.ndarray, Shape((-1, 4))] = Field( | ||
default=[[5.2, 2.3, 5.0, 0.7]] | ||
), | ||
) -> np.ndarray: | ||
return self.model.predict(df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters