jccantre
10/1/2019 - 6:10 PM

Create the Scoring Script

%%writefile score.py
import json
import numpy as np
import os
import pickle
import pandas as pd
from sklearn.externals import joblib
from sklearn.svm import SVC

from inference_schema.schema_decorators import input_schema, output_schema
from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType
from inference_schema.parameter_types.pandas_parameter_type import PandasParameterType

from azureml.core.model import Model

def init():
    global model
    #model = joblib.load('recommender.pkl')
    model_path = Model.get_model_path('fwrk')
    model = joblib.load(model_path)

input_sample = pd.DataFrame(data=[{
              "input_name_1": 5,         # This is a decimal type sample. Use the data type that reflects this column in your data
              "input_name_2": 5,    # This is a string type sample. Use the data type that reflects this column in your data
              "input_name_3": 3            # This is a integer type sample. Use the data type that reflects this column in your data
            }])

output_sample = np.array([0])              # This is a integer type sample. Use the data type that reflects the expected result

@input_schema('data', PandasParameterType(input_sample))
@output_schema(NumpyParameterType(output_sample))

def run(data):
    try:
        result = model.predict(data)
        # you can return any datatype as long as it is JSON-serializable
        return result.tolist()
    except Exception as e:
        error = str(e)
        return error