GithubHelp home page GithubHelp logo

noahgift / flask-ml-azure-serverless Goto Github PK

View Code? Open in Web Editor NEW
94.0 5.0 196.0 216 KB

Deploy Flask Machine Learning Application on Azure App Services

Makefile 20.39% Python 46.26% Shell 21.08% Dockerfile 12.27%
udacity northwestern oreilly-books northwestern-434

flask-ml-azure-serverless's Introduction

flask-ml-azure-serverless

Deploy Flask Machine Learning Application on Azure App Services

continuous-delivery

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
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

flask-ml-azure-serverless's People

Contributors

noahgift avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

flask-ml-azure-serverless's Issues

Libraries Versions

Problems encountered while doing lab via Coursera:

  1. Flask version 1.0.2 raises Jinja2 error.
    ImportError: cannot import name 'Markup' from 'jinja2' (/home/coder/project/VENV/lib/python3.8/site-packages/jinja2/__init__.py)
    Updating to version 2.2.2 resolves the issue.

  2. Numpy installed by default is 1.24.1, which raises:
    AttributeError: module 'numpy' has no attribute 'float'
    on joblib.load(file_name) call.
    Preinstalling numpy==1.23 resolves the issue.

  3. prediction = list(clf.predict(scaled_payload)) on line 71 raises

raw_predictions = self.loss_.get_init_raw_predictions(
AttributeError: 'LeastSquaresError' object has no attribute 'get_init_raw_predictions'

Which can be resolved by downgrading scikit-learn to 0.20.3, but python3.8 + updated pip does not support installing it.

So, there is an open question about resolving it: use the latest versions and update the code (imports and logic would be different) or use previous Python versions.

Update Lint command in Makefile

Probably it is intentionally left but pylint should be updated with W0702 otherwise build will failed during CI/CD.
pylint --disable=R,C,W1203,W0702 app.py

make_prediction.sh doesn't exist

The book & README.md asks us to run ./make_prediction.sh but I can't find it in the repo.

There is a file called make_predict.sh but running it locally (after app.py is running in another tab) results in a long html script being printed in the terminal which includes error messages such as " <title>AttributeError: 'LeastSquaresError' object has no attribute 'get_init_raw_predictions' // Werkzeug Debugger</title> ".

Just wondering if there's a missing file that got lost along the way? Thanks!

AttributeError: 'LeastSquaresError' object has no attribute 'get_init_raw_predictions'

I had some issues getting started with this repo, a hint on which python version to use would have been helpful.
FYI: when I first got started with this repo, I was using Python v3.8, which resulted in installation errors. After downgrading to Python v3.6.15, make install and python app.py run smoothly.

However, when executing ./make_predict.sh, I get the the following error message:

Traceback (most recent call last):
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/testuser/.py36/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/testuser/xpand/flask-ml-azure-serverless/app.py", line 68, in predict
    prediction = list(clf.predict(scaled_payload))
  File "/home/testuser/.py36/lib/python3.6/site-packages/sklearn/ensemble/_gb.py", line 2569, in predict
    return self._raw_predict(X).ravel()
  File "/home/testuser/.py36/lib/python3.6/site-packages/sklearn/ensemble/_gb.py", line 1655, in _raw_predict
    raw_predictions = self._raw_predict_init(X)
  File "/home/testuser/.py36/lib/python3.6/site-packages/sklearn/ensemble/_gb.py", line 1649, in _raw_predict_init
    raw_predictions = self.loss_.get_init_raw_predictions(
AttributeError: 'LeastSquaresError' object has no attribute 'get_init_raw_predictions'

I assume, this is due to some version conflict between the serialized prediction model from boston_housing_prediction.joblib and the version of scikit-learn.

I'm using an umodified version of requirements.txt.

Output of $ pip freeze:

astroid==2.4.2
click==8.0.4
dataclasses==0.8
Flask==1.0.2
importlib-metadata==4.8.3
isort==5.10.1
itsdangerous==2.0.1
Jinja2==3.0.3
joblib==1.1.1
lazy-object-proxy==1.4.3
MarkupSafe==2.0.1
mccabe==0.6.1
numpy==1.19.5
pandas==1.1.5
pylint==2.6.2
python-dateutil==2.8.2
pytz==2022.6
scikit-learn==0.22.2
scipy==1.5.4
six==1.16.0
toml==0.10.2
typed-ast==1.4.3
typing_extensions==4.1.1
Werkzeug==2.0.3
wrapt==1.14.1
zipp==3.6.0

Can you please help me resolving this issue as I'm unable to continue my training without this...

ImportError: cannot import name 'Markup' from 'jinja2'

I'm all new to python, currently attending a course on machine learning.
I've followed your instructions "by-the-letter", but when I run python app.py
I get the following error message:

$ python app.py
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from flask import Flask, request, jsonify
  File "/home/testuser/.flask-ml-azure/lib/python3.8/site-packages/flask/__init__.py", line 19, in <module>
    from jinja2 import Markup, escape
ImportError: cannot import name 'Markup' from 'jinja2' (/home/testuser/.flask-ml-azure/lib/python3.8/site-packages/jinja2/__init__.py)

Environment:

$ pip freeze
astroid==2.4.2
click==8.1.3
Flask==1.0.2
isort==5.10.1
itsdangerous==2.1.2
Jinja2==3.1.2
joblib==1.2.0
lazy-object-proxy==1.4.3
MarkupSafe==2.1.1
mccabe==0.6.1
numpy==1.23.5
pandas==1.1.5
pkg_resources==0.0.0
pylint==2.6.2
python-dateutil==2.8.2
pytz==2022.6
scikit-learn==0.22.2
scipy==1.9.3
six==1.16.0
toml==0.10.2
Werkzeug==2.2.2
wrapt==1.14.1

Can you please help me getting your sample app up-and-running?

Thanks ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.