API¶
churn_prediction.api.app
¶
FastAPI application entry point for the Churn Prediction Batch Inference API.
This module initializes the FastAPI application, configures global middleware, and manages the application lifecycle—specifically the loading and unloading of ML artifacts (OrdinalEncoder and XGBoost Booster) into the application state.
create_app()
¶
Create and configure the FastAPI application.
Initializes the application metadata, lifecycle management, CORS configuration, and API routers.
Returns:
| Type | Description |
|---|---|
FastAPI
|
Fully configured FastAPI application instance ready to serve prediction, explainability, download, and health endpoints. |
Source code in src/churn_prediction/api/app.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
lifespan(app)
async
¶
Manage startup and shutdown of the FastAPI application.
During startup, loads the fitted preprocessing encoder and trained
XGBoost model from the configured artifact directory and stores them in
app.state for dependency injection.
During shutdown, clears the references from application state so that model artifacts can be released from memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application instance whose lifecycle is being managed. |
required |
Yields:
| Type | Description |
|---|---|
None
|
Control is yielded to the running FastAPI application after all required ML artifacts have been loaded. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the required model, preprocessing artifact, or demonstration dataset cannot be found. |
Exception
|
If an ML artifact cannot be loaded successfully. |
Source code in src/churn_prediction/api/app.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
churn_prediction.api.dependencies
¶
Model and Encoder Dependency Providers.
This module contains utility functions used as FastAPI dependencies to retrieve the preprocessor (OrdinalEncoder) and the predictive model (XGBoost Booster) from the application state. It ensures that these artifacts are properly loaded before being used in request handlers, raising HTTP 503 errors if they are missing.
get_encoder(request)
¶
Retrieve the fitted encoder from FastAPI application state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Incoming FastAPI request providing access to the application state. |
required |
Returns:
| Type | Description |
|---|---|
OrdinalEncoder
|
Fitted ordinal encoder loaded during application startup. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
HTTP 503 error if the encoder has not been loaded into application state. |
Source code in src/churn_prediction/api/dependencies.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
get_model(request)
¶
Retrieve the trained XGBoost model from application state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Incoming FastAPI request providing access to the application state. |
required |
Returns:
| Type | Description |
|---|---|
Booster
|
Trained XGBoost booster loaded during application startup. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
HTTP 503 error if the model has not been loaded into application state. |
Source code in src/churn_prediction/api/dependencies.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
churn_prediction.api.routes.predict
¶
Inference API Module.
This module defines the FastAPI router and endpoints for generating batch predictions using a pre-trained XGBoost model and a scikit-learn OrdinalEncoder. It handles CSV file uploads, converts the incoming CSV payload into an optimized Parquet file on disk asynchronously, triggers the prediction pipeline, and provides a mechanism to download prediction artifacts.
PredictionResponse
¶
Bases: BaseModel
Response model for the prediction endpoint.
download_predictions(batch_id)
async
¶
Return the generated prediction CSV for a batch.
Locates the prediction artifact associated with batch_id and returns
it as a downloadable HTTP file response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_id
|
str
|
Unique prediction batch identifier used to locate the output artifact. |
required |
Returns:
| Type | Description |
|---|---|
FileResponse
|
HTTP response containing the generated prediction CSV. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
HTTP 400 if |
Source code in src/churn_prediction/api/routes/predict.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
predict_batch(file, encoder, model)
async
¶
Process an uploaded dataset and initiate batch churn prediction.
Accepts a customer CSV upload, persists the input data, executes the prediction pipeline using the pre-loaded encoder and XGBoost model, and returns metadata identifying the generated prediction batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
Uploaded CSV file containing customer records to score. |
required |
encoder
|
OrdinalEncoder
|
Pre-fitted encoder used to transform categorical input features. |
required |
model
|
Booster
|
Trained XGBoost booster used to generate churn predictions. |
required |
Returns:
| Type | Description |
|---|---|
PredictionResponse
|
Response containing the batch identifier, original filename, processing status, number of records, and prediction download URL. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the uploaded file is invalid, cannot be persisted, or the prediction pipeline fails. |
ValueError
|
If CSV-to-Parquet conversion or dataset preprocessing fails. |
ParserError
|
If the uploaded CSV cannot be parsed. |
Source code in src/churn_prediction/api/routes/predict.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
churn_prediction.api.routes.explain
¶
Explainability API Module.
This module provides endpoints to generate model interpretability reports using SHAP (SHapley Additive exPlanations) values for the churn prediction model. It integrates with the XGBoost booster and processed datasets to visualize feature contributions.
ExplainabilityResponse
¶
Bases: BaseModel
Response model for the explainability endpoint.
generate_explanation(batch_id, xgb_booster)
async
¶
Generate SHAP explainability artifacts for a prediction batch.
Loads the processed input features associated with the specified batch, computes SHAP explanations using the supplied XGBoost booster, and persists dashboard-ready explainability artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_id
|
str
|
Unique identifier of the prediction batch to explain. |
required |
xgb_booster
|
Booster
|
Trained XGBoost model used to calculate SHAP contributions. |
required |
Returns:
| Type | Description |
|---|---|
ExplainabilityResponse
|
Response containing the batch identifier and location of the generated explainability artifacts. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
HTTP 404 if the batch input artifact does not exist. HTTP 500 if data loading or SHAP artifact generation fails. |
Source code in src/churn_prediction/api/routes/explain.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
churn_prediction.api.routes.health
¶
Health Check API Module.
This module provides a FastAPI router with endpoints to monitor the operational status of the API. It verifies the availability of critical application state dependencies, specifically the encoder and the machine learning model, to ensure the service is ready to handle requests.
HealthResponse
¶
Bases: BaseModel
Response model for the health check endpoint.
check_health(request)
async
¶
Report API and machine-learning dependency health.
Verifies that the preprocessing encoder and XGBoost model are available in application state and reports the current process memory usage and health-check timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Incoming FastAPI request used to inspect application state. |
required |
Returns:
| Type | Description |
|---|---|
HealthResponse
|
Health information containing API status, encoder availability, model availability, memory utilization, and timestamp. |
Source code in src/churn_prediction/api/routes/health.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |