Search in sources :

Example 6 with EndpointName

use of com.google.cloud.aiplatform.v1.EndpointName 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 7 with EndpointName

use of com.google.cloud.aiplatform.v1.EndpointName 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 8 with EndpointName

use of com.google.cloud.aiplatform.v1.EndpointName 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 9 with EndpointName

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

the class UndeployModelSample method undeployModelSample.

static void undeployModelSample(String project, String endpointId, String modelId) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    EndpointServiceSettings endpointServiceSettings = EndpointServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (EndpointServiceClient endpointServiceClient = EndpointServiceClient.create(endpointServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        ModelName modelName = ModelName.of(project, location, modelId);
        // key '0' assigns traffic for the newly deployed model
        // Traffic percentage values must add up to 100
        // Leave dictionary empty if endpoint should not accept any traffic
        Map<String, Integer> trafficSplit = new HashMap<>();
        trafficSplit.put("0", 100);
        OperationFuture<UndeployModelResponse, UndeployModelOperationMetadata> operation = endpointServiceClient.undeployModelAsync(endpointName.toString(), modelName.toString(), trafficSplit);
        System.out.format("Operation name: %s\n", operation.getInitialFuture().get().getName());
        System.out.println("Waiting for operation to finish...");
        UndeployModelResponse undeployModelResponse = operation.get(180, TimeUnit.SECONDS);
        System.out.format("Undeploy Model Response: %s\n", undeployModelResponse);
    }
}
Also used : ModelName(com.google.cloud.aiplatform.v1.ModelName) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) HashMap(java.util.HashMap) EndpointServiceClient(com.google.cloud.aiplatform.v1.EndpointServiceClient) UndeployModelResponse(com.google.cloud.aiplatform.v1.UndeployModelResponse) EndpointServiceSettings(com.google.cloud.aiplatform.v1.EndpointServiceSettings) UndeployModelOperationMetadata(com.google.cloud.aiplatform.v1.UndeployModelOperationMetadata)

Example 10 with EndpointName

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

the class DeployModelCustomTrainedModelSample method deployModelCustomTrainedModelSample.

static void deployModelCustomTrainedModelSample(String project, String endpointId, String model, String deployedModelDisplayName) throws IOException, ExecutionException, InterruptedException {
    EndpointServiceSettings settings = EndpointServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (EndpointServiceClient client = EndpointServiceClient.create(settings)) {
        MachineSpec machineSpec = MachineSpec.newBuilder().setMachineType("n1-standard-2").build();
        DedicatedResources dedicatedResources = DedicatedResources.newBuilder().setMinReplicaCount(1).setMachineSpec(machineSpec).build();
        String modelName = ModelName.of(project, location, model).toString();
        DeployedModel deployedModel = DeployedModel.newBuilder().setModel(modelName).setDisplayName(deployedModelDisplayName).setDedicatedResources(dedicatedResources).build();
        // key '0' assigns traffic for the newly deployed model
        // Traffic percentage values must add up to 100
        // Leave dictionary empty if endpoint should not accept any traffic
        Map<String, Integer> trafficSplit = new HashMap<>();
        trafficSplit.put("0", 100);
        EndpointName endpoint = EndpointName.of(project, location, endpointId);
        OperationFuture<DeployModelResponse, DeployModelOperationMetadata> response = client.deployModelAsync(endpoint, deployedModel, trafficSplit);
        // You can use OperationFuture.getInitialFuture to get a future representing the initial
        // response to the request, which contains information while the operation is in progress.
        System.out.format("Operation name: %s\n", response.getInitialFuture().get().getName());
        // OperationFuture.get() will block until the operation is finished.
        DeployModelResponse deployModelResponse = response.get();
        System.out.format("deployModelResponse: %s\n", deployModelResponse);
    }
}
Also used : HashMap(java.util.HashMap) DedicatedResources(com.google.cloud.aiplatform.v1.DedicatedResources) MachineSpec(com.google.cloud.aiplatform.v1.MachineSpec) DeployedModel(com.google.cloud.aiplatform.v1.DeployedModel) DeployModelResponse(com.google.cloud.aiplatform.v1.DeployModelResponse) DeployModelOperationMetadata(com.google.cloud.aiplatform.v1.DeployModelOperationMetadata) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) EndpointServiceClient(com.google.cloud.aiplatform.v1.EndpointServiceClient) EndpointServiceSettings(com.google.cloud.aiplatform.v1.EndpointServiceSettings)

Aggregations

EndpointName (com.google.cloud.aiplatform.v1.EndpointName)12 PredictResponse (com.google.cloud.aiplatform.v1.PredictResponse)8 PredictionServiceClient (com.google.cloud.aiplatform.v1.PredictionServiceClient)8 PredictionServiceSettings (com.google.cloud.aiplatform.v1.PredictionServiceSettings)8 Value (com.google.protobuf.Value)8 ArrayList (java.util.ArrayList)5 EndpointServiceClient (com.google.cloud.aiplatform.v1.EndpointServiceClient)4 EndpointServiceSettings (com.google.cloud.aiplatform.v1.EndpointServiceSettings)4 ListValue (com.google.protobuf.ListValue)3 HashMap (java.util.HashMap)3 DedicatedResources (com.google.cloud.aiplatform.v1.DedicatedResources)2 DeployModelOperationMetadata (com.google.cloud.aiplatform.v1.DeployModelOperationMetadata)2 DeployModelResponse (com.google.cloud.aiplatform.v1.DeployModelResponse)2 DeployedModel (com.google.cloud.aiplatform.v1.DeployedModel)2 MachineSpec (com.google.cloud.aiplatform.v1.MachineSpec)2 ModelName (com.google.cloud.aiplatform.v1.ModelName)2 ClassificationPredictionResult (com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)2 AutomaticResources (com.google.cloud.aiplatform.v1.AutomaticResources)1 DeleteOperationMetadata (com.google.cloud.aiplatform.v1.DeleteOperationMetadata)1 PredictRequest (com.google.cloud.aiplatform.v1.PredictRequest)1