|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "7e44f382", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Local Model Serving with MLflow" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 37, |
| 14 | + "id": "f7c4b8ed", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import httpx \n", |
| 19 | + "import json\n", |
| 20 | + "import mlflow\n", |
| 21 | + "from mlflow_for_ml_dev.src.utils.folder_operations import get_project_root\n", |
| 22 | + "\n", |
| 23 | + "# set mlflow tracking uri\n", |
| 24 | + "mlflow.set_tracking_uri(uri=(get_project_root() / 'mlruns').as_uri())" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "id": "94833c27", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "```shell\n", |
| 33 | + "mlflow models serve --options\n", |
| 34 | + "```\n", |
| 35 | + "\n", |
| 36 | + "To run the code below make sure you deploy the model using.\n", |
| 37 | + "\n", |
| 38 | + "`poetry run mlflow models serve --model-uri models:/Iris_Classifier_Model@production --env-manager local`" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "id": "358ba627", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "## Scoring Iris Classifier Model" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 50, |
| 52 | + "id": "096a2fa0", |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "url = \"http://127.0.0.1:5001/invocations\"\n", |
| 57 | + "\n", |
| 58 | + "payload = {'dataframe_split':\n", |
| 59 | + " {\n", |
| 60 | + " 'columns': ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], \n", |
| 61 | + " 'data': [[6.1, 2.8, 4.7, 1.2]]\n", |
| 62 | + " }\n", |
| 63 | + " } \n", |
| 64 | + "headers = {\"Content-Type\": \"application/json\"}\n", |
| 65 | + "response = httpx.post(url, data=json.dumps(payload), headers=headers)\n" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 51, |
| 71 | + "id": "4cee092a", |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [ |
| 74 | + { |
| 75 | + "data": { |
| 76 | + "text/plain": [ |
| 77 | + "<Response [200 OK]>" |
| 78 | + ] |
| 79 | + }, |
| 80 | + "execution_count": 51, |
| 81 | + "metadata": {}, |
| 82 | + "output_type": "execute_result" |
| 83 | + } |
| 84 | + ], |
| 85 | + "source": [ |
| 86 | + "response" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "2ce72d97", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "## Signature validation" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 52, |
| 100 | + "id": "cba325a0", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "url = \"http://127.0.0.1:5001/invocations\"\n", |
| 105 | + "\n", |
| 106 | + "payload = {'dataframe_split':\n", |
| 107 | + " {\n", |
| 108 | + " 'columns': ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], \n", |
| 109 | + " 'data': [[6.1, 2.8, 4.7, \"string\"]] # invalid data type\n", |
| 110 | + " }\n", |
| 111 | + " } \n", |
| 112 | + "headers = {\"Content-Type\": \"application/json\"}\n", |
| 113 | + "response = httpx.post(url, data=json.dumps(payload), headers=headers)" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 53, |
| 119 | + "id": "dadf245c", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "data": { |
| 124 | + "text/plain": [ |
| 125 | + "<Response [400 BAD REQUEST]>" |
| 126 | + ] |
| 127 | + }, |
| 128 | + "execution_count": 53, |
| 129 | + "metadata": {}, |
| 130 | + "output_type": "execute_result" |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "response" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": 54, |
| 140 | + "id": "da6b0d77", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "data": { |
| 145 | + "text/plain": [ |
| 146 | + "{'error_code': 'BAD_REQUEST',\n", |
| 147 | + " 'message': 'Invalid input. Data is not compatible with model signature. Failed to convert column petal width (cm) to type \\'float64\\'. Error: \\'ValueError(\"could not convert string to float: \\'string\\'\")\\''}" |
| 148 | + ] |
| 149 | + }, |
| 150 | + "execution_count": 54, |
| 151 | + "metadata": {}, |
| 152 | + "output_type": "execute_result" |
| 153 | + } |
| 154 | + ], |
| 155 | + "source": [ |
| 156 | + "response.json()" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "id": "0b064f0c", |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [] |
| 166 | + } |
| 167 | + ], |
| 168 | + "metadata": { |
| 169 | + "kernelspec": { |
| 170 | + "display_name": ".venv", |
| 171 | + "language": "python", |
| 172 | + "name": "python3" |
| 173 | + }, |
| 174 | + "language_info": { |
| 175 | + "codemirror_mode": { |
| 176 | + "name": "ipython", |
| 177 | + "version": 3 |
| 178 | + }, |
| 179 | + "file_extension": ".py", |
| 180 | + "mimetype": "text/x-python", |
| 181 | + "name": "python", |
| 182 | + "nbconvert_exporter": "python", |
| 183 | + "pygments_lexer": "ipython3", |
| 184 | + "version": "3.11.9" |
| 185 | + } |
| 186 | + }, |
| 187 | + "nbformat": 4, |
| 188 | + "nbformat_minor": 5 |
| 189 | +} |
0 commit comments