Skip to content

Instantly share code, notes, and snippets.

@jasonpolites
Created January 5, 2018 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonpolites/364e6da6f581cf5871d23d8c04dec04f to your computer and use it in GitHub Desktop.
Save jasonpolites/364e6da6f581cf5871d23d8c04dec04f to your computer and use it in GitHub Desktop.
MODEL_NAME=census
MODEL_VERSION=v1
// Copyright 2017, 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'
const google = require('googleapis');
const ml = google.ml({
version: 'v1'
});
// Load environment variables from `.env` file and/or OS.
require('dotenv').config();
const config = {
modelName: process.env.MODEL_NAME,
modelVersion: process.env.MODEL_VERSION
}
exports.getPrediction2 = (req, res) => {
if(req.method === 'POST') {
// Assume the body contains a JSON object:
// example: {"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"}
let data = req.body;
cmlePredict(data, (err, result) => {
if(err) {
// Something went wrong
console.error(err);
res.sendStatus(500);
return;
}
// Do something with the result
res.send(result);
});
} else {
// Only accept POST
res.sendStatus(400);
}
};
function cmlePredict(data, callback) {
// Use the default application credential to authenticate against the ML Engine API
google.auth.getApplicationDefault(function (err, authClient, projectId) {
if (err) {
// Failed to get default credentials (this shouldn't happen)
console.error(err);
callback(err);
return;
}
// http://google.github.io/google-api-nodejs-client/21.2.0/ml.html
const params = {
auth: authClient,
name: `projects/${projectId}/models/${config.modelName}/versions/${config.modelVersion}`,
resource: {
instances: [
data
]
}
};
// Retrieve a prediction from the given model with the given data array
ml.projects.predict(params, callback);
});
}
{
"name": "ml-engine-sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^4.0.0",
"google-auth-library": "^0.12.0",
"googleapis": "^23.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment