- A Docker container for deployment of Python production-ready ML model using Flask.
- A proof of concept/simple prototype for ML model deployment in Azure.
- Important: Note that another repository is the foundation for this repository. This repository is foucsed on model deployment and another one is focused on model training. Be sure to check out both.
- Project Details
- Demo
- Folder Structure
- Learning Purpose
- Technology Applied
- Software Requirement
- Local Setup (Windows 10 Environment)
- Deploy Docker image to Azure
- Technical Contributions
- Learning Lessons
- Credits
- Contain 2 Endpoints:
- /api/v1/car_price/prediction (POST): Given an input features, return the prediction result.
- /api/v1/car_price/global_feature_importance (GET): Return global feature importance for the raw features and preprocessed features.
-
Demo for car dealership Flask API (Local deployment)
-
Demo for car dealership Flask API (Azure deployment)
car_dealership
folder contains flask application.resources
folder contains input for testing, delete this folder when building Docker image.
- To learn how to build Python 🐍 production-ready model using Docker.
- To learn how to deploy built Docker image to Azure Web App.
- To get my hands dirty in DevOps 😉.
- Docker: To containerise the API since there is many Python dependencies, why not just bundle it up.
- Flask: For building API to allow access to trained models.
- Azure AutoML: Auto optmization of machine learning models without even trying 🤣.
- Windows Edition
- Docker Desktop on Windows
- Visual Studio 2019 Community Version: To develop the API and manage Python dependencies
- Azure free account (Poor man choice 🤓)
Tips 💡: For installing Docker Desktop in Windows, I personally recommended to setup WSL 2 backend instead of setting up Hyper-V backend and Windows containers. The setup steps for first option is available in this MS docs. The latter option requires Hypervisor, which is not available in Windows Home Edition. However, you can try to install Hypervisor in Windows Home Edition.
-
Download the source code from GitHub.
-
Open cmd in the code folder.
-
Assuming that you have installed Docker Desktop, execute the command:
docker build -t car_dealership_api:latest
-
If the build is successful, execute the command:
docker run -p 4000:80 car_dealership_api:latest
- This will run the built Docker container in localhost:4000. You can define any open ports that you like.
- Try to navigate to http://locahost:4000/api/v1/car_price/global_feature_importance to see if any JSON is returned.
-
Follow the intructions in this MS docs to create an Azure container registry.
-
Before proceeding to host the Docker container, try to pull the image and see if everything works.
az acr login
docker run -p 4000:80 <container-registry-login-server-url>/<repo-name>:<your-current-version-number>
- This will remotely pull the image to your local machine and run the built Docker container in localhost:4000.
- Try to navigate to http://locahost:4000/api/v1/car_price/global_feature_importance to see if any JSON is returned.
-
Follow the instructions in this MS docs to create an Web App to host Docker container.
-
Enjoy your Docker-containerised Python Flask API hosted in Azure!!! Congrats 🎉🍾🎊
- Define a requirements.txt for the Python environment for Azure AutoML inference.
- Define a docker image for the deployment of Azure AutoML model.
- You are going to encounter numerous errors and bugs during deployment. But that's part of the process. I spend like 5 weeks in figuring out how things work from creating API using Flask to building the Docker image.
-
Stack Overflow Community
-
ONNX Runtime for Azure ML. I use
mcr.microsoft.com/azureml/onnxruntime:latest
as the base image of the Docker deployment. -
My university UTAR. For renewing my free $100 Azure credit yearly. However, I almost used finish already due to inexperience 😢.