Exemplo n.º 1
0
def init():
    global model
    # This name is model.id of model that we want to deploy deserialize the model file back
    # into a sklearn model
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'model.pkl')
    path = os.path.normpath(model_path)
    path_split = path.split(os.sep)
    log_server.update_custom_dimensions({
        'model_name': path_split[-3],
        'model_version': path_split[-2]
    })
    try:
        logger.info("Loading model from path.")
        model = joblib.load(model_path)
        logger.info("Loading successful.")
    except Exception as e:
        logging_utilities.log_traceback(e, logger)
        raise


@input_schema('data', PandasParameterType(input_sample))
@output_schema(NumpyParameterType(output_sample))
def run(data):
    try:
        result = model.predict(data)
        return json.dumps({"result": result.tolist()})
    except Exception as e:
        result = str(e)
        return json.dumps({"error": result})
import pandas as pd
import joblib

import azureml.automl.core
from azureml.automl.core.shared import logging_utilities, log_server
from azureml.telemetry import INSTRUMENTATION_KEY

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 inference_schema.parameter_types.standard_py_parameter_type import StandardPythonParameterType

data_sample = PandasParameterType(pd.DataFrame({"age": pd.Series([0.0], dtype="float64"), "anaemia": pd.Series([0], dtype="int64"), "creatinine_phosphokinase": pd.Series([0], dtype="int64"), "diabetes": pd.Series([0], dtype="int64"), "ejection_fraction": pd.Series([0], dtype="int64"), "high_blood_pressure": pd.Series([0], dtype="int64"), "platelets": pd.Series([0.0], dtype="float64"), "serum_creatinine": pd.Series([0.0], dtype="float64"), "serum_sodium": pd.Series([0], dtype="int64"), "sex": pd.Series([0], dtype="int64"), "smoking": pd.Series([0], dtype="int64"), "time": pd.Series([0], dtype="int64")}))
input_sample = StandardPythonParameterType({'data': data_sample})

result_sample = NumpyParameterType(np.array([0]))
output_sample = StandardPythonParameterType({'Results':result_sample})

try:
    log_server.enable_telemetry(INSTRUMENTATION_KEY)
    log_server.set_verbosity('INFO')
    logger = logging.getLogger('azureml.automl.core.scoring_script_v2')
except:
    pass


def init():
    global model
    # This name is model.id of model that we want to deploy deserialize the model file back
    # into a sklearn model
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'model.pkl')
Exemplo n.º 3
0
    # variables to monitor model input and output data
    inputs_dc = ModelDataCollector("Support vector classifier model",
                                   designation="inputs",
                                   feature_names=[
                                       "feat1", "feat2", "feat3", "feat4",
                                       "feat5", "feat6", "feat7"
                                   ])
    prediction_dc = ModelDataCollector("Support vector classifier model",
                                       designation="predictions",
                                       feature_names=["weatherprediction"])


@input_schema('data',
              NumpyParameterType(
                  np.array([[34.927778, 0.24, 7.3899, 83, 16.1000, 1016.51,
                             1]])))
@output_schema(NumpyParameterType(np.array([0])))
def run(data):
    try:
        data = scaler.fit_transform(data.reshape(1, 7))
        inputs_dc.collect(data)

        # model inference
        result = model.run([label_name],
                           {input_name: data.astype(np.float32)})[0]
        # this call is saving model output data into Azure Blob
        prediction_dc.collect(result)

    except Exception as e:
        result = 'error'
Exemplo n.º 4
0
            if '.pkl' in file:
                model_path = os.path.join(path, file)
    if model_path is None:
        raise ValueError(".pkl model not found")
    model = joblib.load(model_path)


input_sample = numpy.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                            [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]])
output_sample = numpy.array([5021.509689995557, 3693.645386402646])


# Inference_schema generates a schema for your web service
# It then creates an OpenAPI (Swagger) specification for the web service
# at http://<scoring_base_url>/swagger.json
@input_schema('data', NumpyParameterType(input_sample))
@output_schema(NumpyParameterType(output_sample))
def run(data, request_headers):
    result = model.predict(data)

    # Demonstrate how we can log custom data into the Application Insights
    # traces collection.
    # The 'X-Ms-Request-id' value is generated internally and can be used to
    # correlate a log entry with the Application Insights requests collection.
    # The HTTP 'traceparent' header may be set by the caller to implement
    # distributed tracing (per the W3C Trace Context proposed specification)
    # and can be used to correlate the request to external systems.
    print(('{{"RequestId":"{0}", '
           '"TraceParent":"{1}", '
           '"NumberOfPredictions":{2}}}').format(
               request_headers.get("X-Ms-Request-Id", ""),
Exemplo n.º 5
0
    model_path = os.path.join(os.environ.get("AZUREML_MODEL_DIR"),
                              model_file_name)
    model = joblib.load(model_path)
    inputs_dc = ModelDataCollector(
        "sample-model",
        designation="inputs",
        feature_names=["feat1", "feat2", "feat3", "feat4"])
    prediction_dc = ModelDataCollector("sample-model",
                                       designation="predictions",
                                       feature_names=["prediction"])


# The run() method is called each time a request is made to the scoring API.
# Shown here are the optional input_schema and output_schema decorators
# from the inference-schema pip package. Using these decorators on your
# run() method parses and validates the incoming payload against
# the example input you provide here. This will also generate a Swagger
# API document for your web service.
@input_schema('data', NumpyParameterType(np.array([[0.1, 1.2, 2.3, 3.4]])))
@output_schema(StandardPythonParameterType({'predict': [['Iris-virginica']]}))
def run(data):
    # Use the model object loaded by init().
    result = model.predict(data)
    inputs_dc.collect(
        data)  #this call is saving our input data into Azure Blob
    prediction_dc.collect(
        result)  #this call is saving our input data into Azure Blob

    # You can return any JSON-serializable object.
    return {"predict": result.tolist()}