Analyzing documents

After you have created (trained) a model, you can request predictions from the model. A prediction occurs when you submit a document to the model and ask it to analyze the document according to the objective for that model (classification, entity extraction, or sentiment analysis).

AutoML Natural Language supports both online prediction, where you submit a single document and the model returns the analysis synchronously, and batch prediction, where you submit a collection of documents that the model analyzes asynchronously.

Online prediction

To make a prediction using the AutoML Natural Language UI:

  1. Click the lightbulb icon in the left navigation bar to display the available models.

    To view the models for a different project, select the project from the drop-down list in the upper right of the title bar.

  2. Click the row for the model you want to use to analyze the document.

  3. Click the Test & Use tab just below the title bar.

  4. Enter the text you want to analyze into the text box, or click the Select a PDF file option and enter the Google Cloud Storage path for a PDF file.

  5. Click Predict.

Code samples

Classification

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your project ID
  • location-id: the location for the resource, us-central1 for the Global location or eu for the European Union
  • model-id: your model ID

HTTP method and URL:

POST https://automl.googleapis.com/v1/projects/project-id/locations/location-id/models/model-id:predict

Request JSON body:

{
  "payload" : {
    "textSnippet": {
      "content": "Google, headquartered in Mountain View, unveiled the new Android phone at the Consumer Electronic Show.  Sundar Pichai said in his keynote that users love their new Android phones.",
        "mime_type": "text/plain"
      },
  }
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
  "payload": [
    {
      "displayName": "Technology",
      "classification": {
        "score": 0.8989502
      }
    },
    {
      "displayName": "Automobiles",
      "classification": {
        "score": 0.10098731
      }
    }
  ]
}

Python

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = 'YOUR_PROJECT_ID'
# model_id = 'YOUR_MODEL_ID'
# content = 'text to predict'

prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = prediction_client.model_path(
    project_id, 'us-central1', model_id
)

text_snippet = automl.types.TextSnippet(
    content=content,
    mime_type='text/plain')  # Types: 'text/plain', 'text/html'
payload = automl.types.ExamplePayload(text_snippet=text_snippet)

response = prediction_client.predict(model_full_id, payload)

for annotation_payload in response.payload:
    print(u'Predicted class name: {}'.format(
        annotation_payload.display_name))
    print(u'Predicted class score: {}'.format(
        annotation_payload.classification.score))

Java

import com.google.cloud.automl.v1.AnnotationPayload;
import com.google.cloud.automl.v1.ExamplePayload;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.PredictRequest;
import com.google.cloud.automl.v1.PredictResponse;
import com.google.cloud.automl.v1.PredictionServiceClient;
import com.google.cloud.automl.v1.TextSnippet;

import java.io.IOException;

class LanguageTextClassificationPredict {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String content = "text to predict";
    predict(projectId, modelId, content);
  }

  static void predict(String projectId, String modelId, String content) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
      // Get the full path of the model.
      ModelName name = ModelName.of(projectId, "us-central1", modelId);
      TextSnippet textSnippet =
          TextSnippet.newBuilder()
              .setContent(content)
              .setMimeType("text/plain") // Types: text/plain, text/html
              .build();
      ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
      PredictRequest predictRequest =
          PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();

      PredictResponse response = client.predict(predictRequest);

      for (AnnotationPayload annotationPayload : response.getPayloadList()) {
        System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
        System.out.format(
            "Predicted sentiment score: %.2f\n\n",
            annotationPayload.getClassification().getScore());
      }
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const content = 'text to predict'

// Imports the Google Cloud AutoML library
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new PredictionServiceClient();

async function predict() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
    payload: {
      textSnippet: {
        content: content,
        mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
      },
    },
  };

  const [response] = await client.predict(request);

  for (const annotationPayload of response.payload) {
    console.log(`Predicted class name: ${annotationPayload.displayName}`);
    console.log(
      `Predicted class score: ${annotationPayload.classification.score}`
    );
  }
}

predict();

C#

/// <summary>
/// Demonstrates using the AutoML client to predict the text content using given model.
/// </summary>
/// <param name="projectId">GCP Project ID.</param>
/// <param name="modelId">the Id of the model.</param>
/// <param name="content">Text content for prediction.</param>
public static object LanguageTextClassificationPredict(string projectId = "YOUR-PROJECT-ID",
    string modelId = "YOUR-MODEL-ID",
    string content = "YOUR TEXT TO PREDICT")
{
    // Initialize the client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    PredictionServiceClient client = PredictionServiceClient.Create();

    // Get the full path of the model.
    string modelFullId = ModelName.Format(projectId, "us-central1", modelId);

    TextSnippet textSnippet = new TextSnippet
    {
        Content = content,
        MimeType = "text/plain" // Types: text/plain, text/html
    };
    ExamplePayload payload = new ExamplePayload
    {
        TextSnippet = textSnippet
    };

    PredictRequest predictRequest = new PredictRequest
    {
        Name = modelFullId,
        Payload = payload
    };

    PredictResponse response = client.Predict(predictRequest);

    foreach (AnnotationPayload annotationPayload in response.Payload)
    {
        Console.WriteLine($"Predicted class name: {annotationPayload.DisplayName}");
        Console.WriteLine(
            $"Predicted sentiment score: " +
            $"{annotationPayload.Classification.Score.ToString("0.00")}");
    }
    return 0;
}

Go

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)

// languageTextClassificationPredict does a prediction for text classification.
func languageTextClassificationPredict(w io.Writer, projectID string, location string, modelID string, content string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "TCN123456789..."
	// content := "text to classify"

	ctx := context.Background()
	client, err := automl.NewPredictionClient(ctx)
	if err != nil {
		return fmt.Errorf("NewPredictionClient: %v", err)
	}
	defer client.Close()

	req := &automlpb.PredictRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
		Payload: &automlpb.ExamplePayload{
			Payload: &automlpb.ExamplePayload_TextSnippet{
				TextSnippet: &automlpb.TextSnippet{
					Content:  content,
					MimeType: "text/plain", // Types: "text/plain", "text/html"
				},
			},
		},
	}

	resp, err := client.Predict(ctx, req)
	if err != nil {
		return fmt.Errorf("Predict: %v", err)
	}

	for _, payload := range resp.GetPayload() {
		fmt.Fprintf(w, "Predicted class name: %v\n", payload.GetDisplayName())
		fmt.Fprintf(w, "Predicted class score: %v\n", payload.GetClassification().GetScore())
	}

	return nil
}

PHP

use Google\Cloud\AutoMl\V1\ExamplePayload;
use Google\Cloud\AutoMl\V1\PredictionServiceClient;
use Google\Cloud\AutoMl\V1\TextSnippet;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $modelId = 'my_model_id_123';
// $content = 'text to predict';
$client = new PredictionServiceClient();
try {
    // get full path of model
    $formattedName = $client->modelName(
        $projectId,
        $location,
        $modelId);

    // create payload
    $textSnippet = (new TextSnippet())
        ->setContent($content)
        ->setMimeType('text/plain'); // Types: 'text/plain', 'text/html'
    $payload = (new ExamplePayload())
        ->setTextSnippet($textSnippet);

    // predict with above model and payload
    $response = $client->predict($formattedName, $payload);
    $annotations = $response->getPayload();

    // display results
    foreach ($annotations as $annotation) {
        $classification = $annotation->getClassification();
        printf('Predicted class name: %s' . PHP_EOL, $annotation->getDisplayName());
        printf('Predicted class score: %s' . PHP_EOL, $classification->getScore());
    }
} finally {
    $client->close();
}

Ruby

require "google/cloud/automl"

project_id = "YOUR_PROJECT_ID"
model_id = "YOUR_MODEL_ID"
content = "text to predict"

prediction_client = Google::Cloud::AutoML::Prediction.new

# Get the full path of the model.
model_full_id = prediction_client.class.model_path project_id, "us-central1", model_id
payload = {
  text_snippet: {
    content:   content,
    # Types: 'text/plain', 'text/html'
    mime_type: "text/plain"
  }
}

response = prediction_client.predict model_full_id, payload

response.payload.each do |annotation_payload|
  puts "Predicted class name: #{annotation_payload.display_name}"
  puts "Predicted class score: #{annotation_payload.classification.score}"
end

Entity extraction

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your project ID
  • location-id: the location for the resource, us-central1 for the Global location or eu for the European Union
  • model-id: your model ID

HTTP method and URL:

POST https://automl.googleapis.com/v1/projects/project-id/locations/location-id/models/model-id:predict

Request JSON body:

{
  "payload" : {
    "textSnippet": {
      "content": "The Wilms tumor-suppressor gene, WT1, plays a key role in urogenital development, and WT1 dysfunction is implicated in both neoplastic and nonneoplastic (glomerulosclerosis) disease. The analysis of diseases linked specifically with WT1 mutations, such as Denys-Drash syndrome (DDS), can provide valuable insight concerning the role of WT1 in development and disease.  We report that heterozygosity for a targeted murine Wt1 allele, Wt1 (tmT396), which truncates ZF3 at codon 396, induces mesangial sclerosis characteristic of DDS in adult heterozygous and chimeric mice. Male genital defects also were evident and there was a single case of Wilms tumor in which the transcript of the nontargeted allele showed an exon 9 skipping event, implying a causal link between Wt1 dysfunction and Wilms tumorigenesis in mice. However, the mutant WT1 (tmT396) protein accounted for only 5% of WT1 in both heterozygous embryonic stem cells and the WT. This has implications regarding the mechanism by which the mutant allele exerts its effect.",
      "mime_type": "text/plain"
      },
   }
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

{
    "payload": [
        {
            "textSentiment": {
                "sentiment": 3
            }
        }
    ],
    "metadata": {
        "sentiment_score": "0.6036837"
    }
}

Python

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = 'YOUR_PROJECT_ID'
# model_id = 'YOUR_MODEL_ID'
# content = 'text to predict'

prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = prediction_client.model_path(
    project_id, 'us-central1', model_id
)

text_snippet = automl.types.TextSnippet(
    content=content,
    mime_type='text/plain')  # Types: 'text/plain', 'text/html'
payload = automl.types.ExamplePayload(text_snippet=text_snippet)

response = prediction_client.predict(model_full_id, payload)

for annotation_payload in response.payload:
    print(u'Text Extract Entity Types: {}'.format(
        annotation_payload.display_name))
    print(u'Text Score: {}'.format(
        annotation_payload.text_extraction.score))
    text_segment = annotation_payload.text_extraction.text_segment
    print(u'Text Extract Entity Content: {}'.format(text_segment.content))
    print(u'Text Start Offset: {}'.format(text_segment.start_offset))
    print(u'Text End Offset: {}'.format(text_segment.end_offset))

Java

import com.google.cloud.automl.v1.AnnotationPayload;
import com.google.cloud.automl.v1.ExamplePayload;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.PredictRequest;
import com.google.cloud.automl.v1.PredictResponse;
import com.google.cloud.automl.v1.PredictionServiceClient;
import com.google.cloud.automl.v1.TextSegment;
import com.google.cloud.automl.v1.TextSnippet;

import java.io.IOException;

class LanguageEntityExtractionPredict {

  static void predict() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String content = "text to predict";
    predict(projectId, modelId, content);
  }

  static void predict(String projectId, String modelId, String content) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
      // Get the full path of the model.
      ModelName name = ModelName.of(projectId, "us-central1", modelId);
      TextSnippet textSnippet =
          TextSnippet.newBuilder().setContent(content).setMimeType("text/plain").build();
      ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
      PredictRequest predictRequest =
          PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();

      PredictResponse response = client.predict(predictRequest);

      for (AnnotationPayload annotationPayload : response.getPayloadList()) {
        System.out.format("Text Extract Entity Type: %s\n", annotationPayload.getDisplayName());
        System.out.format("Text score: %.2f\n", annotationPayload.getTextExtraction().getScore());
        TextSegment textSegment = annotationPayload.getTextExtraction().getTextSegment();
        System.out.format("Text Extract Entity Content: %s\n", textSegment.getContent());
        System.out.format("Text Start Offset: %s\n", textSegment.getStartOffset());
        System.out.format("Text End Offset: %s\n\n", textSegment.getEndOffset());
      }
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const content = 'text to predict'

// Imports the Google Cloud AutoML library
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new PredictionServiceClient();

async function predict() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
    payload: {
      textSnippet: {
        content: content,
        mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
      },
    },
  };

  const [response] = await client.predict(request);

  for (const annotationPayload of response.payload) {
    console.log(
      `Text Extract Entity Types: ${annotationPayload.displayName}`
    );
    console.log(`Text Score: ${annotationPayload.textExtraction.score}`);
    const textSegment = annotationPayload.textExtraction.textSegment;
    console.log(`Text Extract Entity Content: ${textSegment.content}`);
    console.log(`Text Start Offset: ${textSegment.startOffset}`);
    console.log(`Text End Offset: ${textSegment.endOffset}`);
  }
}

predict();

C#

/// <summary>
/// Demonstrates using the AutoML client to predict the text content using given model.
/// </summary>
/// <param name="projectId">GCP Project ID.</param>
/// <param name="modelId">the Id of the model.</param>
/// <param name="content">Text content for prediction.</param>
public static object LanguageEntityExtractionPredict(string projectId = "YOUR-PROJECT-ID",
    string modelId = "YOUR-MODEL-ID",
    string content = "YOUR TEXT TO PREDICT")
{
    // Initialize the client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    PredictionServiceClient client = PredictionServiceClient.Create();

    // Get the full path of the model.
    string modelFullId = ModelName.Format(projectId, "us-central1", modelId);

    TextSnippet textSnippet = new TextSnippet
    {
        Content = content,
        MimeType = "text/plain"
    };
    ExamplePayload payload = new ExamplePayload
    {
        TextSnippet = textSnippet
    };

    PredictRequest predictRequest = new PredictRequest
    {
        Name = modelFullId,
        Payload = payload
    };

    PredictResponse response = client.Predict(predictRequest);

    foreach (AnnotationPayload annotationPayload in response.Payload)
    {
        Console.WriteLine($"Text Extract Entity Type: {annotationPayload.DisplayName}");
        Console.WriteLine($"Text score: {annotationPayload.TextExtraction.Score.ToString("0.00")}");
        TextSegment textSegment = annotationPayload.TextExtraction.TextSegment;
        Console.WriteLine($"Text Extract Entity Content: {textSegment.Content}");
        Console.WriteLine($"Text Start Offset: {textSegment.StartOffset}");
        Console.WriteLine($"Text End Offset: {textSegment.EndOffset}");
    }
    return 0;
}

Go

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)

// languageEntityExtractionPredict does a prediction for text entity extraction.
func languageEntityExtractionPredict(w io.Writer, projectID string, location string, modelID string, content string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "TEN123456789..."
	// content := "text to extract entities"

	ctx := context.Background()
	client, err := automl.NewPredictionClient(ctx)
	if err != nil {
		return fmt.Errorf("NewPredictionClient: %v", err)
	}
	defer client.Close()

	req := &automlpb.PredictRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
		Payload: &automlpb.ExamplePayload{
			Payload: &automlpb.ExamplePayload_TextSnippet{
				TextSnippet: &automlpb.TextSnippet{
					Content:  content,
					MimeType: "text/plain", // Types: "text/plain", "text/html"
				},
			},
		},
	}

	resp, err := client.Predict(ctx, req)
	if err != nil {
		return fmt.Errorf("Predict: %v", err)
	}

	for _, payload := range resp.GetPayload() {
		fmt.Fprintf(w, "Text extract entity types: %v\n", payload.GetDisplayName())
		fmt.Fprintf(w, "Text score: %v\n", payload.GetTextExtraction().GetScore())
		textSegment := payload.GetTextExtraction().GetTextSegment()
		fmt.Fprintf(w, "Text extract entity content: %v\n", textSegment.GetContent())
		fmt.Fprintf(w, "Text start offset: %v\n", textSegment.GetStartOffset())
		fmt.Fprintf(w, "Text end offset: %v\n", textSegment.GetEndOffset())
	}

	return nil
}

PHP

use Google\Cloud\AutoMl\V1\ExamplePayload;
use Google\Cloud\AutoMl\V1\PredictionServiceClient;
use Google\Cloud\AutoMl\V1\TextSnippet;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $modelId = 'my_model_id_123';
// $content = 'text to predict';

$client = new PredictionServiceClient();

try {
    // get full path of model
    $formattedName = $client->modelName(
        $projectId,
        $location,
        $modelId);

    // create payload
    $textSnippet = (new TextSnippet())
        ->setContent($content)
        ->setMimeType('text/plain'); // Types: 'text/plain', 'text/html'
    $payload = (new ExamplePayload())
        ->setTextSnippet($textSnippet);

    // predict with above model and payload
    $response = $client->predict($formattedName, $payload);
    $annotations = $response->getPayload();

    // display results
    foreach ($annotations as $annotation) {
        $textExtraction = $annotation->getTextExtraction();
        printf('Annotation name: %s' . PHP_EOL, $annotation->getDisplayName());
        printf('Text extraction score: %s' . PHP_EOL, $textExtraction->getScore());
        $textSegment = $textExtraction->getTextSegment();
        printf('Text segment: %s' . PHP_EOL, $textSegment->getContent());
        printf('Offset start: %s' . PHP_EOL, $textSegment->getStartOffset());
        printf('Offset end: %s' . PHP_EOL, $textSegment->getEndOffset());
    }
} finally {
    $client->close();
}

Ruby

require "google/cloud/automl"

project_id = "YOUR_PROJECT_ID"
model_id = "YOUR_MODEL_ID"
content = "text to predict"

prediction_client = Google::Cloud::AutoML::Prediction.new

# Get the full path of the model.
model_full_id = prediction_client.class.model_path project_id, "us-central1", model_id
payload = {
  text_snippet: {
    content:   content,
    # Types: 'text/plain', 'text/html'
    mime_type: "text/plain"
  }
}

response = prediction_client.predict model_full_id, payload

response.payload.each do |annotation_payload|
  puts "Text Extract Entity Types: #{annotation_payload.display_name}"
  puts "Text Score: #{annotation_payload.text_extraction.score}"
  text_segment = annotation_payload.text_extraction.text_segment
  puts "Text Extract Entity Content: #{text_segment.content}"
  puts "Text Start Offset: #{text_segment.start_offset}"
  puts "Text End Offset: #{text_segment.end_offset}"
end

Sentiment analysis

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your project ID
  • location-id: the location for the resource, us-central1 for the Global location or eu for the European Union
  • model-id: your model ID

HTTP method and URL:

POST https://automl.googleapis.com/v1/projects/project-id/locations/location-id/models/model-id:predict

Request JSON body:

{
  "payload" : {
    "textSnippet": {
      "content": "Enjoy your vacation!",
         "mime_type": "text/plain"
       },
  }
}

To send your request, expand one of these options:

You should receive a successful status code (2xx) and an empty response.

Python

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = 'YOUR_PROJECT_ID'
# model_id = 'YOUR_MODEL_ID'
# content = 'text to predict'

prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = prediction_client.model_path(
    project_id, 'us-central1', model_id
)

text_snippet = automl.types.TextSnippet(
    content=content,
    mime_type='text/plain')  # Types: 'text/plain', 'text/html'
payload = automl.types.ExamplePayload(text_snippet=text_snippet)

response = prediction_client.predict(model_full_id, payload)

for annotation_payload in response.payload:
    print(u'Predicted class name: {}'.format(
        annotation_payload.display_name))
    print(u'Predicted sentiment score: {}'.format(
        annotation_payload.text_sentiment.sentiment))

Java

import com.google.cloud.automl.v1.AnnotationPayload;
import com.google.cloud.automl.v1.ExamplePayload;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.PredictRequest;
import com.google.cloud.automl.v1.PredictResponse;
import com.google.cloud.automl.v1.PredictionServiceClient;
import com.google.cloud.automl.v1.TextSnippet;

import java.io.IOException;

class LanguageSentimentAnalysisPredict {

  static void predict() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String content = "text to predict";
    predict(projectId, modelId, content);
  }

  static void predict(String projectId, String modelId, String content) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
      // Get the full path of the model.
      ModelName name = ModelName.of(projectId, "us-central1", modelId);
      TextSnippet textSnippet =
          TextSnippet.newBuilder()
              .setContent(content)
              .setMimeType("text/plain") // Types: text/plain, text/html
              .build();
      ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
      PredictRequest predictRequest =
          PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();

      PredictResponse response = client.predict(predictRequest);

      for (AnnotationPayload annotationPayload : response.getPayloadList()) {
        System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
        System.out.format(
            "Predicted sentiment score: %d\n", annotationPayload.getTextSentiment().getSentiment());
      }
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const content = 'text to predict'

// Imports the Google Cloud AutoML library
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new PredictionServiceClient();

async function predict() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
    payload: {
      textSnippet: {
        content: content,
        mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
      },
    },
  };

  const [response] = await client.predict(request);

  for (const annotationPayload of response.payload) {
    console.log(`Predicted class name: ${annotationPayload.displayName}`);
    console.log(
      `Predicted sentiment score: ${annotationPayload.textSentiment.sentiment}`
    );
  }
}

predict();

C#

/// <summary>
/// Demonstrates using the AutoML client to predict the text content using given model.
/// </summary>
/// <param name="projectId">GCP Project ID.</param>
/// <param name="modelId">the Id of the model.</param>
/// <param name="content">Text content for prediction.</param>
public static object LanguageSentimentAnalysisPredict(string projectId = "YOUR-PROJECT-ID",
    string modelId = "YOUR-MODEL-ID",
    string content = "YOUR TEXT TO PREDICT")
{
    // Initialize the client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    PredictionServiceClient client = PredictionServiceClient.Create();

    // Get the full path of the model.
    string modelFullId = ModelName.Format(projectId, "us-central1", modelId);

    TextSnippet textSnippet = new TextSnippet
    {
        Content = content,
        MimeType = "text/plain" // Types: text/plain, text/html
    };
    ExamplePayload payload = new ExamplePayload
    {
        TextSnippet = textSnippet
    };

    PredictRequest predictRequest = new PredictRequest
    {
        Name = modelFullId,
        Payload = payload
    };

    PredictResponse response = client.Predict(predictRequest);

    foreach (AnnotationPayload annotationPayload in response.Payload)
    {
        Console.WriteLine($"Predicted class name: {annotationPayload.DisplayName}");
        Console.WriteLine(
            $"Predicted sentiment score: {annotationPayload.TextSentiment.Sentiment}");
    }
    return 0;
}

Go

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)

// languageSentimentAnalysisPredict does a prediction for text sentiment analysis.
func languageSentimentAnalysisPredict(w io.Writer, projectID string, location string, modelID string, content string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "TST123456789..."
	// content := "text to analyze sentiment"

	ctx := context.Background()
	client, err := automl.NewPredictionClient(ctx)
	if err != nil {
		return fmt.Errorf("NewPredictionClient: %v", err)
	}
	defer client.Close()

	req := &automlpb.PredictRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
		Payload: &automlpb.ExamplePayload{
			Payload: &automlpb.ExamplePayload_TextSnippet{
				TextSnippet: &automlpb.TextSnippet{
					Content:  content,
					MimeType: "text/plain", // Types: "text/plain", "text/html"
				},
			},
		},
	}

	resp, err := client.Predict(ctx, req)
	if err != nil {
		return fmt.Errorf("Predict: %v", err)
	}

	for _, payload := range resp.GetPayload() {
		fmt.Fprintf(w, "Predicted class name: %v\n", payload.GetDisplayName())
		fmt.Fprintf(w, "Predicted sentiment score: %v\n", payload.GetTextSentiment().GetSentiment())
	}

	return nil
}

PHP

use Google\Cloud\AutoMl\V1\ExamplePayload;
use Google\Cloud\AutoMl\V1\PredictionServiceClient;
use Google\Cloud\AutoMl\V1\TextSnippet;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $modelId = 'my_model_id_123';
// $content = 'text to predict';

$client = new PredictionServiceClient();

try {
    // get full path of model
    $formattedName = $client->modelName(
        $projectId,
        $location,
        $modelId);

    $textSnippet = (new TextSnippet())
        ->setContent($content)
        ->setMimeType('text/plain'); // Types: 'text/plain', 'text/html'
    // create payload
    $payload = (new ExamplePayload())
        ->setTextSnippet($textSnippet);

    // predict with above model and payload
    $response = $client->predict($formattedName, $payload);
    $annotations = $response->getPayload();

    // display results
    foreach ($annotations as $annotation) {
        $textSentiment = $annotation->getTextSentiment();
        printf('Predicted sentiment name: %s' . PHP_EOL, $annotation->getDisplayName());
        printf('Predicted sentiment score: %s' . PHP_EOL, $textSentiment->getSentiment());
    }
} finally {
    $client->close();
}

Ruby

require "google/cloud/automl"

project_id = "YOUR_PROJECT_ID"
model_id = "YOUR_MODEL_ID"
content = "text to predict"

prediction_client = Google::Cloud::AutoML::Prediction.new

# Get the full path of the model.
model_full_id = prediction_client.class.model_path project_id, "us-central1", model_id
payload = {
  text_snippet: {
    content:   content,
    # Types: 'text/plain', 'text/html'
    mime_type: "text/plain"
  }
}

response = prediction_client.predict model_full_id, payload

response.payload.each do |annotation_payload|
  puts "Predicted class name: #{annotation_payload.display_name}"
  puts "Predicted sentiment score: #{annotation_payload.text_sentiment.sentiment}"
end

Batch prediction

To classify a collection of documents with a high-throughput asynchronous process, use the batchPredict method. Create a CSV file with a single column that lists the files to classify, one file per row. The CSV file and each input file needs to be stored in your Google Cloud Storage bucket.

gs://folder/text1.txt
gs://folder/text2.pdf

If you would like to use your model to do high-throughput asynchronous prediction on a corpus of documents you can use the batchPredict method.

For entity analysis models, you need to prepare a JSONL file that contains all the documents that you would like to analyze. Each document should be linked to a unique id.

{ "id": "0", "text_snippet": { "content": "First item content to be analyzed." } }
{ "id": "1", "text_snippet": { "content": "Second item content to be analyzed." } }
...
{ "id": "n", "text_snippet": { "content": "Last item content to be analyzed." } }

REST & CMD LINE

Before using any of the request data below, make the following replacements:

  • project-id: your project ID
  • location-id: the location for the resource, us-central1 for the Global location or eu for the European Union
  • model-id: your model ID

HTTP method and URL:

POST https://automl.googleapis.com/v1/projects/project-id/locations/location-id/models/model-id:batchPredict

Request JSON body:

{
  "input_config": { "gcs_source": { "input_uris": [ "csv-file-URI"] } },
  "output_config": { "gcs_destination": { "output_uri_prefix": "dest-dir-URI" } }
 }

To send your request, expand one of these options:

You should see output similar to the following. You can use the operation ID to get the status of the task. For an example, see Getting the status of an operation.

{
  "name": "projects/434039606874/locations/us-central1/operations/TCN8195786061721370625",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.automl.v1beta1.OperationMetadata",
    "createTime": "2019-03-13T15:37:49.972372Z",
    "updateTime": "2019-03-13T15:37:49.972372Z"
  }
}

Python

from google.cloud import automl

# TODO(developer): Uncomment and set the following variables
# project_id = 'YOUR_PROJECT_ID'
# model_id = 'YOUR_MODEL_ID'
# input_uri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl'
# output_uri = 'gs://YOUR_BUCKET_ID/path_to_save_results/'

prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = prediction_client.model_path(
    project_id, 'us-central1', model_id)

gcs_source = automl.types.GcsSource(
    input_uris=[input_uri])

input_config = automl.types.BatchPredictInputConfig(gcs_source=gcs_source)
gcs_destination = automl.types.GcsDestination(
    output_uri_prefix=output_uri)
output_config = automl.types.BatchPredictOutputConfig(
    gcs_destination=gcs_destination)
# [0.0-1.0] Only produce results higher than this value
params = {'score_threshold': '0.8'}

response = prediction_client.batch_predict(
    model_full_id, input_config, output_config, params)

print('Waiting for operation to complete...')
print(u'Batch Prediction results saved to Cloud Storage bucket. {}'.format(
    response.result()))

Java

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1.BatchPredictInputConfig;
import com.google.cloud.automl.v1.BatchPredictOutputConfig;
import com.google.cloud.automl.v1.BatchPredictRequest;
import com.google.cloud.automl.v1.BatchPredictResult;
import com.google.cloud.automl.v1.GcsDestination;
import com.google.cloud.automl.v1.GcsSource;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.cloud.automl.v1.PredictionServiceClient;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class BatchPredict {

  static void batchPredict() throws IOException, ExecutionException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    String modelId = "YOUR_MODEL_ID";
    String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl";
    String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/";
    batchPredict(projectId, modelId, inputUri, outputUri);
  }

  static void batchPredict(String projectId, String modelId, String inputUri, String outputUri)
          throws IOException, ExecutionException, InterruptedException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
      // Get the full path of the model.
      ModelName name = ModelName.of(projectId, "us-central1", modelId);
      GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
      BatchPredictInputConfig inputConfig =
              BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
      GcsDestination gcsDestination =
              GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
      BatchPredictOutputConfig outputConfig =
              BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
      BatchPredictRequest request =
              BatchPredictRequest.newBuilder()
                      .setName(name.toString())
                      .setInputConfig(inputConfig)
                      .setOutputConfig(outputConfig)
                      .build();

      OperationFuture<BatchPredictResult, OperationMetadata> future =
              client.batchPredictAsync(request);

      System.out.println("Waiting for operation to complete...");
      BatchPredictResult response = future.get();
      System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl';
// const outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/';

// Imports the Google Cloud AutoML library
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new PredictionServiceClient();

async function batchPredict() {
  // Construct request
  const request = {
    name: client.modelPath(projectId, location, modelId),
    inputConfig: {
      gcsSource: {
        inputUris: [inputUri],
      },
    },
    outputConfig: {
      gcsDestination: {
        outputUriPrefix: outputUri,
      },
    },
  };

  const [operation] = await client.batchPredict(request);

  console.log(`Waiting for operation to complete...`);
  // Wait for operation to complete.
  const [response] = await operation.promise();
  console.log(
    `Batch Prediction results saved to Cloud Storage bucket. ${response}`
  );
}

batchPredict();

C#

/// <summary>
/// Demonstrates using the AutoML client to predict image or text contents.
/// </summary>
/// <param name="projectId">GCP Project ID.</param>
/// <param name="modelId">the Id of the model.</param>
/// <param name="inputUri">GCS bucket of your csv or jsonl file that contains paths to the images or text contents.</param>
/// <param name="outputUri">The GCS path to store the output of your prediction request.</param>
public static object BatchPredict(string projectId = "YOUR-PROJECT-ID",
    string modelId = "YOUR-MODEL-ID",
    string inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl", //images or text contents
    string outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/")
{
    // Initialize the client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    PredictionServiceClient client = PredictionServiceClient.Create();

    // Get the full path of the model.
    string modelFullId = ModelName.Format(projectId, "us-central1", modelId);

    GcsSource gcsSource = new GcsSource
    {
        InputUris = { inputUri }
    };

    BatchPredictInputConfig inputConfig = new BatchPredictInputConfig
    {
        GcsSource = gcsSource
    };

    GcsDestination gcsDestination = new GcsDestination
    {
        OutputUriPrefix = outputUri
    };

    BatchPredictOutputConfig outputConfig = new BatchPredictOutputConfig
    {
        GcsDestination = gcsDestination
    };

    BatchPredictRequest request = new BatchPredictRequest
    {
        Name = modelFullId,
        InputConfig = inputConfig,
        OutputConfig = outputConfig
    };

    var result = Task.Run(() => client.BatchPredictAsync(request)).Result;
    Console.WriteLine("Waiting for operation to complete...");
    result.PollUntilCompleted();

    Console.WriteLine("Batch Prediction results saved to specified Cloud Storage bucket.");
    return 0;
}

Go

import (
	"context"
	"fmt"
	"io"

	automl "cloud.google.com/go/automl/apiv1"
	automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)

// batchPredict does a batch prediction.
func batchPredict(w io.Writer, projectID string, location string, modelID string, inputURI string, outputURI string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// modelID := "ICN123456789..."
	// inputURI := "gs://BUCKET_ID/path_to_your_input_csv_or_jsonl"
	// outputURI := "gs://BUCKET_ID/path_to_save_results/"

	ctx := context.Background()
	client, err := automl.NewPredictionClient(ctx)
	if err != nil {
		return fmt.Errorf("NewPredictionClient: %v", err)
	}
	defer client.Close()

	req := &automlpb.BatchPredictRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
		InputConfig: &automlpb.BatchPredictInputConfig{
			Source: &automlpb.BatchPredictInputConfig_GcsSource{
				GcsSource: &automlpb.GcsSource{
					InputUris: []string{inputURI},
				},
			},
		},
		OutputConfig: &automlpb.BatchPredictOutputConfig{
			Destination: &automlpb.BatchPredictOutputConfig_GcsDestination{
				GcsDestination: &automlpb.GcsDestination{
					OutputUriPrefix: outputURI,
				},
			},
		},
		Params: map[string]string{
			"score_threshold": "0.8", // [0.0-1.0] Only produce results higher than this value
		},
	}

	op, err := client.BatchPredict(ctx, req)
	if err != nil {
		return fmt.Errorf("BatchPredict: %v", err)
	}
	fmt.Fprintf(w, "Processing operation name: %q\n", op.Name())

	resp, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait: %v", err)
	}

	fmt.Fprintf(w, "Batch Prediction results saved to Cloud Storage bucket.\n")
	fmt.Fprintf(w, "%v", resp)

	return nil
}

PHP

use Google\Cloud\AutoMl\V1\BatchPredictInputConfig;
use Google\Cloud\AutoMl\V1\BatchPredictOutputConfig;
use Google\Cloud\AutoMl\V1\GcsSource;
use Google\Cloud\AutoMl\V1\GcsDestination;
use Google\Cloud\AutoMl\V1\PredictionServiceClient;

/** Uncomment and populate these variables in your code */
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $modelId = 'my_model_id_123';
// $inputUri = 'gs://cloud-samples-data/text.txt';
// $outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';

$client = new PredictionServiceClient();

try {
    // get full path of model
    $formattedName = $client->modelName(
        $projectId,
        $location,
        $modelId
    );

    // set the multiple GCS uri
    $gcsSource = (new GcsSource())
        ->setInputUri($inputUri);
    $inputConfig = (new BatchPredictInputConfig())
        ->setGcsSource($gcsSource);
    $gcsDestination = (new GcsDestination())
        ->setInputUri($outputUri);
    $outputConfig = (new BatchPredictOutputConfig())
        ->setGcsDestination($gcsDestination);
    // params is additional domain-specific parameters
    // score_threshold is used to filter the result
    $params = ['score_threshold' => '0.8']; // value between 0.0 and 1.0

    $operationResponse = $client->batchPredict(
        $formattedName,
        $inputConfig,
        $outputConfig,
        $params
    );

    printf('Waiting for operation to complete...' . PHP_EOL);
    $operationResponse->pollUntilComplete();
    if ($operationResponse->operationSucceeded()) {
        $result = $operationResponse->getResult();
        printf('Batch Prediction results saved to Cloud Storage bucket. %s' . PHP_EOL, $result);
    } else {
        $error = $operationResponse->getError();
        print($error->getMessage());
    }
} finally {
    $client->close();
}

Ruby

require "google/cloud/automl"

project_id = "YOUR_PROJECT_ID"
model_id = "YOUR_MODEL_ID"
input_uri = "gs://YOUR_BUCKET_ID/path_to_your_input_file.jsonl"
output_uri = "gs://YOUR_BUCKET_ID/path_to_save_results/"

prediction_client = Google::Cloud::AutoML::Prediction.new

# Get the full path of the model.
model_full_id = prediction_client.class.model_path project_id, "us-central1", model_id
input_config = {
  gcs_source: {
    input_uris: [input_uri]
  }
}
output_config = {
  gcs_destination: {
    output_uri_prefix: output_uri
  }
}

operation = prediction_client.batch_predict model_full_id, input_config, output_config

puts "Waiting for operation to complete..."

# Wait until the long running operation is done
operation.wait_until_done!

puts "Batch Prediction results saved to Cloud Storage bucket."

Was this page helpful? Let us know how we did:

Send feedback about...

AutoML Natural Language
Need help? Visit our support page.