-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prediction API sample. #52
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2016, 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'; | ||
|
||
// [START predict] | ||
var google = require('googleapis'); | ||
var hostedmodels = google.prediction('v1.6').hostedmodels; | ||
|
||
function auth (callback) { | ||
google.auth.getApplicationDefault(function(err, authClient) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
// The createScopedRequired method returns true when running on GAE or a | ||
// local developer machine. In that case, the desired scopes must be passed | ||
// in manually. When the code is running in GCE or a Managed VM, the scopes | ||
// are pulled from the GCE metadata server. | ||
// See https://cloud.google.com/compute/docs/authentication for more | ||
// information. | ||
if (authClient.createScopedRequired && authClient.createScopedRequired()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This really shouldn't be required, if it is, we should open a bug on the API client. Python's does this automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ¯_(ツ)_/¯ |
||
// Scopes can be specified either as an array or as a single, | ||
// space-delimited string. | ||
authClient = authClient.createScoped([ | ||
'https://www.googleapis.com/auth/prediction' | ||
]); | ||
} | ||
callback(null, authClient); | ||
}); | ||
} | ||
|
||
function predict(callback) { | ||
auth(function(err, authClient) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
// Predict the sentiment for the word "hello". | ||
hostedmodels.predict({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you separate the auth code away from the api client call? |
||
auth: authClient, | ||
// Project id used for this sample | ||
project: '414649711441', | ||
hostedModelName: 'sample.sentiment', | ||
resource: { | ||
input: { | ||
// Predict sentiment of the word "hello" | ||
csvInstance: ['hello'] | ||
} | ||
} | ||
}, callback); | ||
}); | ||
} | ||
// [END predict] | ||
|
||
exports.predict = predict; | ||
|
||
if (module === require.main) { | ||
predict(function (err, result) { | ||
console.log(err, result); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "nodejs-docs-samples-prediction", | ||
"description": "Node.js samples for Google Cloud Prediction API.", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache Version 2.0", | ||
"scripts": { | ||
"hostedmodels": "node hostedmodels.js" | ||
}, | ||
"dependencies": { | ||
"googleapis": "^2.1.7" | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. engines field for required node version? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2016, 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'; | ||
|
||
var assert = require('assert'); | ||
var hostedmodels = require('../../prediction/hostedmodels'); | ||
|
||
var EXPECTED_RESULT = { | ||
kind: 'prediction#output', | ||
id: 'sample.sentiment', | ||
selfLink: 'https://www.googleapis.com/prediction/v1.6/projects/414649711441' + | ||
'/hostedmodels/sample.sentiment/predict', | ||
outputLabel: 'positive', | ||
outputMulti: [ | ||
{ label: 'positive', score: '0.784671' }, | ||
{ label: 'negative', score: '0.186649' }, | ||
{ label: 'neutral', score: '0.028680' } | ||
] | ||
}; | ||
|
||
describe('prediction/hostedmodels', function () { | ||
it('should predict', function (done) { | ||
hostedmodels.predict(function (err, result) { | ||
if (err) { | ||
return done(err); | ||
} | ||
assert.deepEqual(result, EXPECTED_RESULT); | ||
done(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does 'running on GAE' apply for node? Or does this mean Managed VMs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it means Managed VMs, the comment came from the googleapis README