Created
November 5, 2019 14:08
-
-
Save ldrewniak/9c678d45acaa75cc0e73ebd56d20415d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# import dependencies\n", | |
"import shutil\n", | |
"import os\n", | |
"from glob import glob\n", | |
"import re\n", | |
"import mlflow.azureml\n", | |
"from mlflow.keras import load_model\n", | |
"from azureml.core import Workspace\n", | |
"from azureml.core.webservice import Webservice\n", | |
"from azureml.core.authentication import ServicePrincipalAuthentication" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# please insert the path to model archive\n", | |
"model_zip_path = '<insert path to model here>'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# get all enviroment variables\n", | |
"account_name = os.getenv(\"account_name\")\n", | |
"account_key = os.getenv(\"account_key\")\n", | |
"workspace_name = os.getenv('workspace_name')\n", | |
"subscription_id = os.getenv('subscription_id')\n", | |
"resource_group = os.getenv('resource_group')\n", | |
"location = os.getenv('location')\n", | |
"tenant_id = os.getenv('tenant_id')\n", | |
"service_principal_id = os.getenv('service_principal_id')\n", | |
"service_principal_password = os.getenv('service_principal_password')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model_local_dir = 'model'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# unpack the model\n", | |
"shutil.unpack_archive(model_zip_path, model_local_dir)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# test if model is loaded properly\n", | |
"load_model(model_local_dir)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# rename as not all symbols are allowed by azure\n", | |
"fileName = model_zip_path[model_zip_path.find('/')+1:-4]\n", | |
"fileName = re.sub(r'[^-\\.a-z0-9]', '-', fileName.lower())\n", | |
"image_name = re.sub(r'\\W+', '', fileName) + '-image'\n", | |
"model_name = re.sub(r'\\W+', '', fileName) + '-model'\n", | |
"deploy_name = re.sub(r'\\W+', '', fileName) + '-deploy'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# create authorization object\n", | |
"auth = ServicePrincipalAuthentication(\n", | |
" tenant_id=tenant_id,\n", | |
" service_principal_id=service_principal_id,\n", | |
" service_principal_password= service_principal_password)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# create/get workspace\n", | |
"azure_workspace = Workspace.create(\n", | |
" name=workspace_name,\n", | |
" subscription_id=subscription_id,\n", | |
" resource_group=resource_group,\n", | |
" location=location,\n", | |
" create_resource_group=True,\n", | |
" exist_ok=True,\n", | |
" auth = auth)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# build the image and save in azure container registry\n", | |
"azure_image, azure_model = mlflow.azureml.build_image(\n", | |
" model_uri=model_local_dir,\n", | |
" workspace=azure_workspace,\n", | |
" synchronous=True,\n", | |
" image_name = image_name,\n", | |
" model_name = model_name)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# deploy container instance from the image\n", | |
"webservice = Webservice.deploy_from_image(\n", | |
" image = azure_image, \n", | |
" workspace=azure_workspace, \n", | |
" name=deploy_name)\n", | |
"webservice.wait_for_deployment()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "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.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment