{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Copyright (c) Microsoft Corporation. All rights reserved.\n",
        "\n",
        "Licensed under the MIT License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "![Impressions](https://PixelServer20190423114238.azurewebsites.net/api/impressions/MachineLearningNotebooks/how-to-use-azureml/using-mlflow/deploy-model/deploy-model.png)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Deploy Model as Azure Machine Learning Web Service using MLflow\n",
        "\n",
        "This example shows you how to use mlflow together with Azure Machine Learning services for deploying a model as a web service. You'll learn how to:\n",
        "\n",
        " 1. Retrieve a previously trained scikit-learn model\n",
        " 2. Create a Docker image from the model\n",
        " 3. Deploy the model as a web service on Azure Container Instance\n",
        " 4. Make a scoring request against the web service.\n",
        "\n",
        "## Prerequisites and Set-up\n",
        "\n",
        "This notebook requires you to first complete the [Use MLflow with Azure Machine Learning for Local Training Run](../train-local/train-local.ipnyb) or [Use MLflow with Azure Machine Learning for Remote Training Run](../train-remote/train-remote.ipnyb) notebook, so as to have an experiment run with uploaded model in your Azure Machine Learning Workspace.\n",
        "\n",
        "Also install following packages if you haven't already\n",
        "\n",
        "```\n",
        "pip install azureml-mlflow pandas\n",
        "```\n",
        "\n",
        "Then, import necessary packages:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import mlflow\n",
        "import azureml.mlflow\n",
        "import azureml.core\n",
        "from azureml.core import Workspace\n",
        "\n",
        "# Check core SDK version number\n",
        "print(\"SDK version:\", azureml.core.VERSION)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Connect to workspace and set MLflow tracking URI\n",
        "\n",
        "Setting the tracking URI is required for retrieving the model and creating an image using the MLflow APIs."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ws = Workspace.from_config()\n",
        "\n",
        "mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Retrieve model from previous run\n",
        "\n",
        "Let's retrieve the experiment from training notebook, and list the runs within that experiment."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "experiment_name = \"experiment-with-mlflow\"\n",
        "exp = ws.experiments[experiment_name]\n",
        "\n",
        "runs = list(exp.get_runs())\n",
        "runs"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then, let's select the most recent training run and find its ID. You also need to specify the path in run history where the model was saved. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "runid = runs[0].id\n",
        "model_save_path = \"model\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Create Docker image\n",
        "\n",
        "To create a Docker image with Azure Machine Learning for Model Management, use ```mlflow.azureml.build_image``` method. Specify the model path, your workspace, run ID and other parameters.\n",
        "\n",
        "MLflow automatically recognizes the model framework as scikit-learn, and creates the scoring logic and includes library dependencies for you.\n",
        "\n",
        "Note that the image creation can take several minutes."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import mlflow.azureml\n",
        "\n",
        "azure_image, azure_model = mlflow.azureml.build_image(model_uri=\"runs:/{}/{}\".format(runid, model_save_path),\n",
        "                                                      workspace=ws,\n",
        "                                                      model_name='diabetes-sklearn-model',\n",
        "                                                      image_name='diabetes-sklearn-image',\n",
        "                                                      synchronous=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Deploy web service\n",
        "\n",
        "Let's use Azure Machine Learning SDK to deploy the image as a web service. \n",
        "\n",
        "First, specify the deployment configuration. Azure Container Instance is a suitable choice for a quick dev-test deployment, while Azure Kubernetes Service is suitable for scalable production deployments.\n",
        "\n",
        "Then, deploy the image using Azure Machine Learning SDK's ```deploy_from_image``` method.\n",
        "\n",
        "Note that the deployment can take several minutes."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from azureml.core.webservice import AciWebservice, Webservice\n",
        "\n",
        "\n",
        "aci_config = AciWebservice.deploy_configuration(cpu_cores=1, \n",
        "                                                memory_gb=1, \n",
        "                                                tags={\"method\" : \"sklearn\"}, \n",
        "                                                description='Diabetes model',\n",
        "                                                location='eastus2')\n",
        "\n",
        "\n",
        "# Deploy the image to Azure Container Instances (ACI) for real-time serving\n",
        "webservice = Webservice.deploy_from_image(\n",
        "    image=azure_image, workspace=ws, name=\"diabetes-model-1\", deployment_config=aci_config)\n",
        "\n",
        "\n",
        "webservice.wait_for_deployment(show_output=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Make a scoring request\n",
        "\n",
        "Let's take the first few rows of test data and score them using the web service"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "test_rows = [\n",
        "    [0.01991321,  0.05068012,  0.10480869,  0.07007254, -0.03596778,\n",
        "     -0.0266789 , -0.02499266, -0.00259226,  0.00371174,  0.04034337],\n",
        "    [-0.01277963, -0.04464164,  0.06061839,  0.05285819,  0.04796534,\n",
        "     0.02937467, -0.01762938,  0.03430886,  0.0702113 ,  0.00720652],\n",
        "    [ 0.03807591,  0.05068012,  0.00888341,  0.04252958, -0.04284755,\n",
        "     -0.02104223, -0.03971921, -0.00259226, -0.01811827,  0.00720652]]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "MLflow-based web service for scikit-learn model requires the data to be converted to Pandas DataFrame, and then serialized as JSON. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import json\n",
        "import pandas as pd\n",
        "\n",
        "test_rows_as_json = pd.DataFrame(test_rows).to_json(orient=\"split\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's pass the conveted and serialized data to web service to get the predictions."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "predictions = webservice.run(test_rows_as_json)\n",
        "\n",
        "print(predictions)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can use the web service's scoring URI to make a raw HTTP request"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "webservice.scoring_uri"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can diagnose the web service using ```get_logs``` method."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "webservice.get_logs()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Next Steps\n",
        "\n",
        "Learn about [model management and inference in Azure Machine Learning service](https://docs.microsoft.com/en-us/azure/machine-learning/service/concept-model-management-and-deployment)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "authors": [
      {
        "name": "rastala"
      }
    ],
    "kernelspec": {
      "display_name": "Python 3.6",
      "language": "python",
      "name": "python36"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}