Deploying a Machine Learning Model as a REST API with Flask
This is perhaps the best known database to be found in the pattern recognition literature. The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is linearly separable from the other 2; the latter are NOT linearly separable from each other.
Predicted attribute: class of iris plant.
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class: -- Iris Setosa -- Iris Versicolour -- Iris Virginica
- Build and train the machine learning model in a Jupyter Notebook (file: model/Iris_model.ipynb),
- save the model in a (pickle) file (file: api/iris_model.pkl)
- create an API application that uses the pre-trained model to generate predictions (file: api/api.py),
- encapsulate the application in a Docker container (file: api/Dockerfile),
- deploy the application to a cloud server.
- Python 3.4+,
- Docker,
- The required Python libraries used can be installed from the included requirements.txt file:
# Clone the project
git clone https://github.com/AchilleasKn/flask_api_python.git
# Change Directory
cd flask_api_python/api
# Install pip for Python3
apt install python3-pip
# Install the requirements
pip3 install -r requirements.txt
# Run the script in Python
python3 api.py
- achilleaskn/flask_api_python:latest
This image is based on the python:3.6-jessie official image
- achilleaskn/flask_api_python:alpine.latest
This image is based on Alpine Linux image which is a lightweight version of Linux
# Clone the project
git clone https://github.com/AchilleasKn/flask_api_python.git
# Change Directory
cd flask_api_python/api
# Build the docker image
docker build -t flask_api .
# For the alpine version run the following
#docker build -f Dockerfile.alpine -t flask_api .
# Run the flask_api image and expose the 5000 port
docker run -d -p 5000:5000 flask_api
# To see the running containers
docker ps
# To see the logs of our running container
docker logs <Container ID>
# Pull the docker image
docker pull achilleaskn/flask_api_python:latest
# For the alpine version run the following
#docker pull achilleaskn/flask_api_python:alpine.latest
# Run the flask_api image and expose the 5000 port
docker run -d -p 5000:5000 achilleaskn/flask_api_python:latest
# For the alpine version run the following
#docker run -d -p 5000:5000 achilleaskn/flask_api_python:alpine.latest
# To see the running containers
docker ps
# To see the logs of our running container
docker logs <Container ID>
Once it is running, the API can be queried using HTTP POST requests. I recommend using postman for testing.
URL: http://0.0.0.0:5000/predict
- Sample query for "Setosa" type:
{
"feature_array":[4.9, 2.9, 1.2, 0.3]
}
The response should look like this:
{
"prediction": [
0
]
}
- Sample query for "Versicolour" type:
{
"feature_array":[6.4, 3.2, 4.5, 1.5]
}
The response should look like this:
{
"prediction": [
1
]
}
- Sample query for "Virginica" type:
{
"feature_array":[6.2, 3.1, 5.3, 2.4]
}
The response should look like this:
{
"prediction": [
2
]
}