https://github.com/noahgift/flask-ml-azure-serverless
Deploy Flask Machine Learning Application on Azure App Services
https://github.com/noahgift/flask-ml-azure-serverless
northwestern northwestern-434 oreilly-books udacity
Last synced: 3 months ago
JSON representation
Deploy Flask Machine Learning Application on Azure App Services
- Host: GitHub
- URL: https://github.com/noahgift/flask-ml-azure-serverless
- Owner: noahgift
- Created: 2020-08-06T14:54:35.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-10T22:55:55.000Z (9 months ago)
- Last Synced: 2025-04-04T00:07:21.070Z (6 months ago)
- Topics: northwestern, northwestern-434, oreilly-books, udacity
- Language: Python
- Homepage:
- Size: 245 KB
- Stars: 109
- Watchers: 4
- Forks: 213
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 🎓 Pragmatic AI Labs | Join 1M+ ML Engineers
### 🔥 Hot Course Offers:
* 🤖 [Master GenAI Engineering](https://ds500.paiml.com/learn/course/0bbb5/) - Build Production AI Systems
* 🦀 [Learn Professional Rust](https://ds500.paiml.com/learn/course/g6u1k/) - Industry-Grade Development
* 📊 [AWS AI & Analytics](https://ds500.paiml.com/learn/course/31si1/) - Scale Your ML in Cloud
* ⚡ [Production GenAI on AWS](https://ds500.paiml.com/learn/course/ehks1/) - Deploy at Enterprise Scale
* 🛠️ [Rust DevOps Mastery](https://ds500.paiml.com/learn/course/ex8eu/) - Automate Everything### 🚀 Level Up Your Career:
* 💼 [Production ML Program](https://paiml.com) - Complete MLOps & Cloud Mastery
* 🎯 [Start Learning Now](https://ds500.paiml.com) - Fast-Track Your ML Career
* 🏢 Trusted by Fortune 500 TeamsLearn end-to-end ML engineering from industry veterans at [PAIML.COM](https://paiml.com)
# flask-ml-azure-serverless
Deploy Flask Machine Learning Application on Azure App Services
## If you run into problems
* Build the container using Docker commands in the `Makefile`
* Rebuild the model using a later version of sklearn and update requirements.txt with your version of sklearn## To run it locally follow these steps (on Python 3.8, there are issues on later version of Python)
1. Create virtual environment and source
```bash
python3 -m venv ~/.flask-ml-azure
source ~/.flask-ml-azure/bin/activate
```2. Run `make install`
3. Run `python app.py`
4. In a separate shell run: `./make_prediction.sh`
## To run it in Azure Pipelines
1. Refer to [Azure Official Documentation guide here throughout](https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python-webapp?view=azure-devops)
2. Launch Azure Shell

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

4. 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](https://www.youtube.com/watch?v=3vtBAfPjQus)*
5. Create virtual environment and source
```bash
python3 -m venv ~/.flask-ml-azure
source ~/.flask-ml-azure/bin/activate
```2. Run `make install`
3. Create an app service and initially deploy your app in Cloud Shell
`az webapp up -n `

4. Verify deployed application works by browsing to deployed url: `https://.azurewebsites.net/`
You will see this output:

5. Verify Machine Learning predictions work
Change the line in `make_predict_azure_app.sh` to match the deployed prediction
`-X POST https://.azurewebsites.net:$PORT/predict `
6. [Create an Azure DevOps project and connect to Azure, (as official documentation describes)](https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python-webapp?view=azure-devops)

7. Connect to Azure Resource Manager

8. Configure connection to previously deployed resource group

9. Create new Python Pipeline with 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](https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/python-webapp?view=azure-devops#yaml-pipeline-explained).
```
# 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/pythontrigger:
- mastervariables:
# Azure Resource Manager connection created during pipeline creation
azureServiceConnectionId: ''
# 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
```
10. Verify Continuous Delivery of Azure Pipelines by changing `app.py`You can watch this [YouTube Walkthrough of this process](https://www.youtube.com/watch?v=3KF9DltYvZU)
11. 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](https://www.youtube.com/watch?v=TItOatTfAOc)
### [Cloud Computing for Data Analysis Book](https://leanpub.com/cloud4data)
This book is being written "just in time", with a weekly release schedule.