Google Apps Script

Prediction Service

The Prediction service allows you to use the Google Prediction API in Apps Script. This API allows users to train prediction models and execute queries against them.

Reference

For detailed information on this service, see the reference documentation for the Prediction API. Like all advanced services in Apps Script, the Prediction service uses the same objects, methods, and parameters as the public API.

Sample code

The sample code below uses version 1.6 of the API.

Open code in new window

Query a hosted model

This sample queries an example hosted model to analyze the sentiment of a text string.

function queryHostedModel() {
  // Replace this value with the project number listed in the Google
  // APIs Console project.
  var projectNumber = 'XXXXXXXX';
  var hostedModelName = 'sample.sentiment';

  // Query the hosted model with a positive statement.
  var predictionString = 'Want to go to the park this weekend?';
  var prediction = Prediction.Hostedmodels.predict(projectNumber,
      hostedModelName,
      {
        input: {
          csvInstance: [predictionString]
        }
      });
  // Logs Sentiment: positive.
  Logger.log('Sentiment: ' + prediction.outputLabel);

  // Now query the hosted model with a negative statement.
  predictionString = 'You are not very nice!';
  prediction = Prediction.Hostedmodels.predict(projectNumber, hostedModelName,
      {
        input: {
          csvInstance: [predictionString]
        }
      });
  // Logs Sentiment: negative.
  Logger.log('Sentiment: ' + prediction.outputLabel);
}

Create and query a trained model

This sample implements the functionality of the Hello Prediction tutorial in Apps Script. It creates a new model and trains it with data from a comma-separated values (CSV) file uploaded to Google Cloud Storage in order to identify the language of a string of text.

Before executing these functions, please set up your project to use Google Cloud Storage by following the Prerequisites and Upload Training Data sections of the Hello Prediction tutorial. Note that you do not need to create a new API console project because Apps Script automatically creates a project for you. To navigate to this project, open the script editor, then select the menu item Resources > Advanged Google services. Next, click the Google Developers Console link.

Create a new trained model

This code creates a new model and trains it using a CSV file uploaded to Google Cloud Storage.

function createNewModel() {
  // Replace this value with the project number listed in the Google
  // APIs Console project.
  var projectNumber = 'XXXXXXXX';
  var id = 'mylanguageidmodel';
  var storageDataLocation = 'languageidsample/language_id.txt';

  // Returns immediately. Training happens asynchronously.
  var result = Prediction.Trainedmodels.insert(projectNumber,
      {
        id: id,
        storageDataLocation: storageDataLocation
      });
  Logger.log(result);
}

Query training status

Because models are trained asynchronously, this code queries the status to find out when training is complete.

function queryTrainingStatus() {
  // Replace this value with the project number listed in the Google
  // APIs Console project.
  var projectNumber = 'XXXXXXXX';
  var id = 'mylanguageidmodel';

  var result = Prediction.Trainedmodels.get(projectNumber, id);
  Logger.log(result.trainingStatus);
}

Query trained model

Once training is complete, this code queries the model to determine the language of the query string.

function queryTrainedModel() {
  // Replace this value with the project number listed in the Google
  // APIs Console project.
  var projectNumber = 'XXXXXXXX';
  var id = 'mylanguageidmodel';
  var query = 'Este es un mensaje de prueba de ejemplo';

  var prediction = Prediction.Trainedmodels.predict(projectNumber, id,
      {
        input:
          {
            csvInstance: [query]
          }
      });
  // Logs Language: Spanish.
  Logger.log('Language: ' + prediction.outputLabel);
}

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.