|
| 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 | +} |
0 commit comments