Skip to content

Instantly share code, notes, and snippets.

@bretmcg
Last active February 3, 2020 19:54
Show Gist options
  • Save bretmcg/474a454ecb29290cbfc28c57052d3949 to your computer and use it in GitHub Desktop.
Save bretmcg/474a454ecb29290cbfc28c57052d3949 to your computer and use it in GitHub Desktop.
Call Cloud ML Engine from Google Cloud Functions
// Copyright 2018, Google, Inc.
// 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.
'use strict'
//
// How to do serverless machine learning prediction by calling
// Google Cloud Machine Learning Engine prediction.
//
// Designed to call the model built in this guide:
// https://cloud.google.com/ml-engine/docs/getting-started-training-prediction
//
// Usage: gcloud beta functions deploy testPrediction --trigger-http
// Call:
// curl -H "Content-Type: application/json" -d '{ "data": {"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-States"}}' http://{CLOUD_FUNCTION_URL}
const google = require('googleapis');
const GoogleAuth = require('google-auth-library');
const authFactory = new GoogleAuth();
// Set to appropriate values, if necessary.
const modelName = `projects/${process.env.GCLOUD_PROJECT}/models/census`;
//const testData = {"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-States"};
// testPrediction HTTP function
exports.testPrediction = function(req, res) {
if(req.method === 'POST') {
// Assume the body contains a JSON object:
// {
// data: {...}
// }
let data = req.body.data;
cmlePredict(data, (err, result) => {
if (err) {
console.error(new Error(err));
return res.status(500).send(err)
}
console.log('Prediction: ', result);
res.status(200).send(result);
});
} else {
// Only accept POST.
res.sendStatus(400);
}
}
function cmlePredict(data, callback) {
authFactory.getApplicationDefault(function (err, authClient, projectId) {
if (err) {
throw err;
}
// http://google.github.io/google-api-nodejs-client/21.2.0/ml.html
var ml = google.ml({
version: 'v1'
});
const params = {
auth: authClient,
name: modelName,
resource: {
instances: [
data
]
}
};
console.log(params.resource);
ml.projects.predict(params, callback);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment