Neural Network Workshop – Lab 10 Hosting Python in Azure

Login to your Azure Account

Create Account and Login

  1. Create a new Azure account if you do not already have one
  2. Login at portal.azurce.com

Reference Material

Please follow the Hints

 

Hint 1

Create a new Azure Account (if you don’t have one or desired):

[collapse]
Hint 2

Login to Azure:

  1. Go to https://portal.azure.com/
  2. Log in with the same credentials you used to create your Azure account, these could have been AAD Work credentials or Microsoft Account credentials

[collapse]

[collapse]

 

Create the Azure Resources and Configure

Create a Resource Group

  1. Create a new Resource Group named: workshop-rg

Reference Material

https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-portal

Full Solution

Follow these steps:

  1. Select Resource Group on the Menu
  2. Click Add
  3. Resource Group Name = workshop-rg
  4. Resource Group Location = Central US
  5. Click Create
  6. Wait until creation is complete, click refresh from the resource group view, you should see your resource group appear

[collapse]

[collapse]
Create the Web App and ASP

  1. Create a Web App in this resource group named: yourname-app
    • OS = Windows
    • Publish = Code
    • App Insights = Off
    • Attach a new App Service plan = New ASP named: yourname-asp with pricing tier Standarad: 1 Small

Reference Material

https://azure.microsoft.com/en-us/get-started/web-app/

Full Solution

Follow these steps:

  1. Select Resource Group on the Menu
  2. Click workshop-rg
  3. Click Add
  4. Search for Web App and select it
  5. Click Create
  6. App Name = yourname-app (each must be universally a unique name, pick one that makes sense to you)
  7. Resource Group = Use Existing, workshop-rg
  8. OS = Windows
  9. Publish = Code
  10. App Service Plan/Location = Create New
    1. App Service Plan  = yourname-asp
    2. Location = Central US
    3. Pricing Tier = S1 Standard
  11. Application Insights  = Off
  12. Click Create
  13. Wait until creation is complete, click refresh from the workshop-rg view, you should see 2 items in your resource group

 

[collapse]

[collapse]
Configure the Web App

  1. Configure your web app with:
    • Python = 3.4
    • Platform = 64-bit
    • Always On = On
    • Application Settings
      • KERAS_BACKEND = cntk
      • WSGI_LOG = d:\home\logfiles\log.txt
  2. Install the Python 3.5.4 x64 extension

Reference Material

https://docs.microsoft.com/en-us/azure/app-service/web-sites-configure

https://azure.microsoft.com/en-us/blog/azure-web-sites-extensions/

Full Solution

Follow these steps:

  1. Select Resource Group on the Menu
  2. Click workshop-rg
  3. Select yourname-app
  4. Select Application Settings on the secondary menu
  5. Set Python = 3.4
  6. Set Platform = 64-bit
  7. Set Always On = On
  8. Under Application Settings create these app settings:
    1. KERAS_BACKEND = cntk
    2. WSGI_LOG = d:\home\logfiles\log.txt
  9. Click Save
  10. Wait until saving is complete
  11. Select Extensions on the secondary menu
  12. Click Add
  13. Click Choose Extension and pick Python 3.5.4 x64
  14. Accept the Legal terms
  15. Click OK
  16. Wait until Extension install completes

[collapse]

[collapse]

 

Configure deployment

Configure GIT deployment

  1. Use Deployment Center to configure Local GIT deployment
  2. Set the Deployment Password

Reference Material

https://docs.microsoft.com/en-us/azure/app-service/app-service-deploy-local-git

Full Solution

Follow these steps:

  1. Open yourname-app
  2. Click Deployment Center from the secondary menu
  3. Select Local Git
  4. Click Continue
  5. Select App Service Kudu
  6. Click Continue
  7. Click Finish
  8. Click Deployment Credentials
  9. Click User Credentials
  10. Enter a password of your choice twice and click save credentials

[collapse]

 

[collapse]
Install a GIT tool (if needed)

  1. If you have a Git tool you know how to use to clone, commit, push, and pull you can skip this step
  2. Download and install SourceTree

Reference Material

https://www.sourcetreeapp.com

https://confluence.atlassian.com/get-started-with-sourcetree/install-sourcetree-847359094.html

Download and Install
  1. Go to https://id.atlassian.com/signup
  2. Create and verify your login (if you don’t already have one)
  3. Go to https://www.sourcetreeapp.com/
  4. Click download
  5. Run the downloaded file
  6. Click through the installer, logging into to your account, only install Git not Mecurial, skip adding remotes

[collapse]

[collapse]
Clone the Kudu Git Repo

  1. Clone the repo using the information found in the Azure Web App Deployment Center to a local folder named yourapp-deploy

Reference Material

https://confluence.atlassian.com/bitbucket/clone-a-repository-223217891.html

Hint 1

Find the info used to clone the Git Repo from azure:

  1. Open yourname-app
  2. Click Deployment Center from the secondary menu
  3. Make a note of the Git Clone Uri from this screen
  4. Use the password you setup in a previous step

[collapse]
Hint 2

Use Sourcetree to clone the repo:

  1. Create a folder on your hard drive named yourapp-deploy
  2. Open Sourcetree
  3. Click the + button near the top of the screen
  4. Click the Clone button
  5. Source Path = enter the Uri from Azure that was labeled Git Clone Uri
    • Enter your password in the login that pops up
  6. Destination Path = click browse and select the folder created in step 1
  7. Click Clone

[collapse]

[collapse]

 

Perform deployment

Cleanup

  1. Open the folder yourapp-deploy
  2. Delete everything besides the .git folder

Reference Material

None

[collapse]
Install the code and trained model

  1. Copy the main.py and out folder to yourapp-deploy

Reference Material

None

[collapse]
Create the Requirements File

  1. Create a file named reqs.txt (NOT the normal requirements.txt)
  2. Define the package requirements within this file

Reference Material

https://devcenter.heroku.com/articles/python-pip

https://pip.pypa.io/en/stable/reference/pip_show/

Hint 1

Create reqs.txt:

  1. Create a new file in yourapp-deploy named reqs.txt
  2. Add the requirements in the form of pipmodule==version number, one per line, use pip list and pip show to find them

[collapse]
Hint 2

Use the following as your contents of reqs.txt:


numpy==1.11.3
scikit-learn==0.18.1
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
Werkzeug==0.12.2
cntk-gpu==2.6
scipy==1.0.0
keras==2.0.6
h5py==2.7.1
view raw

reqs.txt

hosted with ❤ by GitHub

[collapse]

[collapse]
Install the Python Proxy and Web.config

  1. Copy ptvs_virtualenv_proxy.py from the Python Tools for Visual Studio in yourapp-deploy
  2. Create a web.config with the contents of hint 2

Reference Material

https://github.com/Microsoft/PTVS

Hint 1

Download this file and place it in yourapp-deploy folder: https://github.com/Microsoft/PTVS/blob/master/Python/Product/BuildTasks/ptvs_virtualenv_proxy.py

[collapse]
Hint 2

Create the web.config file:

  1. Create a new web.config file in yourapp-deploy folder.
  2. Place these contents in the file and save:


<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="WSGI_ALT_VIRTUALENV_HANDLER" value="main.app" />
<add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS"
value="d:\home\python354x64\python.exe" />
<add key="WSGI_HANDLER"
value="ptvs_virtualenv_proxy.get_venv_handler()" />
<add key="PYTHONPATH" value="D:\home\site\wwwroot" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed"></httpErrors>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="Python27_via_FastCGI" />
<remove name="Python34_via_FastCGI" />
<add name="Python FastCGI"
path="handler.fcgi"
verb="*"
modules="FastCgiModule"
scriptProcessor="d:\home\python354x64\python.exe|d:\home\python354x64\wfastcgi.py"
resourceType="Unspecified"
requireAccess="Script" />
</handlers>
<rewrite>
<rules>
<rule name="Static Files" stopProcessing="true">
<conditions>
<add input="true" pattern="false" />
</conditions>
</rule>
<rule name="Configure Python" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
view raw

web.config

hosted with ❤ by GitHub

[collapse]

[collapse]
Create the Wheelhouse

  1. Open an Anaconda Prompt in your CNTK environment
  2. Change to the yourapp-deploy folder
  3. Create a wheelhouse based off of the reqs.txt file

Reference Material

https://pypi.org/project/Wheelhouse/

https://docs.python-guide.org/dev/virtualenvs/

Hint 1

Create the wheelhouse:

  1. Open an Anaconda Prompt
  2. Activate cntk-py35
  3. Use the “cd” command to change to the folder yourapp-deploy
  4. python.exe -m pip wheel -r reqs.txt -w wheelhouse
  5. Wait while the wheelhouse is built, it will take a long time

[collapse]

 

[collapse]
Create the Deployment Script

  1. Create a file named .deployment in yourapp-deploy
  2. Add these contents to the file:
[config]
command = deploy.cmd
  1. Create a file named deploy.cmd
  2. Add these contents to the file:
d:\home\python354x64\python.exe -m pip install --upgrade --force-reinstall --ignore-install -r reqs.txt

Reference Material

None

[collapse]
Publish

  1. Commit the changes in yourapp-deploy
  2. Push changes and wait, watching the output from the push

Reference Material

https://confluence.atlassian.com/sourcetreekb/commit-push-and-pull-a-repository-on-sourcetree-785616067.html

Hint 1

Commit the Changes:

  1. Open Source Tree
  2. Select the tab for yourapp-deploy
  3. Click the commit toolbar button
  4. Click the stage all button
  5. Enter a comment
  6. Click Commit

[collapse]
Hint 2

Commit the Changes:

  1. Click the Push button the toolbar
  2. On the next dialog check the checkbox to watch the commit
  3. Wait a long time, you are waiting for the push, the installation of the python dependencies, this will take a while

[collapse]

[collapse]

 

Test with CURL

Test the service with Curl

  1. Open an Anaconda prompt and switch to your Sales-Win-Loss folder
  2. curl -X POST -d @request.json “http://yourapp-app.azurewebsites.net/predict” -H “Content-Type: application/json”
  3. Verify you get a json document back with a prediction

Reference Material

None

[collapse]

 

Lab Complete!

 

Extra Credit – Python in Azure

Learn More about Deploying Python to Azure App Services