Search in sources :

Example 6 with PredictionServiceClient

use of com.google.cloud.aiplatform.v1.PredictionServiceClient in project java-automl by googleapis.

the class TablesBatchPredictBigQuery method batchPredict.

static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
    // 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);
        // Configure the source of the file from BigQuery
        BigQuerySource bigQuerySource = BigQuerySource.newBuilder().setInputUri(inputUri).build();
        BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().setBigquerySource(bigQuerySource).build();
        // Configure where to store the output in BigQuery
        BigQueryDestination bigQueryDestination = BigQueryDestination.newBuilder().setOutputUri(outputUri).build();
        BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().setBigqueryDestination(bigQueryDestination).build();
        // Build the request that will be sent to the API
        BatchPredictRequest request = BatchPredictRequest.newBuilder().setName(name.toString()).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
        // Start an asynchronous request
        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 BigQuery.");
    }
}
Also used : BatchPredictRequest(com.google.cloud.automl.v1beta1.BatchPredictRequest) ModelName(com.google.cloud.automl.v1beta1.ModelName) BatchPredictInputConfig(com.google.cloud.automl.v1beta1.BatchPredictInputConfig) BatchPredictOutputConfig(com.google.cloud.automl.v1beta1.BatchPredictOutputConfig) BatchPredictResult(com.google.cloud.automl.v1beta1.BatchPredictResult) BigQuerySource(com.google.cloud.automl.v1beta1.BigQuerySource) BigQueryDestination(com.google.cloud.automl.v1beta1.BigQueryDestination) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) PredictionServiceClient(com.google.cloud.automl.v1beta1.PredictionServiceClient)

Example 7 with PredictionServiceClient

use of com.google.cloud.aiplatform.v1.PredictionServiceClient in project java-automl by googleapis.

the class LanguageEntityExtractionPredict method predict.

static void predict(String projectId, String modelId, String content) throws IOException {
    // 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);
        // For available mime types, see:
        // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet
        TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).setMimeType(// Types: text/plain, text/html
        "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());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) TextSnippet(com.google.cloud.automl.v1.TextSnippet) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) TextSegment(com.google.cloud.automl.v1.TextSegment) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 8 with PredictionServiceClient

use of com.google.cloud.aiplatform.v1.PredictionServiceClient in project java-automl by googleapis.

the class LanguageSentimentAnalysisPredict method predict.

static void predict(String projectId, String modelId, String content) throws IOException {
    // 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);
        // For available mime types, see:
        // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet
        TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).setMimeType(// Types: text/plain, text/html
        "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("Predicted class name: %s\n", annotationPayload.getDisplayName());
            System.out.format("Predicted sentiment score: %d\n", annotationPayload.getTextSentiment().getSentiment());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) TextSnippet(com.google.cloud.automl.v1.TextSnippet) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 9 with PredictionServiceClient

use of com.google.cloud.aiplatform.v1.PredictionServiceClient in project java-automl by googleapis.

the class PredictionApi method predict.

// [START automl_vision_predict]
/**
 * Demonstrates using the AutoML client to predict an image.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param modelId the Id of the model which will be used for text classification.
 * @param filePath the Local text file path of the content to be classified.
 * @param scoreThreshold the Confidence score. Only classifications with confidence score above
 *     scoreThreshold are displayed.
 */
static void predict(String projectId, String computeRegion, String modelId, String filePath, String scoreThreshold) throws IOException {
    // Instantiate client for prediction service.
    try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, computeRegion, modelId);
        // Read the image and assign to payload.
        ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
        Image image = Image.newBuilder().setImageBytes(content).build();
        ExamplePayload examplePayload = ExamplePayload.newBuilder().setImage(image).build();
        // Additional parameters that can be provided for prediction e.g. Score Threshold
        Map<String, String> params = new HashMap<>();
        if (scoreThreshold != null) {
            params.put("score_threshold", scoreThreshold);
        }
        // Perform the AutoML Prediction request
        PredictResponse response = predictionClient.predict(name, examplePayload, params);
        System.out.println("Prediction results:");
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.println("Predicted class name :" + annotationPayload.getDisplayName());
            System.out.println("Predicted class score :" + annotationPayload.getClassification().getScore());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.automl.v1.Image) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 10 with PredictionServiceClient

use of com.google.cloud.aiplatform.v1.PredictionServiceClient in project java-automl by googleapis.

the class BatchPredict method batchPredict.

static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
    // 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.");
    }
}
Also used : BatchPredictRequest(com.google.cloud.automl.v1.BatchPredictRequest) ModelName(com.google.cloud.automl.v1.ModelName) GcsSource(com.google.cloud.automl.v1.GcsSource) BatchPredictInputConfig(com.google.cloud.automl.v1.BatchPredictInputConfig) BatchPredictOutputConfig(com.google.cloud.automl.v1.BatchPredictOutputConfig) BatchPredictResult(com.google.cloud.automl.v1.BatchPredictResult) GcsDestination(com.google.cloud.automl.v1.GcsDestination) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient)

Aggregations

EndpointName (com.google.cloud.aiplatform.v1.EndpointName)8 PredictResponse (com.google.cloud.aiplatform.v1.PredictResponse)8 PredictionServiceClient (com.google.cloud.aiplatform.v1.PredictionServiceClient)8 PredictionServiceSettings (com.google.cloud.aiplatform.v1.PredictionServiceSettings)8 ModelName (com.google.cloud.automl.v1.ModelName)8 PredictionServiceClient (com.google.cloud.automl.v1.PredictionServiceClient)8 Value (com.google.protobuf.Value)8 ExamplePayload (com.google.cloud.automl.v1.ExamplePayload)7 PredictResponse (com.google.cloud.automl.v1.PredictResponse)7 AnnotationPayload (com.google.cloud.automl.v1.AnnotationPayload)6 PredictRequest (com.google.cloud.automl.v1.PredictRequest)6 ArrayList (java.util.ArrayList)5 TextSnippet (com.google.cloud.automl.v1.TextSnippet)4 ModelName (com.google.cloud.automl.v1beta1.ModelName)4 PredictionServiceClient (com.google.cloud.automl.v1beta1.PredictionServiceClient)4 ByteString (com.google.protobuf.ByteString)4 Image (com.google.cloud.automl.v1.Image)3 ListValue (com.google.protobuf.ListValue)3 ClassificationPredictionResult (com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)2 AnnotationPayload (com.google.cloud.automl.v1beta1.AnnotationPayload)2