Overview
The Prediction API is a FastAPI service that provides real-time and batch prediction endpoints for student purchase probability. The service loads trained model artifacts, applies feature engineering, and returns predictions with configurable thresholds.Architecture
- Framework: FastAPI 1.2.0
- Model Loading: joblib artifacts loaded at startup
- Feature Engineering: Runtime feature transformation via
FeatureConfig - Monitoring: In-memory tracking of predictions for drift detection
- Logging: JSON Lines prediction log for audit trails
Request/Response Schemas
PredictRequest
PredictResponse
BatchPredictRequest
BatchPredictResponse
API Endpoints
POST /predict
Single prediction endpoint. Request Example:POST /batch_predict
Batch prediction endpoint for multiple records. Request Example:GET /health
Health check endpoint. Response Schema:Artifact Loading
The service loads artifacts during startup via theload_artifacts() function (src/api.py:195):
- Model: Loaded from
artifacts/purchase_model.pklusing joblib - Threshold: Read from
artifacts/threshold.txtas float - Feature Config: Built from
config.yamlwith epsilon and engagement weights - Drift Baseline: Loaded from
artifacts/drift_baseline.json(optional)
Feature Engineering
The API applies runtime feature engineering usingadd_engineered_features() from src/features.py. The FeatureConfig includes:
epsilon: Small constant to avoid division by zerominutes_watched_weight: Weight for engagement scoredays_on_platform_weight: Weight for engagement scorecourses_started_weight: Weight for engagement score
Validation
The service validates thatpractice_exams_passed cannot exceed practice_exams_started (src/api.py:243-248). Invalid requests return HTTP 422.
Prediction Logging
All predictions are logged toartifacts/prediction_log.jsonl in JSON Lines format:
config.yaml under monitoring.prediction_log_file.
Starting the Service
Start the API using uvicorn:Error Handling
- 503 Service Unavailable: Model artifacts not loaded
- 422 Unprocessable Entity: Validation errors (e.g., invalid field values)
- 500 Internal Server Error: Unexpected errors during prediction
Related
- Monitoring - Drift detection and retraining triggers
- Streaming Pipelines - Real-time inference orchestration