This project operationalizes a Machine Learning Microservice API. It uses a pre-trained sklearn model to predict housing prices in Boston based on various features, such as the average number of rooms in a house, highway access, teacher-to-pupil ratios, and more. The data was initially sourced from Kaggle and can be found on the data source page.
The microservice serves housing price predictions via API calls. This framework can be extended to other pre-trained machine learning models, including those for image recognition or data labeling. The entire application has been containerized using Docker and deployed with Kubernetes.
app.py: The main application scriptmodel_data/: Folder containing the trained model and prediction datarequirements.txt: Lists the dependencies for the app
output_txt_files/: Folder containing output files for Docker and Kubernetes
Dockerfile: Defines the image for deploying the app within a container
Makefile: Contains useful commands for setup, installation, testing, linting, running Docker, running Kubernetes, uploading to DockerHub, and morerun_docker.sh: Script to build and start the containerrun_kubernetes.sh: Script to deploy the app on Kubernetesupload_docker.sh: Script to upload the Docker image to DockerHubmake_prediction.sh: Script to test the application and generate predictions
- Create a virtual environment and activate it.
- Run
make installto install the necessary dependencies.
You can run the app in three different ways: Standalone, Docker, or Kubernetes.
Run the app with the following command:
python app.pyThe app will be accessible at http://localhost:80.
Use the run_docker.sh script to build and start the container:
./utils/run_docker.shThe script will:
- Build a Docker image
- List images to verify that the app is dockerized
- Run the container with port mapping (host port 5000 to container port 80)
The app will be accessible at http://localhost:5000.
Use the run_kubernetes.sh script to deploy the app on a Kubernetes cluster:
./utils/run_kubernetes.shThe script will:
- Start a container in the Kubernetes cluster (ensure you have a cluster ready, e.g., via
minikube) - Wait for the pod to start
- List the pod to confirm it is running
- Forward port 5000 (host) to port 80 (container)
The app will be accessible at http://localhost:5000.
To test the application, run the make_prediction.sh script. The model will return a predicted price based on the input provided. A sample input JSON is as follows:
{
"CHAS":{
"0":0
},
"RM":{
"0":9.575
},
"TAX":{
"0":296.0
},
"PTRATIO":{
"0":15.3
},
"B":{
"0":396.9
},
"LSTAT":{
"0":4.98
}
}