Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Notebooks/2_ModelTrainingAndDeployment/model-training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,21 @@
"label": null,
"name": "catalog_name",
"options": {
"validationRegex": null,
"widgetDisplayType": "Text"
"widgetDisplayType": "Text",
"validationRegex": null
},
"parameterDataType": "String"
},
"widgetInfo": {
"widgetType": "text",
"defaultValue": "pedroz_e2edata_dev",
"label": null,
"name": "catalog_name",
"options": {
"widgetType": "text",
"autoCreated": null,
"validationRegex": null,
"widgetType": "text"
},
"widgetType": "text"
"validationRegex": null
}
}
}
}
Expand Down
104 changes: 34 additions & 70 deletions Notebooks/3_Inference/realtime-inference.ipynb
Original file line number Diff line number Diff line change
@@ -1,70 +1,39 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This notebook is meant to show a simple example of how to use the realtime inference endpoint"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"application/vnd.databricks.v1+cell": {
"cellMetadata": {
"byteLimit": 2048000,
"rowLimit": 10000
},
"cellMetadata": {},
"inputWidgets": {},
"nuid": "f558f7fa-8486-4a4d-bcc5-c9a71312e9e4",
"nuid": "c349d0aa-264d-41f5-837b-72703eb01f87",
"showTitle": false,
"tableResultSettingsMap": {},
"title": ""
}
},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"import numpy as np\n",
"import pandas as pd\n",
"import json\n",
"from sklearn import datasets"
"# This notebook is meant to show a simple example of how to use the near real-time inference endpoint"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"application/vnd.databricks.v1+cell": {
"cellMetadata": {
"byteLimit": 2048000,
"rowLimit": 10000
},
"cellMetadata": {},
"inputWidgets": {},
"nuid": "1afd51a3-7462-4710-89c7-8244d7f2f11f",
"nuid": "62d91fa5-5a0d-4fbc-a81b-1850fd7b8d2f",
"showTitle": false,
"tableResultSettingsMap": {},
"title": ""
}
},
"outputs": [],
"source": [
"def create_tf_serving_json(data):\n",
" return {'inputs': {name: data[name].tolist() for name in data.keys()} if isinstance(data, dict) else data.tolist()}\n",
"\n",
"def score_model(dataset):\n",
" url = 'https://adb-4181970831265458.18.azuredatabricks.net/serving-endpoints/pedroz_e2edata_dev-default-iris_model-endpoint/invocations'\n",
" headers = {'Authorization': f'Bearer {os.environ.get(\"DATABRICKS_TOKEN\")}', 'Content-Type': 'application/json'}\n",
" ds_dict = {'dataframe_split': dataset.to_dict(orient='split')} if isinstance(dataset, pd.DataFrame) else create_tf_serving_json(dataset)\n",
" data_json = json.dumps(ds_dict, allow_nan=True)\n",
" response = requests.request(method='POST', headers=headers, url=url, data=data_json)\n",
" if response.status_code != 200:\n",
" raise Exception(f'Request failed with status {response.status_code}, {response.text}')\n",
" return response.json()"
"from databricks.sdk import WorkspaceClient"
]
},
{
Expand All @@ -77,30 +46,31 @@
"rowLimit": 10000
},
"inputWidgets": {},
"nuid": "298490eb-582e-4ba3-8818-8617fc36c94a",
"nuid": "f82f4dff-f1f0-42da-b982-2a83442d3ba6",
"showTitle": false,
"tableResultSettingsMap": {},
"title": ""
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/spark-d43833de-3f9d-486f-a93a-08/.ipykernel/2824/command-8394587244958649-400682212:4: FutureWarning: The default value of regex will change from True to False in a future version. In addition, single character regular expressions will *not* be treated as literal strings when regex=True.\n",
" df_samples.columns = df_samples.columns.str.replace(' ', '_').str.replace('(', '').str.replace(')', '')\n",
"/home/spark-d43833de-3f9d-486f-a93a-08/.ipykernel/2824/command-8394587244958649-400682212:4: FutureWarning: The default value of regex will change from True to False in a future version. In addition, single character regular expressions will *not* be treated as literal strings when regex=True.\n",
" df_samples.columns = df_samples.columns.str.replace(' ', '_').str.replace('(', '').str.replace(')', '')\n"
]
}
],
"outputs": [],
"source": [
"# Pull the dataset for running the inference\n",
"iris_samples = datasets.load_iris(as_frame=True)\n",
"df_samples = pd.DataFrame(data = iris_samples['data'], columns = iris_samples['feature_names'])\n",
"df_samples.columns = df_samples.columns.str.replace(' ', '_').str.replace('(', '').str.replace(')', '')\n",
"df_samples = df_samples.head(1)"
"w = WorkspaceClient()\n",
"\n",
"sample_input = [\n",
" {\n",
" \"sepal_length_cm\": 5.1,\n",
" \"sepal_width_cm\": 3.5,\n",
" \"petal_length_cm\": 1.4,\n",
" \"petal_width_cm\": 0.2\n",
" }\n",
"]\n",
"\n",
"prediction = w.serving_endpoints.query(\n",
" name=\"pedroz_e2edata_dev-default-iris_model-endpoint\",\n",
" dataframe_records=sample_input\n",
")\n",
"\n",
"display(prediction)"
]
},
{
Expand All @@ -113,26 +83,20 @@
"rowLimit": 10000
},
"inputWidgets": {},
"nuid": "5f5aa914-0c0e-4387-83e6-0e9930a2099e",
"nuid": "6f25d6d3-1170-47a6-837e-69f90f17e1dd",
"showTitle": false,
"tableResultSettingsMap": {},
"title": ""
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'predictions': [0]}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"score_model(df_samples)"
"print(\n",
" 'Input:',\n",
" sample_input,\n",
" '\\nOutput: ',\n",
" prediction.predictions\n",
")"
]
}
],
Expand All @@ -142,7 +106,7 @@
"dashboards": [],
"environmentMetadata": {
"base_environment": "",
"environment_version": "2"
"environment_version": "4"
},
"inputWidgetPreferences": null,
"language": "python",
Expand Down