Skip to content

Commit 945f503

Browse files
committed
Add Prediction API sample.
1 parent dade986 commit 945f503

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
"coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls",
2727
"deps_datastore": "cd datastore && npm i && cd ../..",
2828
"deps_storage": "cd storage && npm i && cd ../..",
29+
"deps_prediction": "cd prediction && npm i && cd ../..",
2930
"deps_express": "cd appengine/express && npm i && cd ../..",
3031
"deps_sendgrid": "cd appengine/sendgrid && npm i && cd ../.. && cd computeengine/sendgrid && npm i && cd ../..",
3132
"deps_memcached": "cd appengine/express-memcached-session && npm i && cd ../..",
3233
"pretest_geddy": "cd appengine/geddy && npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;",
33-
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
34+
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_prediction && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
3435
"test": "npm run jshint && npm run cover"
3536
},
3637
"devDependencies": {

prediction/hostedmodels.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START predict]
17+
var google = require('googleapis');
18+
var hostedmodels = google.prediction('v1.6').hostedmodels;
19+
20+
function predict(callback) {
21+
22+
google.auth.getApplicationDefault(function(err, authClient) {
23+
if (err) {
24+
return callback(err);
25+
}
26+
// The createScopedRequired method returns true when running on GAE or a
27+
// local developer machine. In that case, the desired scopes must be passed
28+
// in manually. When the code is running in GCE or a Managed VM, the scopes
29+
// are pulled from the GCE metadata server.
30+
// See https://cloud.google.com/compute/docs/authentication for more
31+
// information.
32+
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
33+
// Scopes can be specified either as an array or as a single,
34+
// space-delimited string.
35+
authClient = authClient.createScoped([
36+
'https://www.googleapis.com/auth/prediction'
37+
]);
38+
}
39+
// Predict the sentiment for the word "hello".
40+
hostedmodels.predict({
41+
auth: authClient,
42+
project: '414649711441',
43+
hostedModelName: 'sample.sentiment',
44+
resource: {
45+
input: {
46+
// Predict sentiment of the word "hello"
47+
csvInstance: ['hello']
48+
}
49+
}
50+
}, callback);
51+
});
52+
}
53+
// [END predict]
54+
55+
exports.predict = predict;
56+
57+
if (module === require.main) {
58+
predict(function (err, result) {
59+
console.log(err, result);
60+
});
61+
}

prediction/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "nodejs-docs-samples-prediction",
3+
"description": "Node.js samples for Google Cloud Prediction API.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"scripts": {
8+
"tasks": "node hostedmodels.js"
9+
},
10+
"dependencies": {
11+
"googleapis": "^2.1.7"
12+
}
13+
}

test/prediction/hostedmodels.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var assert = require('assert');
17+
var hostedmodels = require('../../prediction/hostedmodels');
18+
19+
var EXPECTED_RESULT = {
20+
kind: 'prediction#output',
21+
id: 'sample.sentiment',
22+
selfLink: 'https://www.googleapis.com/prediction/v1.6/projects/414649711441' +
23+
'/hostedmodels/sample.sentiment/predict',
24+
outputLabel: 'positive',
25+
outputMulti: [
26+
{ label: 'positive', score: '0.784671' },
27+
{ label: 'negative', score: '0.186649' },
28+
{ label: 'neutral', score: '0.028680' }
29+
]
30+
};
31+
32+
describe('prediction/hostedmodels', function () {
33+
it('should predict', function (done) {
34+
hostedmodels.predict(function (err, result) {
35+
if (err) {
36+
return done(err);
37+
}
38+
assert.deepEqual(result, EXPECTED_RESULT);
39+
done();
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)