Skip to content

Latest commit

 

History

History
203 lines (137 loc) · 6.54 KB

README.md

File metadata and controls

203 lines (137 loc) · 6.54 KB

flask-ml-azure-serverless

Deploy Flask Machine Learning Application on Azure App Services

continuous-delivery

To run it locally follow these steps

  1. Create virtual environment and source
python3 -m venv ~/.flask-ml-azure
source ~/.flask-ml-azure/bin/activate
  1. Run make install

  2. Run python app.py

  3. In a separate shell run: ./make_prediction.sh

To run it in Azure Pipelines

  1. Refer to Azure Official Documentation guide here throughout

  2. Launch Azure Shell

1-launch-azure-shell

  1. Create Github Repo with Azure Pipelines Enabled (Could be a fork of this repo)

2-create-Github-Repo

  1. Clone the repo into Azure Cloud Shell

Note: You make need to follow this YouTube video guide on how to setup SSH keys and configure cloudshell environment

  1. Create virtual environment and source
python3 -m venv ~/.flask-ml-azure
source ~/.flask-ml-azure/bin/activate
  1. Run make install

  2. Create an app service and initially deploy your app in Cloud Shell

az webapp up -n <your-appservice>

3-flask-ml-service

  1. Verify deployed application works by browsing to deployed url: https://<your-appservice>.azurewebsites.net/

You will see this output:

4-deployed-app

  1. Verify Machine Learning predictions work

Change the line in make_predict_azure_app.sh to match the deployed prediction -X POST https://<yourappname>.azurewebsites.net:$PORT/predict

5-successful-prediction

  1. Create an Azure DevOps project and connect to Azure, (as official documentation describes)

6-devops

  1. Connect to Azure Resource Manager

7-service-connection

  1. Configure connection to previously deployed resource group

8-azure-pipelines-setup

  1. Create new Python Pipeline with Github Integration

9-newpipeline

10-github-integration

This process will create a YAML file that looks roughly like the YAML output shown below. Refer to the official Azure Pipeline YAML documentation for more information about it.

# Python to Linux Web App on Azure
# Build your Python project and deploy it to Azure as a Linux Web App.
# Change python version to one thats appropriate for your application.
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- master

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureServiceConnectionId: '<youridhere>'
  
  # Web app name
  webAppName: 'flask-ml-service'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Environment name
  environmentName: 'flask-ml-service'

  # Project root folder. Point to the folder containing manage.py file.
  projectRoot: $(System.DefaultWorkingDirectory)
  
  # Python version: 3.7
  pythonVersion: '3.7'

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: BuildJob
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(pythonVersion)'
      displayName: 'Use Python $(pythonVersion)'
    
    - script: |
        python -m venv antenv
        source antenv/bin/activate
        python -m pip install --upgrade pip
        pip install setup
        pip install -r requirements.txt
      workingDirectory: $(projectRoot)
      displayName: "Install requirements"

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(projectRoot)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      displayName: 'Upload package'
      artifact: drop

- stage: Deploy
  displayName: 'Deploy Web App'
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: DeploymentJob
    pool:
      vmImage: $(vmImageName)
    environment: $(environmentName)
    strategy:
      runOnce:
        deploy:
          steps:
          
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
            displayName: 'Use Python version'

          - task: AzureWebApp@1
            displayName: 'Deploy Azure Web App : flask-ml-service'
            inputs:
              azureSubscription: $(azureServiceConnectionId)
              appName: $(webAppName)
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
  1. Verify Continuous Delivery of Azure Pipelines by changing app.py

You can watch this YouTube Walkthrough of this process

  1. Add a lint step (this gates your code against syntax failure)
    - script: |
        python -m venv antenv
        source antenv/bin/activate
        make install
        make lint
      workingDirectory: $(projectRoot)
      displayName: 'Run lint tests'

You can watch this YouTube Walkthrough of this process

This book is being written "just in time", with a weekly release schedule.

cloud4data books