-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict chance graph.json
More file actions
238 lines (238 loc) · 12.5 KB
/
Copy pathpredict chance graph.json
File metadata and controls
238 lines (238 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{
"properties": {},
"icon": "",
"description": "Titanic_DI - predict_chance",
"processes": {
"python3operator2": {
"component": "com.sap.system.python3Operator",
"metadata": {
"label": "Python3 Operator",
"x": 237,
"y": 207,
"height": 80,
"width": 120,
"extensible": true,
"config": {
"script": "def process_data(data):\n import csv\n # api.logger.info(data)\n reader = csv.reader(data.split('\\n'), delimiter=',')\n train = []\n categories = []\n rows = list(reader)\n for row in rows[1:-1]: # skip header\n data = row[:8]\n category = row[8]\n\n train.append(data)\n categories.append(category)\n # api.logger.info(category)\n\n # api.logger.info('last reader line: ' + str(rows[len(rows) - 1]))\n return train, categories\n\ndef train_mlp(train_data):\n import pickle\n from sklearn.preprocessing import StandardScaler\n from sklearn.neural_network import MLPClassifier\n \n X_train, y_train = process_data(train_data)\n # X_test, _ = process_data(test_data)) \n scaler = StandardScaler()\n # Fit only to the training data\n scaler.fit(X_train)\n # Now apply the transformations to the data:\n X_train = scaler.transform(X_train)\n # X_test = scaler.transform(X_test)\n \n multi_layer_perceptron = MLPClassifier(hidden_layer_sizes=(30,20,10))\n multi_layer_perceptron.fit(X_train,y_train)\n predictions = multi_layer_perceptron.predict(X_train)\n from sklearn.metrics import classification_report,confusion_matrix\n # print(confusion_matrix(y_train,predictions))\n report = classification_report(y_train,predictions, output_dict=True)\n \n serialized_model = {\n 'model': multi_layer_perceptron, \n 'scaler': scaler\n }\n return pickle.dumps(serialized_model), report['weighted avg']['f1-score'], report['accuracy']\n\ndef on_input(input):\n api.logger.info(input)\n modelblob, f1_score, accuracy = train_mlp(input)\n # to send metrics to the Submit Metrics operator, create a Python dictionary of key-value pairs\n metrics_dict = {\n \"f1-score\": str(f1_score),\n \"accuracy\": str(accuracy)\n }\n # send the metrics to the output port - Submit Metrics operator will use this to persist the metrics \n # api.send(\"metrics\", api.Message(metrics_dict))\n\n # create & send the model blob to the output port - Artifact Producer operator will use this to persist the model and create an artifact ID\n api.send(\"modelblob\", modelblob)\n \napi.set_port_callback(\"input\", on_input)\n"
},
"additionalinports": [
{
"name": "input",
"type": "string"
}
],
"additionaloutports": [
{
"name": "modelblob",
"type": "blob"
}
]
}
},
"python3operator1": {
"component": "com.sap.system.python3Operator",
"metadata": {
"label": "Python36 - Inference",
"x": 402.99999809265137,
"y": 99.99999952316284,
"height": 80,
"width": 120,
"extensible": true,
"config": {
"metadata": {},
"script": "import json\r\nimport numpy as np\r\nimport pickle\r\n\r\n# Global vars to keep track of model status\r\nserlzd_model = None\r\nmodel_ready = False\r\n\r\n# Validate input data is JSON\r\ndef is_json(data):\r\n try:\r\n json_object = json.loads(data)\r\n except ValueError as e:\r\n return False\r\n return True\r\n\r\n# When Model Blob reaches the input port\r\ndef on_model(model_blob):\r\n global serlzd_model\r\n global model_ready\r\n\r\n serlzd_model = model_blob\r\n model_ready = True\r\n api.logger.info(\"Model Received & Ready\")\r\n \r\n# Client POST request received\r\ndef on_input(msg):\r\n error_message = \"\"\r\n success = False\r\n try:\r\n api.logger.info(\"POST request received from Client - checking if model is ready\")\r\n if model_ready:\r\n api.logger.info(\"Model Ready\")\r\n api.logger.info(\"Received data from client - validating json input\")\r\n \r\n user_data = msg.body.decode('utf-8')\r\n # Received message from client, verify json data is valid\r\n if is_json(user_data):\r\n api.logger.info(\"Received valid json data from client - ready to use\")\r\n \r\n feed = json.loads(user_data)\r\n input_data = np.array(feed['data'])\r\n api.logger.info(str(input_data))\r\n \r\n model_dict = pickle.loads(serlzd_model)\r\n mlp = model_dict['model']\r\n scaler = model_dict['scaler']\r\n input_data = scaler.transform(input_data)\r\n \r\n # check path\r\n attr = msg.attributes\r\n op_id = attr['openapi.operation_id']\r\n api.logger.info('operation_id: ' + op_id )\r\n if 'predict_classes' in op_id:\r\n prediction = mlp.predict(input_data).tolist()\r\n else:\r\n prediction = mlp.predict_proba(input_data).tolist()\r\n api.logger.info(str(prediction))\r\n\r\n success = True\r\n else:\r\n api.logger.info(\"Invalid JSON received from client - cannot apply model.\")\r\n error_message = \"Invalid JSON provided in request: \" + user_data\r\n success = False\r\n else:\r\n api.logger.info(\"Model has not yet reached the input port - try again.\")\r\n error_message = \"Model has not yet reached the input port - try again.\"\r\n success = False\r\n except Exception as e:\r\n api.logger.error(e)\r\n error_message = \"An error occurred: \" + str(e)\r\n \r\n if success:\r\n # apply carried out successfully, send a response to the user\r\n msg.body = json.dumps({'Results': prediction})\r\n else:\r\n msg.body = json.dumps({'Error': error_message})\r\n \r\n new_attributes = {'message.request.id': msg.attributes['message.request.id']}\r\n msg.attributes = new_attributes\r\n api.send('output', msg)\r\n \r\napi.set_port_callback(\"modelblob\", on_model)\r\napi.set_port_callback(\"input\", on_input)\r\n"
},
"additionalinports": [
{
"name": "input",
"type": "message"
},
{
"name": "modelblob",
"type": "blob"
}
],
"additionaloutports": [
{
"name": "output",
"type": "message"
}
]
}
},
"openapiservlow11": {
"component": "com.sap.openapi.server",
"metadata": {
"label": "OpenAPI Servlow",
"x": 17,
"y": 25.99999976158142,
"height": 80,
"width": 120,
"config": {
"basePath": "${deployment}",
"timeout": 300000,
"websocket": true,
"swaggerSpec": "{\n \"schemes\":[\n \"http\",\n \"https\"\n ],\n \"swagger\":\"2.0\",\n \"info\":{\n \"description\":\"This is an example of using the OpenAPI Servlow to carry out inference with an existing model.\",\n \"title\":\"OpenAPI demo\",\n \"termsOfService\":\"http://www.sap.com/vora/terms/\",\n \"contact\":{\n\n },\n \"license\":{\n \"name\":\"Apache 2.0\",\n \"url\":\"http://www.apache.org/licenses/LICENSE-2.0.html\"\n },\n \"version\":\"1.0.0\"\n },\n \"basePath\":\"/$deployment\",\n \"paths\":{\n \"/v1/predict_proba\":{\n \"post\":{\n \"description\":\"Upload data in json format\",\n \"consumes\":[\n \"application/json\"\n ],\n \"produces\":[\n \"application/json\"\n ],\n \"summary\":\"probabilities per category\",\n \"operationId\":\"predict_proba\",\n \"parameters\":[\n {\n \"type\":\"object\",\n \"description\":\"json data\",\n \"name\":\"body\",\n \"in\":\"body\",\n \"required\":true\n }\n ],\n \"responses\":{\n \"200\":{\n \"description\":\"Data received\"\n },\n \"500\":{\n \"description\":\"Error during upload of json\"\n }\n }\n }\n },\n \"/v1/predict_classes\":{\n \"post\":{\n \"description\":\"Upload data in json format\",\n \"consumes\":[\n \"application/json\"\n ],\n \"produces\":[\n \"application/json\"\n ],\n \"summary\":\"predicted category\",\n \"operationId\":\"predict_classes\",\n \"parameters\":[\n {\n \"type\":\"object\",\n \"description\":\"json data\",\n \"name\":\"body\",\n \"in\":\"body\",\n \"required\":true\n }\n ],\n \"responses\":{\n \"200\":{\n \"description\":\"Data received\"\n },\n \"500\":{\n \"description\":\"Error during upload of json\"\n }\n }\n }\n }\n },\n \"definitions\":{\n },\n \"securityDefinitions\":{\n \"UserSecurity\":{\n \"type\":\"basic\"\n }\n }\n}",
"oneway": false
}
},
"name": "openapiservlow1"
},
"responseinterceptor11": {
"component": "com.sap.util.responseinterceptor",
"metadata": {
"label": "Response Interceptor",
"x": 217.99999904632568,
"y": 39.99999952316284,
"height": 80,
"width": 120,
"config": {
"maxWait": 300000
}
},
"name": "responseinterceptor1"
},
"readfile1": {
"component": "com.sap.storage.read",
"metadata": {
"label": "Read File",
"x": -48,
"y": 212,
"height": 80,
"width": 120,
"config": {
"service": "SDL",
"path": "/shared/Titanic/titanic_train.csv",
"onlyReadOnChange": true
}
}
},
"tostringconverter1": {
"component": "com.sap.util.toStringConverter",
"metadata": {
"label": "ToString Converter",
"x": 122,
"y": 238,
"height": 50,
"width": 50,
"config": {}
}
}
},
"groups": [
{
"name": "group1",
"nodes": [
"python3operator2"
],
"metadata": {
"description": "sklearnpod"
},
"tags": {
"pandas": "",
"opensuse": "",
"python36": "",
"sklearn": "",
"tornado": "5.0.2"
}
},
{
"name": "group2",
"nodes": [
"python3operator1"
],
"metadata": {
"description": "predictpod"
},
"tags": {
"opensuse": "",
"pandas": "",
"tornado": "5.0.2",
"python36": "",
"sklearn": ""
}
}
],
"connections": [
{
"metadata": {
"points": "141,65.99999976158142 184.99999952316284,65.99999976158142 184.99999952316284,70.99999952316284 212.99999904632568,70.99999952316284"
},
"src": {
"port": "out",
"process": "openapiservlow11"
},
"tgt": {
"port": "in",
"process": "responseinterceptor11"
}
},
{
"metadata": {
"points": "341.9999990463257,79.99999952316284 370,80 370,131 397.99999809265137,130.99999952316284"
},
"src": {
"port": "out",
"process": "responseinterceptor11"
},
"tgt": {
"port": "input",
"process": "python3operator1"
}
},
{
"metadata": {
"points": "526.9999980926514,139.99999952316284 554.9999976158142,139.99999952316284 554.9999976158142,72 369.9999985694885,72 369.9999985694885,12 168.99999952316284,12 168.99999952316284,88.99999952316284 212.99999904632568,88.99999952316284"
},
"src": {
"port": "output",
"process": "python3operator1"
},
"tgt": {
"port": "resp",
"process": "responseinterceptor11"
}
},
{
"metadata": {
"points": "176,263 204,263 204,247 232,247"
},
"src": {
"port": "outstring",
"process": "tostringconverter1"
},
"tgt": {
"port": "input",
"process": "python3operator2"
}
},
{
"metadata": {
"points": "76,261 96.5,261 96.5,272 117,272"
},
"src": {
"port": "outFile",
"process": "readfile1"
},
"tgt": {
"port": "inmessage",
"process": "tostringconverter1"
}
},
{
"metadata": {
"points": "361,247 379.5,247 379.5,149 397.99999809265137,148.99999952316284"
},
"src": {
"port": "modelblob",
"process": "python3operator2"
},
"tgt": {
"port": "modelblob",
"process": "python3operator1"
}
}
],
"inports": {},
"outports": {}
}