import json
import numpy
from azureml.core.model import Model
import joblib


def init():
    global model

    # load the model from file into a global object
    model_path = Model.get_model_path(
        model_name="sklearn_regression_model.pkl")
    model = joblib.load(model_path)


def run(raw_data):
    data = json.loads(raw_data)["data"]
    data = numpy.array(data)
    result = model.predict(data)
    return {"result": result.tolist()}


if __name__ == "__main__":
    # Test scoring
    init()
    test_row = '{"data":[[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1]]}'
    prediction = run(test_row)
    print("Test result: ", prediction)
