Python Web App

About

Applications

HoudiniUnreal EngineUnity 3DNukeMayaBlenderZBrushPythonMixed RealityMachine LearningGraphic DesignExtras
About

Site created with Notion, Super & Cluster

← Back

Notes

Overview

  • With ML applications, the model, scaler, and any other applicable components run in the Python-based backend.

Process

Flask

  • Data input through API requests using Postman which relays JSON data to the flask app and Python's request library.
  • Flask app connects to HTML web form.

Components

  • App
    • HTML form using Flask linked to home.html
    • Form data input into Flask.
    • Flask returns data generated by function through function.html.
  • Templates
    • home.html
      • Flask-based form for accepting user input.
      • Relays user input to Flask app.
    • function.html
      • Returns data generated by funcion.

Heroku Deployment

  • In an Anaconda Prompt, navigate to the location of the files to be deployed.
  • Create a venv with intended Python version:
  • conda create --name venvcustomname python=3.7
  • Activate new venv and install relevant libraries for application.
    • Use pip, not conda, so that a pip freeze requirements file can be generated.
    • Libraries
      • flask
      • Flask-WTF
      • scikit-learn
        • This also installs joblib and numpy.
      • tensorflow
      • gunicorn
        • Python web server gateway interface.
        • Used to push the flask app to Heroku server.
  • Generate pip freeze requirements file to log the installed libraries.
  • pip freeze > requirements.txt

    Resource: https://pip.pypa.io/en/stable/cli/pip_freeze/

  • Create process file required by Heroku.
    • Label the file Procfile with no file extension.
    • Include the following (note the name of the app file reference):
    • web: gunicorn app:app
  • On Heroku Dashboard, Create new app.
    • Choose unique App name. (my-tf-flower-app)
    • Choose Deployment method, namely Heroku Git and follow instructions.
  • Push rejected notifications
    • No matching distribution found for <libraryname>
      • Identify the library that was not able to be installed and identify a suitable version from the available options in Heroku.
      • Update the requirements.txt file to reflect the intended version of the library.
    • Compile slug size is too large (max is 500M)
      • Consider other library versions that may be lighter and without heavy dependencies.
      • Resources
        • https://devcenter.heroku.com/articles/slug-compiler#slug-size
        • https://help.heroku.com/KUFMEES1/my-slug-size-is-too-large-how-can-i-make-it-smaller
    • Update the requirements.txt file to reflect the intended version of the library.
    • Commit and push update in Prompt
    • git commit -am "Updated lib in requirements file."
      git push heroku master
  • Heroku attempts to compress and build source.
    • Successful deployment results in message of
    • Launching...
      https://<appname>.herokuapp.com/ deployed to Heroku
      Verifying deploy... done.
  • Deactivate venv in prompt.

Local Testing

Postman

  • Use Postman to send a POST request to the API in JSON format.

Troubleshooting

TF on Heroku

  • The Tensorflow module is very large, exceeding the slug limits for Heroku, primarily because of the GPU support. To mitigate the slug limits issue that arises, replace tensorflow with tensorflow-cpu in the pip freeze requirements file.
  • Source: https://stackoverflow.com/questions/61796196/heroku-tensorflow-2-2-1-too-large-for-deployment/62356779#62356779

  • Beware that apps on Heroku using TF can take some time to load.

On This Page

  • Notes
  • Overview
  • Process
  • Flask
  • Heroku Deployment
  • Local Testing
  • Postman
  • Troubleshooting
  • TF on Heroku