Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_service_env_pip_dependencies(tmpdir):
@bentoml.env(pip_dependencies=['numpy', 'pandas', 'torch'])
class ServiceWithList(bentoml.BentoService):
@bentoml.api(DataframeHandler)
def predict(self, df):
return df
service_with_list = ServiceWithList()
saved_path = service_with_list.save(str(tmpdir))
requirements_txt_path = os.path.join(saved_path, 'requirements.txt')
with open(requirements_txt_path, 'rb') as f:
saved_requirements = f.read()
module_list = saved_requirements.decode('utf-8').split('\n')
assert 'numpy' in module_list
assert 'pandas' in module_list
assert 'torch' in module_list
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
from sklearn import svm
from sklearn import datasets
from bentoml import BentoService, load, api, env, artifacts, ver
from bentoml.artifact import PickleArtifact
from bentoml.handlers import JsonHandler
@artifacts([PickleArtifact('clf')])
@env(pip_dependencies=["scikit-learn"])
@ver(major=1, minor=0)
class IrisClassifier(BentoService):
"""
Iris SVM Classifier
"""
@api(JsonHandler)
def predict(self, parsed_json):
return self.artifacts.clf.predict(parsed_json)
if __name__ == "__main__":
clf = svm.SVC(gamma='scale')
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf.fit(X, y)
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)
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 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)
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))
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]
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)