Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert os.path.exists(saved_path)
model_service = bentoml.load(saved_path)
assert len(model_service.get_service_apis()) == 1
api = model_service.get_service_apis()[0]
assert api.name == "predict"
assert isinstance(api.handler, DataframeHandler)
assert api.func(1) == 2
# Check api methods are available
assert model_service.predict(1) == 2
assert model_service.version == expected_version
class TestBentoWithOutArtifact(bentoml.BentoService):
@bentoml.api(DataframeHandler)
def test(self, df):
return df
def test_bento_without_artifact(tmpdir):
TestBentoWithOutArtifact().save_to_dir(str(tmpdir))
model_service = bentoml.load(str(tmpdir))
assert model_service.test(1) == 1
assert len(model_service.get_service_apis()) == 1
def test_save_duplicated_bento_exception_raised(tmpdir, test_bento_service_class):
test_model = TestModel()
svc = test_bento_service_class()
svc.pack("model", test_model)
def test_image_handler_pip_dependencies():
class TestImageService(bentoml.BentoService):
@bentoml.api(ImageHandler)
def test(self, image):
return image
service = TestImageService()
assert 'imageio' in service._env._pip_dependencies
def test_pip_dependencies_env():
@bentoml.env(pip_dependencies=["numpy"])
class ServiceWithString(bentoml.BentoService):
@bentoml.api(DataframeHandler)
def predict(self, df):
return df
service_with_string = ServiceWithString()
assert 'numpy' in service_with_string.env._pip_dependencies
@bentoml.env(pip_dependencies=['numpy', 'pandas', 'torch'])
class ServiceWithList(bentoml.BentoService):
@bentoml.api(DataframeHandler)
def predict(self, df):
return df
service_with_list = ServiceWithList()
assert 'numpy' in service_with_list.env._pip_dependencies
assert 'pandas' in service_with_list.env._pip_dependencies
assert 'torch' in service_with_list.env._pip_dependencies
import bentoml # noqa: E402
from bentoml.artifact import PickleArtifact # noqa: E402
BASE_TEST_PATH = "/tmp/bentoml-test"
class MyFakeModel(object):
def predict(self, df):
df['age'] = df['age'].add(5)
return df
@bentoml.artifacts([PickleArtifact('fake_model')])
@bentoml.env()
class MyFakeBentoModel(bentoml.BentoService):
"""
My RestServiceTestModel packaging with BentoML
"""
@bentoml.api(bentoml.handlers.DataframeHandler, input_columns_require=['age'])
def predict(self, df):
"""
predict expects dataframe as input
"""
return self.artifacts.fake_model.predict(df)
def generate_fake_dataframe_model():
"""
Generate a fake model, saved it to tmp and return saved_path
"""
from bentoml import BentoService, api, env, artifacts
from bentoml.artifact import SklearnModelArtifact
from bentoml.handlers import DataframeHandler
@artifacts([SklearnModelArtifact('model')])
@env(pip_dependencies=["scikit-learn"])
class IrisClassifier(BentoService):
@api(DataframeHandler)
def predict(self, df):
return self.artifacts.model.predict(df)
import pandas as pd
import bentoml
from bentoml.artifact import PickleArtifact
from bentoml.handlers import DataframeHandler
@bentoml.artifacts([PickleArtifact('model')])
@bentoml.env(pip_dependencies=['sklearn', 'numpy', 'pandas'])
class SentimentLRModel(bentoml.BentoService):
@bentoml.api(DataframeHandler, typ='series')
def predict(self, series):
"""
predict expects pandas.Series as input
"""
return self.artifacts.model.predict(series)
import numpy as np
from PIL import Image
from bentoml import api, artifacts, env, BentoService
from bentoml.artifact import KerasModelArtifact
from bentoml.handlers import ImageHandler
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
@env(pip_dependencies=['tensorflow==1.13.1', 'Pillow', 'numpy'])
@artifacts([KerasModelArtifact('classifier')])
class KerasFashionMnistService(BentoService):
@api(ImageHandler, pilmode='L')
def predict(self, img):
img = Image.fromarray(img).resize((28, 28))
img = np.array(img.getdata()).reshape((1,28,28,1))
class_idx = self.artifacts.classifier.predict_classes(img)[0]
return class_names[class_idx]
import pandas as pd
import numpy as np
from tensorflow import keras
from tensorflow.keras.preprocessing import sequence, text
from bentoml import api, env, BentoService, artifacts
from bentoml.artifact import KerasModelArtifact, PickleArtifact
from bentoml.handlers import JsonHandler
max_features = 1000
@artifacts([
KerasModelArtifact('model'),
PickleArtifact('word_index')
])
@env(conda_dependencies=['tensorflow', 'numpy', 'pandas'])
class TextClassificationService(BentoService):
def word_to_index(self, word):
if word in self.artifacts.word_index and self.artifacts.word_index[word] <= max_features:
return self.artifacts.word_index[word]
else:
return self.artifacts.word_index[""]
def preprocessing(self, text_str):
sequence = text.text_to_word_sequence(text_str)
return list(map(self.word_to_index, sequence))
@api(JsonHandler)
def predict(self, parsed_json):
if type(parsed_json) == list:
input_data = list(map(self.preprocessing, parsed_json))
else: # expecting type(parsed_json) == dict:
import pandas as pd
import numpy as np
from tensorflow import keras
from bentoml import api, env, BentoService, artifacts
from bentoml.artifact import TfKerasModelArtifact, PickleArtifact
from bentoml.handlers import JsonHandler
@artifacts([
TfKerasModelArtifact('model'),
PickleArtifact('word_index')
])
@env(requirements_txt="""tensorflow>=1.13.1
numpy>=1.16.2
pandas>=0.24.2
""")
class TextClassificationService(BentoService):
def word_to_index(self, word):
if word in self.artifacts.word_index:
return self.artifacts.word_index[word]
else:
return self.artifacts.word_index[""]
@api(JsonHandler)
def predict(self, parsed_json):
"""
"""
text = parsed_json['text']
sequence = keras.preprocessing.text.hashing_trick(
text,
256,
import pandas as pd
import bentoml
from bentoml.artifact import PickleArtifact
from bentoml.handlers import DataframeHandler
@bentoml.artifacts([PickleArtifact('sentiment_lr')])
@bentoml.env(pip_dependencies=["scikit-learn", "pandas"])
class SentimentLRModel(bentoml.BentoService):
@bentoml.api(DataframeHandler, typ='series')
def predict(self, series):
"""
predict expects pandas.Series as input
"""
return self.artifacts.sentiment_lr.predict(series)