Skip to content

Reference for flask with nginx, gunicorn, and redis

Notifications You must be signed in to change notification settings

jumzzz/model_serving

Repository files navigation

Serving Models

This serves as a reference and a demo on how to serve models with LightGBM, Flask, gunicorn, nginx, and redis

The task is to create a Web Service that serves a model that predicts the Income Bracket using Adult Census Dataset

Training Details

Model Training is implemented at training folder.

Implemented Stuff

  • Web Service that uses Flask, gunicorn, and nginx ✔️
  • Uses redis for caching ✔️
  • POST driven endpoint ✔️
  • Model Feature Engineering ✔️
  • Hyperparameter Tuning (LGBM w/ Bayesian Optimization) ✔️
  • docker + docker-compose ✔️
  • Unit Testing + Github Actions ✔️

How to Run the WebService Locally

Two options are available, either with docker-compose or terraform

With docker-compose

The simplest way to run the Web Server is to use docker-compose. In this exact location of this repository simply type:

docker-compose up -d --build

Where it spins-up three Docker Containers. Namely:

  • Container for Flask and gunicorn
  • Container for nginx
  • Container for redis

You can use the following endpoint locally

http://0.0.0.0:1337/predict_income

Where it takes a sample payload from a POST request

With terraform

You can also run this with terraform. Simply run the following

terraform init
terraform plan
terraform apply

If you want to destroy the docker containers simply

terraform destroy

Sample Payload

For more details, go to sample_payload directory. You'll find a sample file, and a sample script to execute.

{'age': 82,
 'workclass': 'Private',
 'fnlwgt': 132870,
 'education': 'HS-grad',
 'marital.status': 'Widowed',
 'occupation': 'Exec-managerial',
 'relationship': 'Not-in-family',
 'race': 'White',
 'sex': 'Female',
 'capital.gain': 0,
 'capital.loss': 4356,
 'hours.per.week': 18,
 'native.country': 'United-States'}

And receives an output

Sample Output using the Previous Payload

{'predicted_income_class': '<=50k', 'prediction_raw': 0.08823289115946195, 'status': 'success'}

Github Actions - Auto-trigger of Unit Testing

Unit Test is triggered on push and pull-request events

Sample Actions