score_model.py
1 import pandas as pd 2 import requests 3 from sktime.datasets import load_longley 4 from sktime.forecasting.model_selection import temporal_train_test_split 5 6 y, X = load_longley() 7 y_train, y_test, X_train, X_test = temporal_train_test_split(y, X) 8 9 # Define local host and endpoint url 10 host = "127.0.0.1" 11 url = f"http://{host}:5000/invocations" 12 13 # Model scoring via REST API requires transforming the configuration DataFrame 14 # into JSON format. As numpy ndarray type is not JSON serializable we need to 15 # convert the exogenous regressor into a list. The wrapper instance will convert 16 # the list back to ndarray type as required by sktime predict methods. For more 17 # details read the MLflow deployment API reference. 18 # (https://mlflow.org/docs/latest/models.html#deploy-mlflow-models) 19 X_test_list = X_test.to_numpy().tolist() 20 predict_conf = pd.DataFrame([ 21 { 22 "fh": [1, 2, 3, 4], 23 "predict_method": "predict_interval", 24 "coverage": [0.9, 0.95], 25 "X": X_test_list, 26 } 27 ]) 28 29 # Create dictionary with pandas DataFrame in the split orientation 30 json_data = {"dataframe_split": predict_conf.to_dict(orient="split")} 31 32 # Score model 33 response = requests.post(url, json=json_data) 34 print(f"\nPyfunc 'predict_interval':\n${response.json()}")