This repository contains a complete MLOps project for predicting customer subscription likelihood using machine learning models. The project includes model training, MLflow tracking, API service for model serving, and containerization.
api_service.py: FastAPI service for model predictions with caching and error handlingmain.ipynb: Main Jupyter notebook containing data analysis and model developmenttrain_and_log_lr.py: Script for training and logging the LogisticRegression modelmlflow_log_model.py: Utilities for logging models to MLflowpreprocessing.py: Data preprocessing utilitiestest_api.py: API service teststest_customer_profiles.py: Test cases for different customer profilesDockerfile: Container definition for the API servicedocker-compose.yml: Service orchestration configurationdata/: Directory containing dataset filesmodels/: Saved model filesmlruns/: MLflow tracking information
- Machine Learning Models:
- Random Forest Classifier
- Logistic Regression
- MLflow Integration:
- Model versioning
- Experiment tracking
- Metrics logging
- FastAPI Service:
- Model prediction endpoints
- Request validation
- Error handling
- Response caching
- CORS and Gzip middleware
- Docker Support:
- Containerized API service
- Multi-stage builds
- Environment configuration
-
Clone the repository:
git clone https://github.com/Jsznn/mlops-project-2.git cd mlops-project-2 -
Install dependencies:
pip install -r requirements.txt
-
Run the API service:
python api_service.py
Or using Docker:
docker-compose up
The API provides endpoints for making predictions using both Random Forest and Logistic Regression models:
GET /: Root endpoint with API informationGET /models: List available models and their versionsPOST /predict/best_rf: Get predictions from Random Forest modelPOST /predict/best_lr: Get predictions from Logistic Regression model
curl -X POST "http://localhost:8000/predict/best_rf" \
-H "Content-Type: application/json" \
-d '{
"usia": 24,
"pekerjaan": "manajer",
"status_perkawinan": "lajang",
"pendidikan": "Pendidikan Tinggi",
"gagal_bayar_sebelumnya": "no",
"pinjaman_rumah": "yes",
"pinjaman_pribadi": "no",
"jenis_kontak": "cellular",
"bulan_kontak_terakhir": "jul",
"hari_kontak_terakhir": "fri",
"jumlah_kontak_kampanye_ini": 2,
"hari_sejak_kontak_sebelumnya": 999,
"jumlah_kontak_sebelumnya": 0,
"hasil_kampanye_sebelumnya": "nonexistent",
"tingkat_variasi_pekerjaan": -1.7,
"indeks_harga_konsumen": 94.215,
"indeks_kepercayaan_konsumen": -40.3,
"suku_bunga_euribor_3bln": 0.885,
"jumlah_pekerja": 4991.6,
"pulau": "Papua"
}'{
"model_name": "best_rf",
"prediction": 1,
"probability_subscribe": 0.7555,
"model_version": "local"
}- Run tests:
python -m pytest test_api.py test_customer_profiles.py - Check API documentation: Visit
http://localhost:8000/docswhen API is running - Monitor MLflow: Start MLflow UI with
mlflow ui
MIT