This repository showcases how to integrate FastAPI, Docker, and Poetry to build a production-ready Machine Learning Operations (MLOps) project. The project is designed to deploy machine learning models via a FastAPI microservice and automate the deployment process using GitHub Actions for a seamless CI/CD pipeline, deploying the containerized app to AWS EC2.
This project is based on the tutorial provided by The Halftime Code, which walks through the entire process from project setup to deployment on AWS.
- FastAPI: A high-performance web framework for serving machine learning models.
- Poetry: Manages Python dependencies and virtual environments.
- Docker: Containerizes the application for flexible deployment.
- AWS EC2: Deployment platform using GitHub Actions for CI/CD.
- GitHub Actions: Automates testing, building, and deploying the model to AWS EC2.
mlops/
├── .github/ # GitHub Actions workflows
│ └── workflows/
│ └── deploy.yml # CI/CD pipeline
├── src/ # Source code
│ ├── api/ # API routes
│ │ └── v1/
│ │ └── routes.py # Prediction endpoint
│ ├── core/ # Core app setup
│ │ └── config.py # Configuration management
│ └── app/ # Business logic
│ └── services/ # Model service logic
├── tests/ # Unit tests
│ ├── test_api.py # Test API endpoints
│ └── test_services.py # Test business logic
├── models/ # Pre-trained machine learning model
│ └── model.pkl
├── Dockerfile # Docker configuration
├── docker-compose.yaml # Docker Compose setup
├── pyproject.toml # Poetry configuration
└── README.md # Documentation
git clone https://github.com/yourusername/mlops-fastapi.git
cd mlops-fastapiEnsure you have Poetry installed:
pip install poetryThen, install the project dependencies:
poetry installTrain a sample machine learning model (RandomForest on Iris dataset) and save it to the models directory:
poetry run python -m train_modelStart the FastAPI server locally:
poetry run uvicorn src.core.server:app --reloadAccess the API at http://127.0.0.1:8000.
You can test the prediction endpoint using curl:
curl -X POST "http://127.0.0.1:8000/api/v1/predict" \
-H "Content-Type: application/json" \
-d '{"features": {"sepal length (cm)": 5.1, "sepal width (cm)": 3.5, "petal length (cm)": 1.4, "petal width (cm)": 0.2}}'Run the unit tests with:
poetry run python -m pytest tests/To build and run the Docker container:
docker build -t mlops-fastapi .
docker run -p 8000:8000 mlops-fastapiThe GitHub Actions CI/CD pipeline automates deployment to AWS EC2. Ensure you configure the necessary GitHub secrets for AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, EC2_SSH_KEY, and DOCKER_PASSWORD.
Follow the tutorial for detailed instructions on setting up AWS EC2 and automating deployment via GitHub Actions.
This project is licensed under the MIT License. See the LICENSE file for more details.