|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "cfd2162d-0f64-4a9d-a36b-01c05a05c6cc", |
4 | 6 | "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Verify Schema Step Demo\n", |
| 9 | + "This notebook walks through a simple example of using the verify-schema step in serving functions, by first downloading the step from the hub and inpecting it, then including the step in the function in two different approaches." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "id": "cc1eb162-f52b-4012-a5c8-07a2259bb7f7", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "## Get the step from the hub" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 2, |
| 23 | + "id": "d814bd50-4eac-4cb4-9eec-9f1b104df85e", |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "import mlrun" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 3, |
| 33 | + "id": "c6cd12de-1818-4e23-988e-6ede4837cd24", |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "hub_step = mlrun.get_hub_step(\"hub://verify_schema\")" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "id": "0a565753-b7da-409a-807d-d77e1ec98517", |
| 43 | + "metadata": {}, |
| 44 | + "source": [ |
| 45 | + "Now the file has been downloaded to our current working directory (provide `local_path` paramater to change this default destination), and we can edit it directly. \n", |
| 46 | + "\n", |
| 47 | + "We can also inspect the `hub_stpe` object:" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 5, |
| 53 | + "id": "37ba4e3b-e337-4651-ba60-69724b00d3eb", |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [ |
| 56 | + { |
| 57 | + "data": { |
| 58 | + "text/plain": [ |
| 59 | + "{'filename': 'verify_schema.py',\n", |
| 60 | + " 'example': 'verify_schema.ipynb',\n", |
| 61 | + " 'local_path': PosixPath('/User'),\n", |
| 62 | + " 'url': 'hub://verify_schema',\n", |
| 63 | + " 'class_name': 'VerifySchema',\n", |
| 64 | + " 'name': 'verify_schema',\n", |
| 65 | + " 'version': '1.0.0',\n", |
| 66 | + " 'categories': ['data-preparation', 'model-serving', 'utilities'],\n", |
| 67 | + " 'description': 'Verifies the event is aligned with the provided schema'}" |
| 68 | + ] |
| 69 | + }, |
| 70 | + "execution_count": 5, |
| 71 | + "metadata": {}, |
| 72 | + "output_type": "execute_result" |
| 73 | + } |
| 74 | + ], |
| 75 | + "source": [ |
| 76 | + "hub_step.to_dict()" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "id": "8f702135-5dff-4355-bc5c-da9146b3eff7", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "## Use custom step in a function" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "id": "73ed3007-f40f-4da8-a3a7-cd9933491322", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "When we are done with editing the step code, we are ready to use it in a function, as any other custom step:" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 7, |
| 98 | + "id": "9f26755e-5053-4103-9fcd-86bf839f6402", |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [ |
| 101 | + { |
| 102 | + "name": "stdout", |
| 103 | + "output_type": "stream", |
| 104 | + "text": [ |
| 105 | + "> 2025-12-31 09:00:01,987 [info] Project loaded successfully: {\"project_name\":\"verify-schema-step-demo\"}\n" |
| 106 | + ] |
| 107 | + } |
| 108 | + ], |
| 109 | + "source": [ |
| 110 | + "project = mlrun.get_or_create_project(\"verify-schema-step-demo\",'./verify-schema-step-demo')" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": 10, |
| 116 | + "id": "20a3cd67-b266-4b2d-9016-2139a2ab7f53", |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "fn = project.set_function(hub_step.get_src_file_path(), name=\"serving-fn\", kind=\"serving\", image = \"mlrun/mlrun\" )\n", |
| 121 | + "graph = fn.set_topology(\"flow\", engine=\"async\")\n", |
| 122 | + "schema = [\"id\", \"height\", \"weight\"]\n", |
| 123 | + "graph.to(class_name=\"VerifySchema\", name=\"verify\", schema=schema)" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 9, |
| 129 | + "id": "8b63efcf-22b0-482b-853c-a4991814693b", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "project.deploy_function(fn)" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": 11, |
| 139 | + "id": "443a9138-e1b1-41e0-8a60-dc28f2680af9", |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [ |
| 142 | + { |
| 143 | + "data": { |
| 144 | + "text/plain": [ |
| 145 | + "{'id': '3425', 'height': 157, 'weight': 58}" |
| 146 | + ] |
| 147 | + }, |
| 148 | + "execution_count": 11, |
| 149 | + "metadata": {}, |
| 150 | + "output_type": "execute_result" |
| 151 | + } |
| 152 | + ], |
| 153 | + "source": [ |
| 154 | + "serving_fn = project.get_function(\"serving-fn\")\n", |
| 155 | + "event = {\n", |
| 156 | + " \"id\": \"3425\",\n", |
| 157 | + " \"height\": 157,\n", |
| 158 | + " \"weight\": 58\n", |
| 159 | + "}\n", |
| 160 | + "serving_fn.invoke(\"/\", body=event)" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "id": "4933f6c7-6442-4756-812c-fd5a257ee540", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "Or try to raise an error:" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": 12, |
| 174 | + "id": "98b8ff5a-783c-414c-bca7-eb5efe702b70", |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [], |
| 177 | + "source": [ |
| 178 | + "event = {\n", |
| 179 | + " \"id\": \"3425\",\n", |
| 180 | + " \"height\": 157\n", |
| 181 | + "}\n", |
| 182 | + "serving_fn.invoke(\"/\", body=event)" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "markdown", |
| 187 | + "id": "83182c10-223c-473d-a17a-2ddc94dd0821", |
| 188 | + "metadata": {}, |
| 189 | + "source": [ |
| 190 | + "## Add step directly from the hub\n", |
| 191 | + "In case you don't need to make any changes to the step code, you can add the step directly from the hub: " |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
5 | 195 | "cell_type": "code", |
| 196 | + "execution_count": 13, |
| 197 | + "id": "7ce84cdc-8a32-4db9-aa73-171a622d1617", |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [ |
| 201 | + "graph.add_step(class_name=\"hub://verify_schema\", name=\"verify-2\", schema=schema).respond()" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": 14, |
| 207 | + "id": "16ff979d-67e3-40c6-93de-aacd50b80fc1", |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [], |
| 210 | + "source": [ |
| 211 | + "project.deploy_function(fn)" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": 15, |
| 217 | + "id": "bf83904c-7be3-4eaa-9564-386bd7ebe77b", |
| 218 | + "metadata": {}, |
6 | 219 | "outputs": [], |
7 | | - "execution_count": null, |
8 | | - "source": "", |
9 | | - "id": "556b36b9b89d0515" |
| 220 | + "source": [ |
| 221 | + "event = {\n", |
| 222 | + " \"id\": \"3425\",\n", |
| 223 | + " \"height\": 157,\n", |
| 224 | + " \"weight\": 58\n", |
| 225 | + "}\n", |
| 226 | + "serving_fn.invoke(\"/\", body=event)" |
| 227 | + ] |
10 | 228 | } |
11 | 229 | ], |
12 | 230 | "metadata": { |
13 | 231 | "kernelspec": { |
14 | | - "display_name": "Python 3", |
| 232 | + "display_name": "mlrun-base-py311", |
15 | 233 | "language": "python", |
16 | | - "name": "python3" |
| 234 | + "name": "conda-env-mlrun-base-py311-py" |
17 | 235 | }, |
18 | 236 | "language_info": { |
19 | 237 | "codemirror_mode": { |
20 | 238 | "name": "ipython", |
21 | | - "version": 2 |
| 239 | + "version": 3 |
22 | 240 | }, |
23 | 241 | "file_extension": ".py", |
24 | 242 | "mimetype": "text/x-python", |
25 | 243 | "name": "python", |
26 | 244 | "nbconvert_exporter": "python", |
27 | | - "pygments_lexer": "ipython2", |
28 | | - "version": "2.7.6" |
| 245 | + "pygments_lexer": "ipython3", |
| 246 | + "version": "3.11.12" |
29 | 247 | } |
30 | 248 | }, |
31 | 249 | "nbformat": 4, |
|
0 commit comments