Table of contents

Project description

[back to the top]

This sample project shows off how to prepare and deploy to Azure Web Apps a simple Python web service with an image classifying model produced in CNTK (Cognitive Toolkit) using FasterRCNN

Results

[back to the top]

Website Demo:
Demo

Sample request and response in Postman: Demo

Deployment steps

Setup

[back to the top]

  1. Download content of this repo

    You can either clone this repo or just download it and unzip to some folder

  2. Setup Python environment

    In order for scripts to work you should have a proper Python environment. If you don't already have it setup then you should follow one of the online tutorials. To setup Python environment and all the dependencies required by CNTK on my local Windows machine I used this tutorial

  3. Download CNTK model and class map file

    Go to /CNTKModels folder in the location were you unzipped this repo and run download_model.py. It will automatically download the pretrained model and class map file required for our evaluation to run properly.

  4. Install Azure CLI tools

    If you don't have it then you can easily do it by openning Windows Command Prompt and running this command:

    pip install azure-cli
  5. Get Azure subscription

    If you don't own any Azure subscriptions you can always create a new free trial with $200 credits to spend

Deploy demo

[back to the top]

  1. Set variables

    Open Command Prompt to the location where you unzipped the contents of this repository (for example: cd C:\Poligon\WebService) and type in as follows (but make sure to replace the [] with a proper value):

    set uname=[username]
    set pass=[password]
    set appn=[web_app_name]
    set rgname=[resource_group_name]
  2. Login to Azure

    In the same CMD type in:

    az login

    You should see something like this:

    AZ login

    Now go to the https://aka.ms/devicelogin website and type in the code:

    Device login website

    You will then be asked to login with an email connected to your Azure subscription

    If everything goes ok you should see the verification message on the website and in console you should see a list of your Azure subscriptions

  3. Setup deployment credentials

    We're setting this up to later be able to remotely deploy code to our Azure Web App

    az webapp deployment user set --user-name %uname% --password %pass%
  4. Create resource group

    Resource groups help you to better manage your stuff in subscription and it's a basic method of deploying services to Azure. Read more here

    az group create --location westeurope --name %rgname%
  5. Create new Azure App Service Plan and new Azure Web App

    az appservice plan create --name %appn% --resource-group %rgname% --sku S1
    az webapp create --name %appn% --resource-group %rgname% --plan %appn%
  6. Configure Azure Web App and add Python extension

    Azure Web Apps by default support only Python 2.7 and 3.4. Because I used Python 3.5 I had to use special extension to setup the environment

    First you need to change some Application Settings on your Web App (the pink ones): Web App preferences Changing Platform is required and changing Always On is optional but I recommend to use it so that our web service stays awake even if not used.

    After we properly save Application Settings we can now add Python 3.5.x extension. In order to this, just type in extensions into the search box Extensions

    And then simply add new extension Extensions

    It should take around a minute or two to properly install the extension Extensions

  7. Setup deployment source for newly created Azure Web App

    This code will not only setup the deployment source for your app but will also retrive the URL you will need in next steps

    az webapp deployment source config-local-git --name %appn% --resource-group %rgname% --query url --output tsv

    It should return something like this: Remote repo 1

  8. Initialize git and add remote repository

    Make sure to replace [remote_repo_address] with the URL returned in step number 7.

    git init
    git remote add azure [remote_repo_address]

    Command with URL should look like this: Remote repo 2

  9. Push application to Azure Web App remote repository

    Last step is to simply push our applications code to Azure Web App

    git add -A
    git commit -m "init"
    git push azure master

    This will trigger our custom deployment script, copy all the files, setup Python environment and install all the required dependencies from requirements.txt file

  10. Test the application

    If everything went smooth you should now have a running Python application and you should be able to test it. I used Postman to test HTTP requests and responses

    Website Demo:
    Demo

    Sample request and response in Postman: Demo

Code highlights

[back to the top]