{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise: Call a Model API\n", "\n", "In this exercise, you will call a MLFlow model API to make predictions on a dataset." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Deploy a Model\n", "\n", "Remember we first need to deploy the model to be able to call it.\n", "\n", "1. First, you need to connect the terminal to the MLFlow Server by setting the `MLFLOW_TRACKING_URI` environment variable. \n", "\n", "```bash\n", "export MLFLOW_TRACKING_URI=http://localhost:5000\n", "```\n", "\n", "2. Then, you can deploy the model using the `mlflow models serve` command **in your terminal**:\n", "\n", "```bash\n", "mlflow models serve --model-uri models:/<>/ --port 5001 --env-manager conda\n", "```\n", "\n", "Where `` is the name of the model and `` 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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Get some data to make predictions.\n", " \n", "You can use the following code to get some data from the `diabetes` dataset:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agesexbmibps1s2s3s4s5s6
00.0380760.0506800.0616960.021872-0.044223-0.034821-0.043401-0.0025920.019907-0.017646
1-0.001882-0.044642-0.051474-0.026328-0.008449-0.0191630.074412-0.039493-0.068332-0.092204
20.0852990.0506800.044451-0.005670-0.045599-0.034194-0.032356-0.0025920.002861-0.025930
3-0.089063-0.044642-0.011595-0.0366560.0121910.024991-0.0360380.0343090.022688-0.009362
40.005383-0.044642-0.0363850.0218720.0039350.0155960.008142-0.002592-0.031988-0.046641
\n", "
" ], "text/plain": [ " age sex bmi bp s1 s2 s3 \\\n", "0 0.038076 0.050680 0.061696 0.021872 -0.044223 -0.034821 -0.043401 \n", "1 -0.001882 -0.044642 -0.051474 -0.026328 -0.008449 -0.019163 0.074412 \n", "2 0.085299 0.050680 0.044451 -0.005670 -0.045599 -0.034194 -0.032356 \n", "3 -0.089063 -0.044642 -0.011595 -0.036656 0.012191 0.024991 -0.036038 \n", "4 0.005383 -0.044642 -0.036385 0.021872 0.003935 0.015596 0.008142 \n", "\n", " s4 s5 s6 \n", "0 -0.002592 0.019907 -0.017646 \n", "1 -0.039493 -0.068332 -0.092204 \n", "2 -0.002592 0.002861 -0.025930 \n", "3 0.034309 0.022688 -0.009362 \n", "4 -0.002592 -0.031988 -0.046641 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn import datasets\n", "\n", "\n", "# Download dataset and convert to pandas dataframe\n", "diabetes_dataset = datasets.load_diabetes(as_frame=True)\n", "X = diabetes_dataset.data\n", "X.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Split the data to get the test set\n", "\n", "💡 Just as an example we can use the test set to make predictions. We can use the [`train_test_split` function](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html) from `sklearn.model_selection` to do this. We should store the split into `X_train`, `y_train`, `X_test`, `y_test`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "\n", "RANDOM_STATE = 42\n", "TEST_SIZE = 0.2\n", "\n", "# 👇 write the code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Prepare the data to be sent\n", "\n", "We need to prepare the data to be sent to the model API.\n", "\n", "- 👉 Create the **headers**\n", "- 👉 Create the **body**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 👇 write the code here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Convert the data to JSON\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "# 👇 write the code here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Send the request\n", "\n", "You can use the `requests` library to send the request to the model API. You can use the `requests.post` function to do this." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "# 👇 write the code here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Show the Response\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 👇 write the code here\n" ] } ], "metadata": { "kernelspec": { "display_name": "mlops-cookbook", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }