%%writefile iris_classifier.py
#writing script for BentoML Service Class
from bentoml import env,artifacts,api, BentoService
from bentoml.artifact import SklearnModelArtifact
from bentoml.adapters import DataframeInput
@env(auto_pip_dependencies=True,
conda_dependencies=['bentoml'])
@artifacts([SklearnModelArtifact("model")])
class IrisClassifier(BentoService):
@api(input=DataframeInput())
def predict(self,df):
return self.artifact.model.predict(df)
Overwriting iris_classifier.py
%%writefile main.py
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from iris_classifier import IrisClassifier
if __name__=="__main__":
#load data
iris=load_iris()
X,y=iris.data, iris.target
#train model
clf=SVC()
clf.fit(X,y)
#create an iris classifier service instance
service=IrisClassifier()
#pack the newly trained service classifier
service.pack("model",clf)
# Save the prediction service to disk for model serving
saved_path=service.save()
Overwriting main.py
!python main.py
[2020-07-26 06:42:24,979] INFO - BentoService bundle 'IrisClassifier:20200726064151_392FB8' saved to: C:\Users\Yashika Sharma/bentoml/repository/IrisClassifier\20200726064151_392FB8
D:\Yashika Sharma\anaconda3\envs\bento\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first. warnings.warn( C:\Users\Yashika Sharma\AppData\Roaming\Python\Python38\site-packages\traitlets\config\loader.py:795: SyntaxWarning: "is" with a literal. Did you mean "=="? if len(key) is 1: C:\Users\Yashika Sharma\AppData\Roaming\Python\Python38\site-packages\traitlets\config\loader.py:804: SyntaxWarning: "is" with a literal. Did you mean "=="? if len(key) is 1:
import bentoml
loaded_iris=bentoml.load(saved_path)
D:\Yashika Sharma\anaconda3\envs\bento\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first. warnings.warn(
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-4-c8234edf732c> in <module> 1 import bentoml ----> 2 loaded_iris=bentoml.load(saved_path) NameError: name 'saved_path' is not defined
!bentoml get IrisClassifier:latest
!bentoml get --print-location IrisClassifier:latest
!bentoml serve IrisClassifier:latest
!cd {"saved_path"} && docker build --quiet -t iris-classifier-server .