This example focuses on the following:
- Building a machine learning model with Scikit-learn (model train and save)
- Creating a FastAPI to serve predictions
- Containerizing the API using Docker
- Create a virtual environment
python3 -m venv ~/opt/MLFlow- Activate it and install necessary packages using
requirements.txt
source ~/opt/MLFlow/bin/activate
pip install -r requirements.txt- Build the Docker image by running the following docker build command:
docker build -t house-price-prediction-api .- Next run the Docker container:
docker run -d -p 80:80 house-price-prediction-apiYour API should now be running and accessible at http://127.0.0.1:80.
You can use curl or Postman to test the /predict endpoint by sending a POST request. Here’s an example request:
curl -X 'POST' \
'http://127.0.0.1:80/predict' \
-H 'Content-Type: application/json' \
-d '{
"MedInc": 3.5,
"AveRooms": 5.0,
"AveOccup": 2.0
}'Also, you can run the following command to execute the docker inside
docker exec -it [docker-id] /bin/bashTo find the Docker ID:
docker ps
