From 691cab1b630ffd8800ab89c40560f1465cf3626e Mon Sep 17 00:00:00 2001 From: Robbie Haertel Date: Fri, 21 Jul 2017 00:20:55 -0700 Subject: [PATCH 1/4] Adding example for CloudML online prediction. --- ml_engine/online_prediction/README.md | 1 + ml_engine/online_prediction/predict.js | 60 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 ml_engine/online_prediction/README.md create mode 100644 ml_engine/online_prediction/predict.js diff --git a/ml_engine/online_prediction/README.md b/ml_engine/online_prediction/README.md new file mode 100644 index 0000000000..07c160f231 --- /dev/null +++ b/ml_engine/online_prediction/README.md @@ -0,0 +1 @@ +https://cloud.google.com/ml-engine/docs/concepts/prediction-overview diff --git a/ml_engine/online_prediction/predict.js b/ml_engine/online_prediction/predict.js new file mode 100644 index 0000000000..ffe2a7a015 --- /dev/null +++ b/ml_engine/online_prediction/predict.js @@ -0,0 +1,60 @@ +var google = require('googleapis'); + +var ml = google.ml('v1'); + +function auth(callback) { + google.auth.getApplicationDefault(function(err, authClient) { + if (err) { + return callback(err); + } + + if (authClient.createScopedRequired && authClient.createScopedRequired()) { + authClient = authClient.createScoped([ + 'https://www.googleapis.com/auth/cloud-platform' + ]); + } + callback(null, authClient); + }); +} + +var instance = { + age: 25, + workclass: " Private", + education: " 11th", + education_num: 7, + marital_status: " Never - married", + occupation: " Machine - op - inspct", + relationship: " Own - child", + race: " Black", + gender: " Male", + capital_gain: 0, + capital_loss: 0, + hours_per_week: 40, + native_country: " United - Stats" +} + +auth(function(err, authClient) { + if (err) { + console.error(err); + } else { + var ml = google.ml({ + version: 'v1', + auth: authClient + }); + + // Predict + ml.projects.predict({ + name: 'projects/op-beta-walkthrough/models/census', + resource: { + instances: [instance] + } + }, function(err, result) { + if (err) { + return callback(err); + } + + console.log(JSON.stringify(result)); + }); + } +}); + From 1cef0e27bf449d58e242d8a7adefe27250603a18 Mon Sep 17 00:00:00 2001 From: Robbie Haertel Date: Fri, 21 Jul 2017 00:20:55 -0700 Subject: [PATCH 2/4] Adding example for CloudML online prediction. --- ml_engine/online_prediction/README.md | 1 + ml_engine/online_prediction/predict.js | 60 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 ml_engine/online_prediction/README.md create mode 100644 ml_engine/online_prediction/predict.js diff --git a/ml_engine/online_prediction/README.md b/ml_engine/online_prediction/README.md new file mode 100644 index 0000000000..07c160f231 --- /dev/null +++ b/ml_engine/online_prediction/README.md @@ -0,0 +1 @@ +https://cloud.google.com/ml-engine/docs/concepts/prediction-overview diff --git a/ml_engine/online_prediction/predict.js b/ml_engine/online_prediction/predict.js new file mode 100644 index 0000000000..ffe2a7a015 --- /dev/null +++ b/ml_engine/online_prediction/predict.js @@ -0,0 +1,60 @@ +var google = require('googleapis'); + +var ml = google.ml('v1'); + +function auth(callback) { + google.auth.getApplicationDefault(function(err, authClient) { + if (err) { + return callback(err); + } + + if (authClient.createScopedRequired && authClient.createScopedRequired()) { + authClient = authClient.createScoped([ + 'https://www.googleapis.com/auth/cloud-platform' + ]); + } + callback(null, authClient); + }); +} + +var instance = { + age: 25, + workclass: " Private", + education: " 11th", + education_num: 7, + marital_status: " Never - married", + occupation: " Machine - op - inspct", + relationship: " Own - child", + race: " Black", + gender: " Male", + capital_gain: 0, + capital_loss: 0, + hours_per_week: 40, + native_country: " United - Stats" +} + +auth(function(err, authClient) { + if (err) { + console.error(err); + } else { + var ml = google.ml({ + version: 'v1', + auth: authClient + }); + + // Predict + ml.projects.predict({ + name: 'projects/op-beta-walkthrough/models/census', + resource: { + instances: [instance] + } + }, function(err, result) { + if (err) { + return callback(err); + } + + console.log(JSON.stringify(result)); + }); + } +}); + From 808207c907b08bb3d3b3ef568b34a7b5286fd78e Mon Sep 17 00:00:00 2001 From: Robbie Haertel Date: Fri, 21 Jul 2017 00:39:15 -0700 Subject: [PATCH 3/4] Adding Copyright to online prediction example --- ml_engine/online_prediction/predict.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ml_engine/online_prediction/predict.js b/ml_engine/online_prediction/predict.js index ffe2a7a015..143c169926 100644 --- a/ml_engine/online_prediction/predict.js +++ b/ml_engine/online_prediction/predict.js @@ -1,3 +1,17 @@ +# Copyright 2017 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + var google = require('googleapis'); var ml = google.ml('v1'); From d04c937ec8350dba366a5194e1a7497efad3f730 Mon Sep 17 00:00:00 2001 From: Robbie Haertel Date: Fri, 21 Jul 2017 00:39:15 -0700 Subject: [PATCH 4/4] Adding Copyright to online prediction example --- ml_engine/online_prediction/predict.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ml_engine/online_prediction/predict.js b/ml_engine/online_prediction/predict.js index ffe2a7a015..143c169926 100644 --- a/ml_engine/online_prediction/predict.js +++ b/ml_engine/online_prediction/predict.js @@ -1,3 +1,17 @@ +# Copyright 2017 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + var google = require('googleapis'); var ml = google.ml('v1');