|
4 | 4 |
|
5 | 5 | from flask import request, jsonify, Response, current_app
|
6 | 6 |
|
| 7 | +from gradient_boosting_model import __version__ as shadow_version |
| 8 | +from regression_model import __version__ as live_version |
| 9 | +from prometheus_client import Histogram, Gauge, Info |
7 | 10 | from gradient_boosting_model.predict import make_prediction
|
8 | 11 | from api.persistence.data_access import PredictionPersistence, ModelType
|
| 12 | +from api.config import APP_NAME |
9 | 13 |
|
10 | 14 |
|
11 | 15 | _logger = logging.getLogger(__name__)
|
12 | 16 |
|
| 17 | +PREDICTION_TRACKER = Histogram( |
| 18 | + name='house_price_prediction_dollars', |
| 19 | + documentation='ML Model Prediction on House Price', |
| 20 | + labelnames=['app_name', 'model_name', 'model_version'] |
| 21 | +) |
| 22 | + |
| 23 | +PREDICTION_GAUGE = Gauge( |
| 24 | + name='house_price_gauge_dollars', |
| 25 | + documentation='ML Model Prediction on House Price for min max calcs', |
| 26 | + labelnames=['app_name', 'model_name', 'model_version'] |
| 27 | +) |
| 28 | + |
| 29 | +PREDICTION_GAUGE.labels( |
| 30 | + app_name=APP_NAME, |
| 31 | + model_name=ModelType.LASSO.name, |
| 32 | + model_version=live_version) |
| 33 | + |
| 34 | +MODEL_VERSIONS = Info( |
| 35 | + 'model_version_details', |
| 36 | + 'Capture model version information', |
| 37 | +) |
| 38 | + |
| 39 | +MODEL_VERSIONS.info({ |
| 40 | + 'live_model': ModelType.LASSO.name, |
| 41 | + 'live_version': live_version, |
| 42 | + 'shadow_model': ModelType.GRADIENT_BOOSTING.name, |
| 43 | + 'shadow_version': shadow_version}) |
| 44 | + |
13 | 45 |
|
14 | 46 | def health():
|
15 | 47 | if request.method == "GET":
|
@@ -47,7 +79,18 @@ def predict():
|
47 | 79 | _logger.warning(f"errors during prediction: {result.errors}")
|
48 | 80 | return Response(json.dumps(result.errors), status=400)
|
49 | 81 |
|
50 |
| - # Step 4: Prepare prediction response |
| 82 | + # Step 4: Monitoring |
| 83 | + for _prediction in result.predictions: |
| 84 | + PREDICTION_TRACKER.labels( |
| 85 | + app_name=APP_NAME, |
| 86 | + model_name=ModelType.LASSO.name, |
| 87 | + model_version=live_version).observe(_prediction) |
| 88 | + PREDICTION_GAUGE.labels( |
| 89 | + app_name=APP_NAME, |
| 90 | + model_name=ModelType.LASSO.name, |
| 91 | + model_version=live_version).set(_prediction) |
| 92 | + |
| 93 | + # Step 5: Prepare prediction response |
51 | 94 | return jsonify(
|
52 | 95 | {
|
53 | 96 | "predictions": result.predictions,
|
|
0 commit comments