Skip to content

Latest commit

 

History

History
64 lines (39 loc) · 5.45 KB

File metadata and controls

64 lines (39 loc) · 5.45 KB

Deploying the API and Web Application to the Cloud


Table of Contents


Overview

So far, we have successfully built the machine learning model using Python and Sckit-Learn, a simple API using Flask, a web application using Flask/Python, and another hybrid web application using Nextjs and Flask. In this lesson, we will wrap up this workshop by deploying all these applications to the cloud (which is the goal of this workshop) so users can access them.

I believe you have seen the processes that got us here by now. All those processes are essential to deploying machine learning models to the web. You must understand what you're building and who you're building for. Upon understanding, you decide the presentation layer you want to work on. If web, you must "package" the model into an API and web application before finally deploying.

To deploy our Python API and web application, we will use Fly, a cloud platform that allows you to deploy app servers close to your users (using edge computing). The free plan includes up to 3 shared-cpu-1x 256mb virtual machines, 3GB persistent volume storage, and 160GB outbound data transfer. You can use that to get started until you need to scale. You can read their documentation to learn how to deploy apps in different languages or frameworks.

To deploy the hybrid Nextjs + Flask app, we will use Vercel, which will deploy the frontend using SSG and the Flask server as a Python serverless function. The same should happen if you use similar web deployment platforms like Netlify (however, I still need to test this and will update here if/when I do).

Deploying the Flask API and App

I believe you have already created a Fly account and installed flyctl. Please see the prerequisites section if you haven't.

Kindly follow the steps below to deploy the API:

  1. Ensure you’re in the root directory of the source code you want to deploy (for me, /app/flask-api or app/flask-app).
  2. Ensure you created a virtual environment before installing the Python libraries used (see the prerequisites section if you didn't).
  3. Run pip3 freeze > requirements.txt to list all the installed packages/version numbers in the virtual environment inside the requirements.txt file.
  4. Login into flyctl using the fly auth login command.
  5. Run the fly launch command to launch a new app (this will scan the source code and detect the app type).
  6. Enter your app name or leave it blank, and flyctl will generate one.
  7. Choose a primary region for deployment (e.g., 'mad' region for Madrid, Spain). Fly uses edge computing to run deployed applications in data centers worldwide. This means you can deploy your apps in multiple regions, and your users' requests will hit the nearest server to them.
  8. A new application will be created in the Fly dashboard, and flyctl will generate some new files (.dockerignore, Procfile, and fly.toml) to your source code.
  9. Run fly deploy to deploy the application (this will take some minutes to finish).
  10. Run fly status to verify the deployment.

Deploying the Nexjs Flask App

Important

If you deploy the nextjs-flask-app with Vercel, you will run into the A Serverless Function has exceeded the unzipped maximum size of 250 MB error. This is because Vercel places restrictions on the maximum uncompressed size of the deployment bundle for functions (and most likely, in our case, the scikit-learn package is increasing the bundle size).


The Python runtime is still in Beta on Vercel. While searching for possible solutions to this serverless function limit error with scikit-learn, the alternative and recommended solution is to deploy the Flask API separately (as we have done earlier) and the Nextjs frontend alone to Vercel. We will then need to update the flask-api to support CORS, change how we get the request data, and use the live API directly in the Nextjs app.

This will be your challenge for this workshop :). I have added some resources to give you a head start in the further resources section. I will also update this section with a solution after the workshop (to give you time to try it out).


Thank you for coming this far; you've done well 👏🏾. Please open a new GitHub discussion using the links below and let me know your thoughts about this lesson or any issues you're experiencing.

Share Feedback | Ask Question


<< previous lesson | next lesson >>