Created
November 5, 2019 14:10
-
-
Save ldrewniak/e6a87811b34090e5c9bba0831a4090df 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": [ | |
"# 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": [ | |
"# get latest model, the one with highest version indicated by v<versionNumber> in the end of the model name\n", | |
"models = glob('models/*')\n", | |
"models_versions = [(float(x[x.rfind('v')+1:-4]), x) for x in models]\n", | |
"lastVersion = max(models_versions)[1]" | |
] | |
}, | |
{ | |
"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(lastVersion, 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 = lastVersion[lastVersion.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()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# save the scoring endpoint in the file, just for convince of not scrolling all the way down in logs\n", | |
"with open('scoring_path', 'w') as f: \n", | |
" f.write(webservice.scoring_uri) " | |
] | |
} | |
], | |
"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