Day 5
Day 5: MLOps Model Deployment Simulation (API Endpoint)
🚀 Objective
The goal of Day 5 was to transition the machine learning model, tracked and saved on Day 4, into a simulated production environment . This involved creating a service that could:
Load the saved model and preprocessor (scaler) artifacts.
Define a prediction function that mimics a REST API endpoint to serve real-time inferences.
This exercise successfully demonstrated the critical "deployment" step in the MLOps lifecycle, where saved artifacts are operationalized for continuous use.
🛠️ Key Concepts Covered
Artifact Loading: Used Python's built-in pickle library to deserialize (load) the trained model.pkl and scaler.pkl.
Prediction Pipeline: Implemented the PredictionService class, which ensures that new, incoming data is transformed/scaled exactly as the training data was before prediction, preventing data skew.
API Simulation: Defined the predict_endpoint method to take raw, JSON-like data and return a structured prediction, mirroring a real-world microservice.
⚙️ Setup and Execution
Prerequisites
Ensure you have the following Python libraries installed:
pip install pandas numpy scikit-learn
Running the Simulation
The script (deployment_api_simulation.py) automatically checks for the model artifacts in the mlruns/Default_Risk_Model/latest_run/artifacts path.
If artifacts are not found, a set of dummy model.pkl and scaler.pkl files will be created so the deployment simulation can run successfully.
Execute the script from your terminal:
python deployment_api_simulation.py
🧠 Expected Output
The script tests the deployment service with an input that simulates a high-risk customer (low age, low income, low education) and confirms the model returns a positive risk prediction (1).
--- Running Day 5: Deployment Simulation (Using Actual Model Prediction) ---
[MOCK] Artifacts not found.
Creating dummy model.pkl and scaler.pkl for simulation...
Dummy artifacts created successfully in mlruns/Default_Risk_Model/latest_run/artifacts
Scaler loaded successfully from: mlruns/Default_Risk_Model/latest_run/artifacts/scaler.pkl
Model loaded successfully from: mlruns/Default_Risk_Model/latest_run/artifacts/model.pkl
--- Simulated API Request for HIGH-RISK Customer --- Incoming Data: {'Age': 19, 'Income': 20000, 'Education': 1} Transformed Input Shape: (1, 3)