|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +// [START aiplatform_deploy_model_sample] |
| 20 | + |
| 21 | +import com.google.api.gax.longrunning.OperationFuture; |
| 22 | +import com.google.cloud.aiplatform.v1beta1.AutomaticResources; |
| 23 | +import com.google.cloud.aiplatform.v1beta1.DedicatedResources; |
| 24 | +import com.google.cloud.aiplatform.v1beta1.DeployModelOperationMetadata; |
| 25 | +import com.google.cloud.aiplatform.v1beta1.DeployModelResponse; |
| 26 | +import com.google.cloud.aiplatform.v1beta1.DeployedModel; |
| 27 | +import com.google.cloud.aiplatform.v1beta1.EndpointName; |
| 28 | +import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient; |
| 29 | +import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings; |
| 30 | +import com.google.cloud.aiplatform.v1beta1.MachineSpec; |
| 31 | +import com.google.cloud.aiplatform.v1beta1.ModelName; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.concurrent.ExecutionException; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.TimeoutException; |
| 38 | + |
| 39 | +public class DeployModelSample { |
| 40 | + |
| 41 | + public static void main(String[] args) |
| 42 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 43 | + // TODO(developer): Replace these variables before running the sample. |
| 44 | + String project = "YOUR_PROJECT_ID"; |
| 45 | + String deployedModelDisplayName = "YOUR_DEPLOYED_MODEL_DISPLAY_NAME"; |
| 46 | + String endpointId = "YOUR_ENDPOINT_NAME"; |
| 47 | + String modelId = "YOUR_MODEL_ID"; |
| 48 | + deployModelSample(project, deployedModelDisplayName, endpointId, modelId); |
| 49 | + } |
| 50 | + |
| 51 | + static void deployModelSample( |
| 52 | + String project, String deployedModelDisplayName, String endpointId, String modelId) |
| 53 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 54 | + EndpointServiceSettings endpointServiceSettings = |
| 55 | + EndpointServiceSettings.newBuilder() |
| 56 | + .setEndpoint("us-central1-aiplatform.googleapis.com:443") |
| 57 | + .build(); |
| 58 | + |
| 59 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 60 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 61 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 62 | + try (EndpointServiceClient endpointServiceClient = |
| 63 | + EndpointServiceClient.create(endpointServiceSettings)) { |
| 64 | + String location = "us-central1"; |
| 65 | + EndpointName endpointName = EndpointName.of(project, location, endpointId); |
| 66 | + // key '0' assigns traffic for the newly deployed model |
| 67 | + // Traffic percentage values must add up to 100 |
| 68 | + // Leave dictionary empty if endpoint should not accept any traffic |
| 69 | + Map<String, Integer> trafficSplit = new HashMap<>(); |
| 70 | + trafficSplit.put("0", 100); |
| 71 | + ModelName modelName = ModelName.of(project, location, modelId); |
| 72 | + AutomaticResources automaticResourcesInput = |
| 73 | + AutomaticResources.newBuilder().setMinReplicaCount(1).setMaxReplicaCount(1).build(); |
| 74 | + DeployedModel deployedModelInput = |
| 75 | + DeployedModel.newBuilder() |
| 76 | + .setModel(modelName.toString()) |
| 77 | + .setDisplayName(deployedModelDisplayName) |
| 78 | + .setAutomaticResources(automaticResourcesInput) |
| 79 | + .build(); |
| 80 | + |
| 81 | + OperationFuture<DeployModelResponse, DeployModelOperationMetadata> deployModelResponseFuture = |
| 82 | + endpointServiceClient.deployModelAsync(endpointName, deployedModelInput, trafficSplit); |
| 83 | + System.out.format( |
| 84 | + "Operation name: %s\n", deployModelResponseFuture.getInitialFuture().get().getName()); |
| 85 | + System.out.println("Waiting for operation to finish..."); |
| 86 | + DeployModelResponse deployModelResponse = deployModelResponseFuture.get(20, TimeUnit.MINUTES); |
| 87 | + |
| 88 | + System.out.println("Deploy Model Response"); |
| 89 | + DeployedModel deployedModel = deployModelResponse.getDeployedModel(); |
| 90 | + System.out.println("\tDeployed Model"); |
| 91 | + System.out.format("\t\tid: %s\n", deployedModel.getId()); |
| 92 | + System.out.format("\t\tmodel: %s\n", deployedModel.getModel()); |
| 93 | + System.out.format("\t\tDisplay Name: %s\n", deployedModel.getDisplayName()); |
| 94 | + System.out.format("\t\tCreate Time: %s\n", deployedModel.getCreateTime()); |
| 95 | + |
| 96 | + DedicatedResources dedicatedResources = deployedModel.getDedicatedResources(); |
| 97 | + System.out.println("\t\tDedicated Resources"); |
| 98 | + System.out.format("\t\t\tMin Replica Count: %s\n", dedicatedResources.getMinReplicaCount()); |
| 99 | + |
| 100 | + MachineSpec machineSpec = dedicatedResources.getMachineSpec(); |
| 101 | + System.out.println("\t\t\tMachine Spec"); |
| 102 | + System.out.format("\t\t\t\tMachine Type: %s\n", machineSpec.getMachineType()); |
| 103 | + System.out.format("\t\t\t\tAccelerator Type: %s\n", machineSpec.getAcceleratorType()); |
| 104 | + System.out.format("\t\t\t\tAccelerator Count: %s\n", machineSpec.getAcceleratorCount()); |
| 105 | + |
| 106 | + AutomaticResources automaticResources = deployedModel.getAutomaticResources(); |
| 107 | + System.out.println("\t\tAutomatic Resources"); |
| 108 | + System.out.format("\t\t\tMin Replica Count: %s\n", automaticResources.getMinReplicaCount()); |
| 109 | + System.out.format("\t\t\tMax Replica Count: %s\n", automaticResources.getMaxReplicaCount()); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +// [END aiplatform_deploy_model_sample] |
0 commit comments