Search in sources :

Example 1 with PredictionServiceClient

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

the class PredictCustomTrainedModelSample method predictCustomTrainedModel.

static void predictCustomTrainedModel(String project, String endpointId, String instance) 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();
        PredictRequest predictRequest = PredictRequest.newBuilder().setEndpoint(endpointName.toString()).addAllInstances(instanceList).build();
        PredictResponse predictResponse = predictionServiceClient.predict(predictRequest);
        System.out.println("Predict Custom Trained model Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            System.out.format("\tPrediction: %s\n", prediction);
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ListValue(com.google.protobuf.ListValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictRequest(com.google.cloud.aiplatform.v1.PredictRequest) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Example 2 with PredictionServiceClient

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

the class PredictImageClassificationSample method predictImageClassification.

static void predictImageClassification(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);
        ImageClassificationPredictionInstance predictionInstance = ImageClassificationPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(predictionInstance));
        ImageClassificationPredictionParams predictionParams = ImageClassificationPredictionParams.newBuilder().setConfidenceThreshold((float) 0.5).setMaxPredictions(5).build();
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.toValue(predictionParams));
        System.out.println("Predict Image Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        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 : 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) ImageClassificationPredictionParams(com.google.cloud.aiplatform.v1.schema.predict.params.ImageClassificationPredictionParams) Value(com.google.protobuf.Value) ImageClassificationPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.ImageClassificationPredictionInstance) ClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)

Example 3 with PredictionServiceClient

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

the class PredictTabularClassificationSample method predictTabularClassification.

static void predictTabularClassification(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 Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            TabularClassificationPredictionResult.Builder resultBuilder = TabularClassificationPredictionResult.newBuilder();
            TabularClassificationPredictionResult result = (TabularClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getClassesCount(); i++) {
                System.out.printf("\tClass: %s", result.getClasses(i));
                System.out.printf("\tScore: %f", result.getScores(i));
            }
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ListValue(com.google.protobuf.ListValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) TabularClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.TabularClassificationPredictionResult) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Example 4 with PredictionServiceClient

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

the class PredictTextSentimentAnalysisSample method predictTextSentimentAnalysis.

static void predictTextSentimentAnalysis(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);
        Value parameter = Value.newBuilder().setNumberValue(0).setNumberValue(5).build();
        Value.Builder instance = Value.newBuilder();
        JsonFormat.parser().merge(jsonString, instance);
        List<Value> instances = new ArrayList<>();
        instances.add(instance.build());
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, parameter);
        System.out.println("Predict Text Sentiment Analysis Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            System.out.format("\tPrediction: %s\n", prediction);
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) Value(com.google.protobuf.Value) ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Example 5 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);
        // Configure the source of the file from a GCS bucket
        GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
        BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
        // Configure where to store the output in a GCS bucket
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).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 specified Cloud Storage bucket.");
    }
}
Also used : BatchPredictRequest(com.google.cloud.automl.v1beta1.BatchPredictRequest) ModelName(com.google.cloud.automl.v1beta1.ModelName) GcsSource(com.google.cloud.automl.v1beta1.GcsSource) BatchPredictInputConfig(com.google.cloud.automl.v1beta1.BatchPredictInputConfig) BatchPredictOutputConfig(com.google.cloud.automl.v1beta1.BatchPredictOutputConfig) BatchPredictResult(com.google.cloud.automl.v1beta1.BatchPredictResult) GcsDestination(com.google.cloud.automl.v1beta1.GcsDestination) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) PredictionServiceClient(com.google.cloud.automl.v1beta1.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