Search in sources :

Example 11 with EndpointName

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

the class DeployModelSample method deployModelSample.

static void deployModelSample(String project, String deployedModelDisplayName, 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);
        // 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);
        ModelName modelName = ModelName.of(project, location, modelId);
        AutomaticResources automaticResourcesInput = AutomaticResources.newBuilder().setMinReplicaCount(1).setMaxReplicaCount(1).build();
        DeployedModel deployedModelInput = DeployedModel.newBuilder().setModel(modelName.toString()).setDisplayName(deployedModelDisplayName).setAutomaticResources(automaticResourcesInput).build();
        OperationFuture<DeployModelResponse, DeployModelOperationMetadata> deployModelResponseFuture = endpointServiceClient.deployModelAsync(endpointName, deployedModelInput, trafficSplit);
        System.out.format("Operation name: %s\n", deployModelResponseFuture.getInitialFuture().get().getName());
        System.out.println("Waiting for operation to finish...");
        DeployModelResponse deployModelResponse = deployModelResponseFuture.get(20, TimeUnit.MINUTES);
        System.out.println("Deploy Model Response");
        DeployedModel deployedModel = deployModelResponse.getDeployedModel();
        System.out.println("\tDeployed Model");
        System.out.format("\t\tid: %s\n", deployedModel.getId());
        System.out.format("\t\tmodel: %s\n", deployedModel.getModel());
        System.out.format("\t\tDisplay Name: %s\n", deployedModel.getDisplayName());
        System.out.format("\t\tCreate Time: %s\n", deployedModel.getCreateTime());
        DedicatedResources dedicatedResources = deployedModel.getDedicatedResources();
        System.out.println("\t\tDedicated Resources");
        System.out.format("\t\t\tMin Replica Count: %s\n", dedicatedResources.getMinReplicaCount());
        MachineSpec machineSpec = dedicatedResources.getMachineSpec();
        System.out.println("\t\t\tMachine Spec");
        System.out.format("\t\t\t\tMachine Type: %s\n", machineSpec.getMachineType());
        System.out.format("\t\t\t\tAccelerator Type: %s\n", machineSpec.getAcceleratorType());
        System.out.format("\t\t\t\tAccelerator Count: %s\n", machineSpec.getAcceleratorCount());
        AutomaticResources automaticResources = deployedModel.getAutomaticResources();
        System.out.println("\t\tAutomatic Resources");
        System.out.format("\t\t\tMin Replica Count: %s\n", automaticResources.getMinReplicaCount());
        System.out.format("\t\t\tMax Replica Count: %s\n", automaticResources.getMaxReplicaCount());
    }
}
Also used : ModelName(com.google.cloud.aiplatform.v1.ModelName) 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) AutomaticResources(com.google.cloud.aiplatform.v1.AutomaticResources) EndpointServiceClient(com.google.cloud.aiplatform.v1.EndpointServiceClient) EndpointServiceSettings(com.google.cloud.aiplatform.v1.EndpointServiceSettings)

Example 12 with EndpointName

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

Example 13 with EndpointName

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

the class EndpointsDelete method deleteEndpoint.

// Delete an endpoint.
public static void deleteEndpoint(String projectId, String locationId, String namespaceId, String serviceId, String endpointId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {
        // The endpoint to delete.
        EndpointName endpointName = EndpointName.of(projectId, locationId, namespaceId, serviceId, endpointId);
        // Send the request to delete the endpoint.
        client.deleteEndpoint(endpointName);
        // Log the action.
        System.out.println("Deleted Endpoint: " + endpointName.toString());
    }
}
Also used : EndpointName(com.google.cloud.servicedirectory.v1.EndpointName) RegistrationServiceClient(com.google.cloud.servicedirectory.v1.RegistrationServiceClient)

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