Search in sources :

Example 16 with PredictionServiceClient

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

the class LanguageTextClassificationPredict 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: %.2f\n\n", annotationPayload.getClassification().getScore());
        }
    }
}
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 17 with PredictionServiceClient

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

the class PredictTabularRegressionSample method predictTabularRegression.

static void predictTabularRegression(String instance, String project, String endpointId) throws IOException {
    PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        ListValue.Builder listValue = ListValue.newBuilder();
        JsonFormat.parser().merge(instance, listValue);
        List<Value> instanceList = listValue.getValuesList();
        Value parameters = Value.newBuilder().setListValue(listValue).build();
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instanceList, parameters);
        System.out.println("Predict Tabular Regression Response");
        System.out.format("\tDisplay Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            TabularRegressionPredictionResult.Builder resultBuilder = TabularRegressionPredictionResult.newBuilder();
            TabularRegressionPredictionResult result = (TabularRegressionPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            System.out.printf("\tUpper bound: %f\n", result.getUpperBound());
            System.out.printf("\tLower bound: %f\n", result.getLowerBound());
            System.out.printf("\tValue: %f\n", result.getValue());
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) TabularRegressionPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.TabularRegressionPredictionResult) ListValue(com.google.protobuf.ListValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Example 18 with PredictionServiceClient

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

the class PredictTextClassificationSingleLabelSample method predictTextClassificationSingleLabel.

static void predictTextClassificationSingleLabel(String project, String content, String endpointId) throws IOException {
    PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        TextClassificationPredictionInstance predictionInstance = TextClassificationPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(predictionInstance));
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.EMPTY_VALUE);
        System.out.println("Predict Text Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions:\n\n");
        for (Value prediction : predictResponse.getPredictionsList()) {
            ClassificationPredictionResult.Builder resultBuilder = ClassificationPredictionResult.newBuilder();
            // Display names and confidences values correspond to
            // IDs in the ID list.
            ClassificationPredictionResult result = (ClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            int counter = 0;
            for (Long id : result.getIdsList()) {
                System.out.printf("Label ID: %d\n", id);
                System.out.printf("Label: %s\n", result.getDisplayNames(counter));
                System.out.printf("Confidence: %.4f\n", result.getConfidences(counter));
                counter++;
            }
        }
    }
}
Also used : TextClassificationPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.TextClassificationPredictionInstance) ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) Value(com.google.protobuf.Value) ClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)

Example 19 with PredictionServiceClient

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

the class PredictTextEntityExtractionSample method predictTextEntityExtraction.

static void predictTextEntityExtraction(String project, String content, String endpointId) throws IOException {
    PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
        String location = "us-central1";
        String jsonString = "{\"content\": \"" + content + "\"}";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        TextExtractionPredictionInstance instance = TextExtractionPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(instance));
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.EMPTY_VALUE);
        System.out.println("Predict Text Entity Extraction Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            TextExtractionPredictionResult.Builder resultBuilder = TextExtractionPredictionResult.newBuilder();
            TextExtractionPredictionResult result = (TextExtractionPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getIdsCount(); i++) {
                long textStartOffset = result.getTextSegmentStartOffsets(i);
                long textEndOffset = result.getTextSegmentEndOffsets(i);
                String entity = content.substring((int) textStartOffset, (int) textEndOffset);
                System.out.format("\tEntity: %s\n", entity);
                System.out.format("\tEntity type: %s\n", result.getDisplayNames(i));
                System.out.format("\tConfidences: %f\n", result.getConfidences(i));
                System.out.format("\tIDs: %d\n", result.getIds(i));
            }
        }
    }
}
Also used : TextExtractionPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.TextExtractionPredictionInstance) ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) TextExtractionPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.TextExtractionPredictionResult) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) Value(com.google.protobuf.Value)

Example 20 with PredictionServiceClient

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

the class PredictImageObjectDetectionSample method predictImageObjectDetection.

static void predictImageObjectDetection(String project, String fileName, String endpointId) throws IOException {
    PredictionServiceSettings settings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(settings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        byte[] contents = Base64.encodeBase64(Files.readAllBytes(Paths.get(fileName)));
        String content = new String(contents, StandardCharsets.UTF_8);
        ImageObjectDetectionPredictionParams params = ImageObjectDetectionPredictionParams.newBuilder().setConfidenceThreshold((float) (0.5)).setMaxPredictions(5).build();
        ImageObjectDetectionPredictionInstance instance = ImageObjectDetectionPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(instance));
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.toValue(params));
        System.out.println("Predict Image Object Detection Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            ImageObjectDetectionPredictionResult.Builder resultBuilder = ImageObjectDetectionPredictionResult.newBuilder();
            ImageObjectDetectionPredictionResult result = (ImageObjectDetectionPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getIdsCount(); i++) {
                System.out.printf("\tDisplay name: %s\n", result.getDisplayNames(i));
                System.out.printf("\tConfidences: %f\n", result.getConfidences(i));
                System.out.printf("\tIDs: %d\n", result.getIds(i));
                System.out.printf("\tBounding boxes: %s\n", result.getBboxes(i));
            }
        }
    }
}
Also used : ImageObjectDetectionPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.ImageObjectDetectionPredictionInstance) ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) ImageObjectDetectionPredictionParams(com.google.cloud.aiplatform.v1.schema.predict.params.ImageObjectDetectionPredictionParams) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) Value(com.google.protobuf.Value) ImageObjectDetectionPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.ImageObjectDetectionPredictionResult)

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