Exercise: Call a Model API¶
In this exercise, you will call a MLFlow model API to make predictions on a dataset.
1. Deploy a Model¶
Remember we first need to deploy the model to be able to call it.
- First, you need to connect the terminal to the MLFlow Server by setting the
MLFLOW_TRACKING_URIenvironment variable.
export MLFLOW_TRACKING_URI=http://localhost:5000
- Then, you can deploy the model using the
mlflow models servecommand in your terminal:
mlflow models serve --model-uri models:/<>/<model_version> --port 5001 --env-manager conda
Where <model_name> is the name of the model and <model_version> is the version of the model you want to deploy. You can find the name and version of the model in the MLFlow UI. Also the --port argument is the port where the server will be running. It's important to choose a port different than the 5000 port where the MLFlow server is running. The --env-manager argument is the environment manager that MLFlow will use to run the model. In this case, we are using conda but you can use pip or docker as well.
2. Get some data to make predictions.¶
You can use the following code to get some data from the diabetes dataset:
from sklearn import datasets
# Download dataset and convert to pandas dataframe
diabetes_dataset = datasets.load_diabetes(as_frame=True)
X = diabetes_dataset.data
X.head()
| age | sex | bmi | bp | s1 | s2 | s3 | s4 | s5 | s6 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.038076 | 0.050680 | 0.061696 | 0.021872 | -0.044223 | -0.034821 | -0.043401 | -0.002592 | 0.019907 | -0.017646 |
| 1 | -0.001882 | -0.044642 | -0.051474 | -0.026328 | -0.008449 | -0.019163 | 0.074412 | -0.039493 | -0.068332 | -0.092204 |
| 2 | 0.085299 | 0.050680 | 0.044451 | -0.005670 | -0.045599 | -0.034194 | -0.032356 | -0.002592 | 0.002861 | -0.025930 |
| 3 | -0.089063 | -0.044642 | -0.011595 | -0.036656 | 0.012191 | 0.024991 | -0.036038 | 0.034309 | 0.022688 | -0.009362 |
| 4 | 0.005383 | -0.044642 | -0.036385 | 0.021872 | 0.003935 | 0.015596 | 0.008142 | -0.002592 | -0.031988 | -0.046641 |
3. Split the data to get the test set¶
💡 Just as an example we can use the test set to make predictions. We can use the train_test_split function from sklearn.model_selection to do this. We should store the split into X_train, y_train, X_test, y_test.
from sklearn.model_selection import train_test_split
RANDOM_STATE = 42
TEST_SIZE = 0.2
# 👇 write the code here
4. Prepare the data to be sent¶
We need to prepare the data to be sent to the model API.
- 👉 Create the headers
- 👉 Create the body
# 👇 write the code here
5. Convert the data to JSON¶
We need to convert the data to JSON to be able to send it to the model API. You can use the json.dumps function from the json module to do this.
import json
# 👇 write the code here
6. Send the request¶
You can use the requests library to send the request to the model API. You can use the requests.post function to do this.
import requests
# 👇 write the code here
7. Show the Response¶
You can use the response.json() method to show the response from the model API. Also check the response.status_code to see if the request was successful.
# 👇 write the code here