End-to-End Machine learning modelšŸ deployment using FAST-API and HEROKUšŸ 

Ravi Kumar
7 min readAug 27, 2022

--

Here in this article, we will discuss end-to-end machine learning model deployment using the FAST-API python module and Heroku (Open source platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud)

Code links: https://github.com/Ravikumar10593-hub/FastAPI_depoloyment_to_heroku

Letā€™s divide this entire process flow into two parts :

  1. Create a model and deploy it in your local host
  2. Deploy the end-to-end solution in the Heroku platform

Create a model and deploy it in your local host

To start with the process, we will be using Google collab to create a basic Diabetes machine learning model that will predict whether a patient is diabetic or not.

Letā€™s get started with reading the dataset and creating a basic SVC model that will take the following parameters to predict the outcome:

pregnancies, Glucose, blood pressure, skin thickness, Insulin, BMI, DiabetesPedigreeFunction, Age

Baseline model

## Reading the important modules
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_score

Reading the dataset and printing the top 5 rows

## loading the diabetes dataset to a pandas DataFrame

diabetes_dataset = pd.read_csv('/content/diabetes.csv')

## printing the first 5 rows of the dataset

diabetes_dataset.head()

The next step is to separate the data & labels and split the data into train and test.

## separating the data and labels
X = diabetes_dataset.drop(columns = 'Outcome', axis=1)

Y = diabetes_dataset['Outcome']

## Train and test split
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.2, stratify=Y, random_state=2)

print(X.shape, X_train.shape, X_test.shape)
## Creating the SVM object
classifier = svm.SVC(kernel='linear')

## training the support vector Machine Classifier
classifier.fit(X_train, Y_train)

After creating an SVM classifier fit the classifier with the X_train and Y_train data.

## accuracy score on the training data
X_train_prediction = classifier.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)

print('Accuracy score of the training data : ', training_data_accuracy)
## accuracy score on the test data
X_test_prediction = classifier.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)

print('Accuracy score of the test data : ', test_data_accuracy)

Now the base model is ready, so we start using this model to deploy our process flow.

Before doing that we need to convert the model into a pickle file.

import pickle

filename = 'diabetes_model.sav'
pickle.dump(classifier, open(filename, 'wb'))

FAST-API

FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python. It provides a very friendly UI to interact with your API responses and uses an asynchronous call method that makes it even faster than other tools.

Difference between Synchronous and Asynchronous

For more details : https://fastapi.tiangolo.com/

Follow along with the production code and the folder structure.

Steps to follow

  • Create a .py file named ml_api
# -*- coding: utf-8 -*-
"""
@author: Ravi
"""
## Importing the libraries
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
import json
## Creating a Fastapi objectapp = FastAPI()## Using Pydantic lib, defining the data type for all the inputsclass model_input(BaseModel):

pregnancies : int
Glucose : int
BloodPressure : int
SkinThickness : int
Insulin : int
BMI : float
DiabetesPedigreeFunction : float
Age : int

## loading the saved model
diabetes_model = pickle.load(open('diabetes_model.sav', 'rb'))
## Creating a POST request to the API@app.post('/diabetes_prediction')
def diabetes_predd(input_parameters : model_input):

input_data = input_parameters.json()
input_dictionary = json.loads(input_data)

preg = input_dictionary['pregnancies']
glu = input_dictionary['Glucose']
bp = input_dictionary['BloodPressure']
skin = input_dictionary['SkinThickness']
insulin = input_dictionary['Insulin']
bmi = input_dictionary['BMI']
dpf = input_dictionary['DiabetesPedigreeFunction']
age = input_dictionary['Age']


input_list = [preg, glu, bp, skin, insulin, bmi, dpf, age]

prediction = diabetes_model.predict([input_list])

if (prediction[0] == 0):
return 'The person is not diabetic'
else:
return 'The person is diabetic'
if __name__ == '__main__':
uvicorn.run(app, host = '127.0.0.1', port=8000)
  • Libraries needed to be installed before testing the deployment
pip install fastapi
pip install uvicorn
pip install pickle5
pip install pydantic
pip install scikit-learn
pip install requests
pip install pypi-json
  • To run the app into the local host
uvicorn ml_api:app --reload

Yellow is the python file name and the blue is the API object name

  • Have a folder with the following files
Folder structure

Once you are done with all the above steps, open your VS code studio and run the command

uvicorn ml_api:app --reload
  • Add /docs in front of the local host URL
http://127.0.0.1:8000/docs

FAST-API UI will pop up with the API name we have created.

Testing the prediction using another parameter in FAST-API

  • Click on the POST API you have created and press Try it out
  • Let's test the following case in the API where the outcome is diabetic
  • Press execute and check the responses

We get the same results, so you can try changing multiple parameters and check the responses accordingly.

Letā€™s move to the second phase where we will be seeing, how we can deploy the same thing in the HEROKU platform.

Deploy the end-to-end solution in the Heroku platform

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps, and test them. Some interesting things about Heroku:

  • It has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go
  • Heroku is said to be a polyglot platform as it has features for a developer to build, run and scale applications in a similar manner across most languages
  • Heroku was acquired by Salesforce in 2010 for $212 million

Note: Before following the steps, you need to have a Github account

Steps to follow to deploy the same in the Heroku cloud server:

  • Push all the files in the Github public repo including Procfile and requirement file

Find the repo link here: https://github.com/Ravikumar10593-hub/FastAPI_depoloyment_to_heroku

numpy
scipy
scikit-learn
matplotlib
pandas
fastapi
uvicorn
gunicorn==19.9.0
uvloop
httptools
  • Click on ā€œCreate new appā€
  • Give your app a unique name separated by ā€˜-ā€™ and press create app
  • Select the Github option and search the GitHub repo you created with all the files in it and press connect
Github repo
  • Deploy code in the main branch
  • Wait for some time and click on visit
Installation logs
  • Click view and add ā€˜/docsā€™ at the end of the URL
https://demo-testing-1999.herokuapp.com/docs

Want to read more: https://medium.com/@ravikumar10593/

Find my all handles: https://linktr.ee/ravikumar10593

If this article helped you donā€™t forget to Follow, like, and share it with your friendsšŸ‘Happy Learning!!

--

--

Ravi Kumar

Hey Techies!, I am here to share my learning about different tech, tools, business and automations, for more details: https://linktr.ee/ravikumar10593