-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from GoogleCloudPlatform/prediction
Add Prediction API sample.
- Loading branch information
Showing
4 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) { | ||
// 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({ | ||
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |