Skip to content

Commit f606042

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

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

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+
// Fetch the list of GCE zones within a project.
40+
// NOTE: You must fill in your valid project ID before running this sample!
41+
hostedmodels.predict({
42+
auth: authClient,
43+
project: '414649711441',
44+
hostedModelName: 'sample.sentiment',
45+
resource: {
46+
input: {
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)