/nodejs-prediction

Node.js client for Google Cloud Prediction: Analyze your data to add various features to your applications, such as customer sentiment analysis, spam detection, recommendation systems, and more.

Primary LanguageJavaScriptApache License 2.0Apache-2.0

Google Cloud Platform logo

Google Prediction API: Node.js Client

release level CircleCI AppVeyor codecov

Node.js idiomatic client for Prediction API.

The Cloud Prediction API provides a RESTful API to build Machine Learning models.

⚠️ Deprecated Module
This library is deprecated. The API will be shut down on April 30, 2018. See the Prediction API End of Life FAQ for more information.

Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.

Table of contents:

Quickstart

Before you begin

  1. Select or create a Cloud Platform project.

    Go to the projects page

  2. Enable billing for your project.

    Enable billing

  3. Enable the Google Prediction API API.

    Enable the API

  4. Set up authentication with a service account so you can access the API from your local workstation.

Installing the client library

npm install --save @google-cloud/prediction

Using the client library

var google = require('googleapis');

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 GAE Flexible, 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);
  });
}

/**
 * @param {string} phrase The phrase for which to predict sentiment,
 * e.g. "good morning".
 * @param {Function} callback Callback function.
 */
function predict(phrase, callback) {
  auth(function(err, authClient) {
    if (err) {
      return callback(err);
    }
    var hostedmodels = google.prediction({
      version: 'v1.6',
      auth: authClient,
    }).hostedmodels;
    // Predict the sentiment for the provided phrase
    hostedmodels.predict(
      {
        // Project id used for this sample
        project: '414649711441',
        hostedModelName: 'sample.sentiment',
        resource: {
          input: {
            // Predict sentiment of the provided phrase
            csvInstance: phrase.split(/\s/gi),
          },
        },
      },
      function(err, prediction) {
        if (err) {
          return callback(err);
        }

        // Received prediction result
        console.log(`Sentiment for "${phrase}": ${prediction.outputLabel}`);
        callback(null, prediction);
      }
    );
  });
}

Samples

Samples are in the samples/ directory. The samples' README.md has instructions for running the samples.

Sample Source Code
Hosted Models source code

The Prediction API Node.js Client API Reference documentation also contains samples.

Versioning

This library follows Semantic Versioning.

This library is deprecated. This means that it is no longer being actively maintained and the only updates the library will receive will be for critical security issues.

More Information: Google Cloud Platform Launch Stages

Contributing

Contributions welcome! See the Contributing Guide.

License

Apache Version 2.0

See LICENSE