{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Deploy an MLflow model with SageMaker" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install MLflow" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -q mlflow==2.0.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup environment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import boto3\n", "import mlflow\n", "import sagemaker\n", "import pandas as pd\n", "import mlflow.sagemaker\n", "from sklearn.datasets import load_boston\n", "from mlflow.deployments import get_deploy_client\n", "\n", "# name of the AWS region to which to deploy the application\n", "region = sagemaker.Session().boto_region_name\n", "# we are using the notebook instance role for training in this example\n", "role = sagemaker.get_execution_role() \n", "# uri of your remote mlflow server\n", "tracking_uri = '' \n", "# set remote mlflow server\n", "mlflow.set_tracking_uri(tracking_uri)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build MLflow docker image to serve the model with SageMaker " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!mlflow sagemaker build-and-push-container" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# URL of the ECR-hosted Docker image the model should be deployed into\n", "image_uri = ''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Deploy a SageMaker endpoint with our scikit-learn model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "endpoint_name = 'boston-housing'\n", "# The location, in URI format, of the MLflow model to deploy to SageMaker.\n", "model_uri = 'models:/boston/1'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "config={\n", " 'execution_role_arn': role,\n", " 'image_url': image_uri,\n", " 'instance_type': 'ml.m5.xlarge',\n", " 'instance_count': 1, \n", " 'region_name': region\n", "}\n", "\n", "client = get_deploy_client(\"sagemaker\")\n", "\n", "client.create_deployment(\n", " name=endpoint_name,\n", " model_uri=model_uri,\n", " flavor='python_function',\n", " config=config\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Predict" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# load boston dataset\n", "data = load_boston()\n", "df = pd.DataFrame(data.data, columns=data.feature_names)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "client = get_deploy_client(f\"sagemaker:/{region}\")\n", "\n", "payload = df.iloc[[0]]\n", "prediction = client.predict(endpoint_name, df.iloc[[0]])\n", "\n", "print(f'Payload: {payload}')\n", "print(f'Prediction: {prediction}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete endpoint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "client.delete_deployment(endpoint_name, config=config)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_python3" }, "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.8.12" }, "notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." }, "nbformat": 4, "nbformat_minor": 2 }