Skip to content

Commit

Permalink
samples: initial commit and it is missing env vars for testing (#7)
Browse files Browse the repository at this point in the history
* samples: initial commit  and it is missing env vars for testing

* sampels: fixed dependency issues and lint errors

* added missing dependencies for other samples pom.xml

* updated the copyright links

* fixed wrong version on imports

* fixed compilar error

* removed env vars

* fiexd the lint issuwe

* lowered uppercase of local var name

* corrected the test resources

* fixed the IT tests

* refactored the deploymodel test

* added missing region tag

* fixed env vars issue

* fixed table issues

* fixed delete tests
  • Loading branch information
munkhuushmgl authored Nov 7, 2020
1 parent 18bd403 commit 7f079f7
Show file tree
Hide file tree
Showing 23 changed files with 1,745 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package aiplatform;

// [START aiplatform_create_dataset_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1beta1.CreateDatasetOperationMetadata;
import com.google.cloud.aiplatform.v1beta1.Dataset;
import com.google.cloud.aiplatform.v1beta1.DatasetServiceClient;
import com.google.cloud.aiplatform.v1beta1.DatasetServiceSettings;
import com.google.cloud.aiplatform.v1beta1.LocationName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateDatasetSample {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String datasetDisplayName = "YOUR_DATASET_DISPLAY_NAME";
String metadataSchemaUri = "YOUR_METADATA_SCHEMA_URI";
createDatasetSample(project, datasetDisplayName, metadataSchemaUri);
}

static void createDatasetSample(
String project, String datasetDisplayName, String metadataSchemaUri)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
DatasetServiceSettings datasetServiceSettings =
DatasetServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DatasetServiceClient datasetServiceClient =
DatasetServiceClient.create(datasetServiceSettings)) {
String location = "us-central1";
LocationName locationName = LocationName.of(project, location);
Dataset dataset =
Dataset.newBuilder()
.setDisplayName(datasetDisplayName)
.setMetadataSchemaUri(metadataSchemaUri)
.build();

OperationFuture<Dataset, CreateDatasetOperationMetadata> datasetFuture =
datasetServiceClient.createDatasetAsync(locationName, dataset);
System.out.format("Operation name: %s\n", datasetFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
Dataset datasetResponse = datasetFuture.get(300, TimeUnit.SECONDS);

System.out.println("Create Dataset Response");
System.out.format("Name: %s\n", datasetResponse.getName());
System.out.format("Display Name: %s\n", datasetResponse.getDisplayName());
System.out.format("Metadata Schema Uri: %s\n", datasetResponse.getMetadataSchemaUri());
System.out.format("Metadata: %s\n", datasetResponse.getMetadata());
System.out.format("Create Time: %s\n", datasetResponse.getCreateTime());
System.out.format("Update Time: %s\n", datasetResponse.getUpdateTime());
System.out.format("Labels: %s\n", datasetResponse.getLabelsMap());
}
}
}
// [END aiplatform_create_dataset_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package aiplatform;

// [START aiplatform_create_endpoint_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1beta1.CreateEndpointOperationMetadata;
import com.google.cloud.aiplatform.v1beta1.Endpoint;
import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient;
import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings;
import com.google.cloud.aiplatform.v1beta1.LocationName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateEndpointSample {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String endpointDisplayName = "YOUR_ENDPOINT_DISPLAY_NAME";
createEndpointSample(project, endpointDisplayName);
}

static void createEndpointSample(String project, String endpointDisplayName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
EndpointServiceSettings endpointServiceSettings =
EndpointServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (EndpointServiceClient endpointServiceClient =
EndpointServiceClient.create(endpointServiceSettings)) {
String location = "us-central1";
LocationName locationName = LocationName.of(project, location);
Endpoint endpoint = Endpoint.newBuilder().setDisplayName(endpointDisplayName).build();

OperationFuture<Endpoint, CreateEndpointOperationMetadata> endpointFuture =
endpointServiceClient.createEndpointAsync(locationName, endpoint);
System.out.format("Operation name: %s\n", endpointFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
Endpoint endpointResponse = endpointFuture.get(300, TimeUnit.SECONDS);

System.out.println("Create Endpoint Response");
System.out.format("Name: %s\n", endpointResponse.getName());
System.out.format("Display Name: %s\n", endpointResponse.getDisplayName());
System.out.format("Description: %s\n", endpointResponse.getDescription());
System.out.format("Labels: %s\n", endpointResponse.getLabelsMap());
System.out.format("Create Time: %s\n", endpointResponse.getCreateTime());
System.out.format("Update Time: %s\n", endpointResponse.getUpdateTime());
}
}
}
// [END aiplatform_create_endpoint_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package aiplatform;

// [START aiplatform_delete_endpoint_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1beta1.DeleteOperationMetadata;
import com.google.cloud.aiplatform.v1beta1.EndpointName;
import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient;
import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteEndpointSample {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String endpointId = "YOUR_ENDPOINT_ID";
deleteEndpointSample(project, endpointId);
}

static void deleteEndpointSample(String project, String endpointId)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
EndpointServiceSettings endpointServiceSettings =
EndpointServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// 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);

OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
endpointServiceClient.deleteEndpointAsync(endpointName);
System.out.format("Operation name: %s\n", operationFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
Empty deleteResponse = operationFuture.get(300, TimeUnit.SECONDS);

System.out.format("Delete Endpoint Response: %s\n", deleteResponse);
}
}
}
// [END aiplatform_delete_endpoint_sample]
113 changes: 113 additions & 0 deletions aiplatform/snippets/src/main/java/aiplatform/DeployModelSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package aiplatform;

// [START aiplatform_deploy_model_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1beta1.AutomaticResources;
import com.google.cloud.aiplatform.v1beta1.DedicatedResources;
import com.google.cloud.aiplatform.v1beta1.DeployModelOperationMetadata;
import com.google.cloud.aiplatform.v1beta1.DeployModelResponse;
import com.google.cloud.aiplatform.v1beta1.DeployedModel;
import com.google.cloud.aiplatform.v1beta1.EndpointName;
import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient;
import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings;
import com.google.cloud.aiplatform.v1beta1.MachineSpec;
import com.google.cloud.aiplatform.v1beta1.ModelName;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeployModelSample {

public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String deployedModelDisplayName = "YOUR_DEPLOYED_MODEL_DISPLAY_NAME";
String endpointId = "YOUR_ENDPOINT_NAME";
String modelId = "YOUR_MODEL_ID";
deployModelSample(project, deployedModelDisplayName, endpointId, modelId);
}

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();

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// 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());
}
}
}
// [END aiplatform_deploy_model_sample]
Loading

0 comments on commit 7f079f7

Please sign in to comment.