Model Deployment
Bluemist provides integration with FastAPI to deploy the ML model as RESTful API. This can be simply achieved by calling deploy_model as described here
Warning
deploy_model should only be executed after train_test_evaluate has been completed successfully
Examples
In [1]:
pip install bluemist
In [2]:
import pandas as pd
from bluemist.environment import initialize
from bluemist.preprocessing import preprocess_data
from bluemist.regression import train_test_evaluate, deploy_model
In [3]:
initialize()
data = pd.read_csv('https://www4.stat.ncsu.edu/~boos/var.select/diabetes.tab.txt', sep='\t')
data.head()
██████╗ ██╗ ██╗ ██╗███████╗███╗ ███╗██╗███████╗████████╗ █████╗ ██╗
██╔══██╗██║ ██║ ██║██╔════╝████╗ ████║██║██╔════╝╚══██╔══╝ ██╔══██╗██║
██████╔╝██║ ██║ ██║█████╗ ██╔████╔██║██║███████╗ ██║ ███████║██║
██╔══██╗██║ ██║ ██║██╔══╝ ██║╚██╔╝██║██║╚════██║ ██║ ██╔══██║██║
██████╔╝███████╗╚██████╔╝███████╗██║ ╚═╝ ██║██║███████║ ██║ ██║ ██║██║
(version 0.1.1)
Bluemist path :: /home/shashank-agrawal/PycharmProjects/bluemist-ai/bluemist
System platform :: posix, Linux, 5.19.0-31-generic, linux-x86_64, ('64bit', 'ELF')
Out[3]:
| AGE | SEX | BMI | BP | S1 | S2 | S3 | S4 | S5 | S6 | Y | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 59 | 2 | 32.1 | 101.0 | 157 | 93.2 | 38.0 | 4.0 | 4.8598 | 87 | 151 |
| 1 | 48 | 1 | 21.6 | 87.0 | 183 | 103.2 | 70.0 | 3.0 | 3.8918 | 69 | 75 |
| 2 | 72 | 2 | 30.5 | 93.0 | 156 | 93.6 | 41.0 | 4.0 | 4.6728 | 85 | 141 |
| 3 | 24 | 1 | 25.3 | 84.0 | 198 | 131.4 | 40.0 | 5.0 | 4.8903 | 89 | 206 |
| 4 | 50 | 1 | 23.0 | 101.0 | 192 | 125.4 | 52.0 | 4.0 | 4.2905 | 80 | 135 |
In [4]:
# Categorical encoding using OneHotEncoder
X_train, X_test, y_train, y_test = preprocess_data(data,
target_variable='Y',
test_size=0.25,
data_scaling_strategy='StandardScaler',
categorical_features=['SEX'],
categorical_encoder='OneHotEncoder',
drop_categories_one_hot_encoder='first')
X_train.head()
Out[4]:
| AGE | BMI | BP | S1 | S2 | S3 | S4 | S5 | S6 | SEX_2 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -2.256402 | -0.237583 | -0.834117 | 0.975180 | 1.289605 | -0.302383 | 0.687862 | 0.125787 | -0.668411 | 0.0 |
| 1 | 1.230453 | 1.618094 | 0.520919 | -0.006013 | 0.320029 | -1.158176 | 0.687862 | 0.605781 | 0.916937 | 1.0 |
| 2 | -1.801595 | -0.644927 | -0.477529 | -1.323615 | -1.127958 | 0.397811 | -0.818322 | -2.042113 | -0.756486 | 1.0 |
| 3 | 0.320838 | 1.527573 | 1.590684 | -0.202252 | 0.090392 | -0.146785 | -0.065230 | -0.516943 | 1.269236 | 1.0 |
| 4 | 0.624043 | 1.188120 | 1.590684 | 0.975180 | -0.273199 | -0.769179 | 1.440954 | 2.494051 | 1.885760 | 1.0 |
In [5]:
# Train and compare models
train_test_evaluate(X_train, X_test, y_train, y_test, target_scaling_strategy='MinMaxScaler')
Training TweedieRegressor: 100%|██████████| 46/46 [00:20<00:00, 2.25it/s]
| mean_absolute_error | mean_squared_error | r2_score | Exception | |
|---|---|---|---|---|
| Estimator | ||||
| ARDRegression | 44.905751 | 3132.297360 | 0.454205 | nan |
| AdaBoostRegressor | 48.719321 | 3818.411711 | 0.334652 | nan |
| BaggingRegressor | 50.486486 | 4114.979459 | 0.282976 | nan |
| BayesianRidge | 45.401862 | 3169.959732 | 0.447643 | nan |
| CCA | 55.569805 | 4685.287437 | 0.183601 | nan |
| DecisionTreeRegressor | 71.225225 | 8269.369369 | -0.440916 | nan |
| DummyRegressor | 64.621513 | 5793.204870 | -0.009451 | nan |
| ElasticNet | 64.621513 | 5793.204870 | -0.009451 | nan |
| ElasticNetCV | 45.415690 | 3186.492960 | 0.444762 | nan |
| ExtraTreeRegressor | 67.666667 | 7536.837838 | -0.313274 | nan |
| ExtraTreesRegressor | 45.631892 | 3580.983005 | 0.376023 | nan |
| GammaRegressor | nan | nan | nan | Some value(s) of y are out of the valid range of the loss 'HalfGammaLoss'. |
| GaussianProcessRegressor | 67.216723 | 8630.149234 | -0.503781 | nan |
| GradientBoostingRegressor | 48.374687 | 3934.861451 | 0.314361 | nan |
| HistGradientBoostingRegressor | 48.686275 | 4010.794382 | 0.301130 | nan |
| HuberRegressor | 44.761295 | 3126.524871 | 0.455211 | nan |
| KNeighborsRegressor | 52.052252 | 4569.992432 | 0.203691 | nan |
| KernelRidge | 93.436868 | 13826.460935 | -1.409225 | nan |
| Lars | 45.033817 | 3118.568339 | 0.456598 | nan |
| LarsCV | 45.606204 | 3222.114649 | 0.438555 | nan |
| Lasso | 64.621513 | 5793.204870 | -0.009451 | nan |
| LassoCV | 45.512171 | 3209.088449 | 0.440825 | nan |
| LassoLars | 64.621513 | 5793.204870 | -0.009451 | nan |
| LassoLarsCV | 45.606204 | 3222.114649 | 0.438555 | nan |
| LassoLarsIC | 45.421964 | 3191.847968 | 0.443829 | nan |
| LinearRegression | 45.033817 | 3118.568339 | 0.456598 | nan |
| LinearSVR | 44.688204 | 3056.343818 | 0.467440 | nan |
| MLPRegressor | 51.895604 | 4568.649034 | 0.203925 | nan |
| NuSVR | 48.338294 | 4112.313615 | 0.283440 | nan |
| OrthogonalMatchingPursuit | 52.597461 | 4691.442401 | 0.182528 | nan |
| OrthogonalMatchingPursuitCV | 45.962764 | 3298.233809 | 0.425291 | nan |
| PLSCanonical | 90.688422 | 11521.103125 | -1.007522 | nan |
| PLSRegression | 45.026657 | 3129.231385 | 0.454740 | nan |
| PassiveAggressiveRegressor | 62.835621 | 6030.485222 | -0.050796 | nan |
| PoissonRegressor | 52.590634 | 4032.492444 | 0.297349 | nan |
| QuantileRegressor | 64.351351 | 6050.441443 | -0.054274 | nan |
| RANSACRegressor | 51.590759 | 4285.951082 | 0.253184 | nan |
| RadiusNeighborsRegressor | 49.636364 | 4217.090909 | 0.324830 | nan |
| RandomForestRegressor | 47.074234 | 3831.500310 | 0.332371 | nan |
| Ridge | 45.110837 | 3130.333945 | 0.454547 | nan |
| RidgeCV | 45.336901 | 3161.739093 | 0.449075 | nan |
| SGDRegressor | 47.938802 | 3587.937037 | 0.374811 | nan |
| SVR | 49.777276 | 4135.553245 | 0.279391 | nan |
| TheilSenRegressor | 46.437096 | 3422.785127 | 0.403589 | nan |
| TransformedTargetRegressor | 45.033817 | 3118.568339 | 0.456598 | nan |
| TweedieRegressor | 48.923752 | 3586.863151 | 0.374998 | nan |
In [ ]:
deploy_model(estimator_name='LinearSVR', host='localhost', port=8000)
INFO: Started server process [167513] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:40194 - "GET / HTTP/1.1" 404 Not Found INFO: 127.0.0.1:40194 - "GET /docs HTTP/1.1" 200 OK INFO: 127.0.0.1:40194 - "GET /openapi.json HTTP/1.1" 200 OK INFO: 127.0.0.1:40210 - "POST /predict HTTP/1.1" 200 OK
To test the API, open the browser and navigate to http://localhost:8000/docs