<jupyter_start><jupyter_text>V2 Model Server (SKLearn)<jupyter_code>import mlrun<jupyter_output>> 2020-10-12 14:52:57,535 [warning] Failed resolving version info. Ignoring and using defaults<jupyter_text>Configuration and package dependencies<jupyter_code>%nuclio config kind="serving"
%nuclio config spec.build.baseImage = "mlrun/mlrun"
%%nuclio cmd -c
python -m pip install numpy cloudpickle v3io sklearn<jupyter_output><empty_output><jupyter_text>Serving class code<jupyter_code>from cloudpickle import load
import numpy as np
from typing import List
class ClassifierModel(mlrun.serving.V2ModelServer):
    def load(self):
        """load and initialize the model and/or other elements"""
        model_file, extra_data = self.get_model('.pkl')
        self.model = load(open(model_file, 'rb'))

    def predict(self, body: dict) -> List:
        """Generate model predictions from sample."""
        feats = np.asarray(body['inputs'])
        result: np.ndarray = self.model.predict(feats)
        return result.tolist()
# nuclio: end-code<jupyter_output><empty_output><jupyter_text>Convert to function object The sklearn-project generated one or more models that will be deployed in the server project `sklearn-servers`<jupyter_code>models_path = 'https://s3.wasabisys.com/iguazio/models/iris/model.pkl'<jupyter_output><empty_output><jupyter_text>Document and save (as a template)<jupyter_code>fn = mlrun.code_to_function('v2-model-server', description="generic sklearn model server",
                     categories=['serving', 'ml'],
                     labels={'author': 'yaronh', 'framework': 'sklearn'},
                     code_output='.')
fn.spec.default_class = 'ClassifierModel'
#print(fn.to_yaml())
fn.export()<jupyter_output>> 2020-10-12 14:47:46,331 [info] function spec saved to path: function.yaml<jupyter_text>Configure and add model(s)<jupyter_code>fn.add_model('mymodel', model_path=models_path)
#fn.verbose = True<jupyter_output><empty_output><jupyter_text>Test models locally (using a server emulator)<jupyter_code># create an emulator (mock server) from the function configuration)
server = fn.to_mock_server(globals())<jupyter_output><empty_output><jupyter_text>Test against the iris dataset<jupyter_code>from sklearn.datasets import load_iris
iris = load_iris()
x = iris['data'].tolist()
result = server.test("/v2/models/mymodel/infer", {"inputs": x})
result.keys()<jupyter_output>> 2020-10-12 14:44:19,297 [debug] router run model mymodel, op=infer
> 2020-10-12 14:44:19,297 [debug] router run model mymodel, op=infer<jupyter_text>Deploy server<jupyter_code>fn.apply(mlrun.mount_v3io())
fn.deploy(project='v2-srv')<jupyter_output>> 2020-10-12 14:44:20,662 [info] deploy started
[nuclio] 2020-10-12 14:44:21,772 (info) Build complete
[nuclio] 2020-10-12 14:44:29,852 (info) Function deploy complete
[nuclio] 2020-10-12 14:44:29,859 done updating v2-srv-v2-model-server, function address: 192.168.3.11:30830<jupyter_text>Test server<jupyter_code>my_data = '''{"inputs":[[5.1, 3.5, 1.4, 0.2],[7.7, 3.8, 6.7, 2.2]]}'''
fn.invoke('/v2/models/mymodel/infer', my_data)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1361743031079782, 'fraction_numerical': 0.05671259211791092, 'mean_word_length': 5.6995708154506435, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 43, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '56588', 'license': 'Apache-2.0', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1091, 'n_words': 230, 'path': 'examples/v2_model_server.ipynb', 'repo_name': 'adiso75/mlrun', 'size': '3122'}