Skip to content

Commit 27e52f8

Browse files
committed
added example for Single Model Serving
1 parent 1527514 commit 27e52f8

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

5_rest_requests_single_model.ipynb

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "55c8afde-9b18-4b6a-9ee5-33924bdb4f16",
6+
"metadata": {},
7+
"source": [
8+
"# REST Inference for single-model server deployment \n",
9+
"\n",
10+
"_Note: Use this procedure for testing a model that you deployed on a single-model server. See `3_rest_requests_multi_model.ipynb` for testing a model that you deployed on a multi-model server._"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "2c004acc-13cd-4917-8480-592c7c2d623b",
16+
"metadata": {},
17+
"source": [
18+
"## First, replace the placeholder with the *url* or *External* you got at the previous step from the Model Serving configuration\n",
19+
"\n",
20+
"The model name is already filled in."
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "0de65d02-84a6-4cff-882e-551cdd42b486",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"model_name = \"fraud\"\n",
31+
"infer_endpoint = 'change_me' # e.g. 'https://model-server.<your-namespace>.svc.cluster.local' or 'https://model-server-<your-namespace>.apps.shift.nerc.mghpcc.org'\n",
32+
"infer_url = f\"{infer_endpoint}/v2/models/{model_name}/infer\""
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"id": "d94f9ece-e9cf-44e2-a8a2-73160186aee8",
38+
"metadata": {},
39+
"source": [
40+
"## Request Function\n",
41+
"\n",
42+
"Build and submit the REST request. \n",
43+
"\n",
44+
"Note: You submit the data in the same format that you used for an ONNX inference."
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "54b9386f-683a-4880-b780-c40bec3ab9f8",
51+
"metadata": {
52+
"tags": []
53+
},
54+
"outputs": [],
55+
"source": [
56+
"import requests\n",
57+
"\n",
58+
"\n",
59+
"def rest_request(data):\n",
60+
" json_data = {\n",
61+
" \"inputs\": [\n",
62+
" {\n",
63+
" \"name\": \"dense_input\",\n",
64+
" \"shape\": [1, 5],\n",
65+
" \"datatype\": \"FP32\",\n",
66+
" \"data\": data\n",
67+
" }\n",
68+
" ]\n",
69+
" }\n",
70+
"\n",
71+
" response = requests.post(infer_url, json=json_data, verify=False)\n",
72+
" response_dict = response.json()\n",
73+
" return response_dict['outputs'][0]['data']"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "5f871f12",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"#Load the scaler\n",
84+
"import pickle\n",
85+
"with open('artifact/scaler.pkl', 'rb') as handle:\n",
86+
" scaler = pickle.load(handle)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"id": "45ad16ac-23da-48bd-9796-f8e4cacae981",
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"data = [0.3111400080477545, 1.9459399775518593, 1.0, 0.0, 0.0]\n",
97+
"prediction = rest_request(scaler.transform([data]).tolist()[0])\n",
98+
"prediction"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"id": "1d66e0f7-4d4e-4879-bdf1-36b712432fd9",
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"threshhold = 0.95\n",
109+
"\n",
110+
"if (prediction[0] > threshhold):\n",
111+
" print('fraud')\n",
112+
"else:\n",
113+
" print('not fraud')"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"id": "5f7b17c0",
119+
"metadata": {},
120+
"source": [
121+
"## Example 1: user buys a coffee\n",
122+
"\n",
123+
"In this example, the user is buying a coffee. The parameters given to the model are:\n",
124+
"* same location as the last transaction (distance=0)\n",
125+
"* same median price as the last transaction (ratio_to_median=1)\n",
126+
"* using a pin number (pin=1)\n",
127+
"* using the credit card chip (chip=1)\n",
128+
"* not an online transaction (online=0)"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": null,
134+
"id": "f0a68b67-b109-4a2f-b097-092f4a4d25ce",
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"data = [0.0, 1.0, 1.0, 1.0, 0.0]\n",
139+
"prediction = rest_request(scaler.transform([data]).tolist()[0])\n",
140+
"prediction\n",
141+
"threshhold = 0.95\n",
142+
"\n",
143+
"if (prediction[0] > threshhold):\n",
144+
" print('The model predicts that this is fraud')\n",
145+
"else:\n",
146+
" print('The model predicts that this is not fraud')"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"id": "db10b280",
152+
"metadata": {},
153+
"source": [
154+
"## Example 2: fraudulent transaction\n",
155+
"\n",
156+
"In this example, someone stole the user's credit card and is buying something online. The parameters given to the model are:\n",
157+
"* very far away from the last transaction (distance=100)\n",
158+
"* median price similar to the last transaction (ratio_to_median=1.2)\n",
159+
"* not using a pin number (pin=0)\n",
160+
"* not using the credit card chip (chip=0)\n",
161+
"* is an online transaction (online=1)"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"id": "219b8927",
168+
"metadata": {
169+
"tags": []
170+
},
171+
"outputs": [],
172+
"source": [
173+
"data = [100, 1.2, 0.0, 0.0, 1.0]\n",
174+
"prediction = rest_request(scaler.transform([data]).tolist()[0])\n",
175+
"prediction\n",
176+
"threshhold = 0.95\n",
177+
"\n",
178+
"if (prediction[0] > threshhold):\n",
179+
" print('The model predicts that this is fraud')\n",
180+
"else:\n",
181+
" print('The model predicts that this is not fraud')"
182+
]
183+
}
184+
],
185+
"metadata": {
186+
"kernelspec": {
187+
"display_name": "Python 3.9",
188+
"language": "python",
189+
"name": "python3"
190+
},
191+
"language_info": {
192+
"codemirror_mode": {
193+
"name": "ipython",
194+
"version": 3
195+
},
196+
"file_extension": ".py",
197+
"mimetype": "text/x-python",
198+
"name": "python",
199+
"nbconvert_exporter": "python",
200+
"pygments_lexer": "ipython3",
201+
"version": "3.9.18"
202+
}
203+
},
204+
"nbformat": 4,
205+
"nbformat_minor": 5
206+
}

0 commit comments

Comments
 (0)