diff --git a/ai-platform/snippets/cancel-batch-prediction-job.js b/ai-platform/snippets/cancel-batch-prediction-job.js new file mode 100644 index 0000000000..2cae0419a5 --- /dev/null +++ b/ai-platform/snippets/cancel-batch-prediction-job.js @@ -0,0 +1,62 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(batchPredictionJobId, project, location = 'us-central1') { + // [START aiplatform_cancel_batch_prediction_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function cancelBatchPredictionJob() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`; + const request = { + name, + }; + + // Cancel batch prediction job request + await jobServiceClient.cancelBatchPredictionJob(request); + console.log('Cancel batch prediction job response :'); + } + + cancelBatchPredictionJob(); + // [END aiplatform_cancel_batch_prediction_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/cancel-custom-job.js b/ai-platform/snippets/cancel-custom-job.js new file mode 100644 index 0000000000..b4cbc7eccd --- /dev/null +++ b/ai-platform/snippets/cancel-custom-job.js @@ -0,0 +1,62 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(customJobId, project, location = 'us-central1') { + // [START aiplatform_cancel_custom_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const customJobId = 'YOUR_CUSTOM_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function cancelCustomJob() { + // Configure the name resource + const name = jobServiceClient.customJobPath(project, location, customJobId); + const request = { + name, + }; + + // Cancel custom job request + const [response] = await jobServiceClient.cancelCustomJob(request); + + console.log('Cancel custom job response'); + console.log(`${response}`); + } + cancelCustomJob(); + // [END aiplatform_cancel_custom_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-custom-job.js b/ai-platform/snippets/create-custom-job.js new file mode 100644 index 0000000000..10db425d40 --- /dev/null +++ b/ai-platform/snippets/create-custom-job.js @@ -0,0 +1,87 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + customJobDisplayName, + containerImageUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_custom_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const customJobDisplayName = 'YOUR_CUSTOM_JOB_DISPLAY_NAME'; + // const containerImageUri = 'YOUR_CONTAINER_IMAGE_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createCustomJob() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const customJob = { + displayName: customJobDisplayName, + jobSpec: { + workerPoolSpecs: [ + { + machineSpec: { + machineType: 'n1-standard-4', + acceleratorType: 'NVIDIA_TESLA_K80', + acceleratorCount: 1, + }, + replicaCount: 1, + containerSpec: { + imageUri: containerImageUri, + command: [], + args: [], + }, + }, + ], + }, + }; + const request = {parent, customJob}; + + // Create custom job request + const [response] = await jobServiceClient.createCustomJob(request); + + console.log('Create custom job response'); + console.log(`${JSON.stringify(response)}`); + } + createCustomJob(); + // [END aiplatform_create_custom_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-image.js b/ai-platform/snippets/create-dataset-image.js new file mode 100644 index 0000000000..6a3ee37186 --- /dev/null +++ b/ai-platform/snippets/create-dataset-image.js @@ -0,0 +1,79 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(datasetDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_dataset_image] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetImage() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml', + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset image response'); + console.log(`Name : ${result.name}`); + console.log(`Display name : ${result.displayName}`); + console.log(`Metadata schema uri : ${result.metadataSchemaUri}`); + console.log(`Metadata : ${JSON.stringify(result.metadata)}`); + console.log(`Labels : ${JSON.stringify(result.labels)}`); + } + createDatasetImage(); + // [END aiplatform_create_dataset_image] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-tabular-bigquery.js b/ai-platform/snippets/create-dataset-tabular-bigquery.js new file mode 100644 index 0000000000..74195038e3 --- /dev/null +++ b/ai-platform/snippets/create-dataset-tabular-bigquery.js @@ -0,0 +1,108 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetDisplayName, + bigquerySourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_dataset_tabular_bigquery] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = 'YOUR_DATASET_DISPLAY_NAME'; + // const bigquerySourceUri = 'YOUR_BIGQUERY_SOURCE_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetTabularBigquery() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const metadata = { + structValue: { + fields: { + inputConfig: { + structValue: { + fields: { + bigquerySource: { + structValue: { + fields: { + uri: { + listValue: { + values: [{stringValue: bigquerySourceUri}], + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/tabular_1.0.0.yaml', + metadata: metadata, + }; + const request = { + parent, + dataset, + }; + + // Create dataset request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset tabular bigquery response'); + console.log(`\tName : ${result.name}`); + console.log(`\tDisplay name : ${result.displayName}`); + console.log(`\tMetadata schema uri : ${result.metadataSchemaUri}`); + console.log(`\tMetadata : ${JSON.stringify(result.metadata)}`); + } + createDatasetTabularBigquery(); + // [END aiplatform_create_dataset_tabular_bigquery] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-tabular-gcs.js b/ai-platform/snippets/create-dataset-tabular-gcs.js new file mode 100644 index 0000000000..68f00af378 --- /dev/null +++ b/ai-platform/snippets/create-dataset-tabular-gcs.js @@ -0,0 +1,108 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetDisplayName, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_dataset_tabular_gcs] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = 'YOUR_DATASET_DISPLAY_NAME'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetTabularGcs() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const metadata = { + structValue: { + fields: { + inputConfig: { + structValue: { + fields: { + gcsSource: { + structValue: { + fields: { + uri: { + listValue: { + values: [{stringValue: gcsSourceUri}], + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/tabular_1.0.0.yaml', + metadata: metadata, + }; + const request = { + parent, + dataset, + }; + + // Create dataset request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset tabular gcs response'); + console.log(`\tName : ${result.name}`); + console.log(`\tDisplay name : ${result.displayName}`); + console.log(`\tMetadata schema uri : ${result.metadataSchemaUri}`); + console.log(`\tMetadata : ${JSON.stringify(result.metadata)}`); + } + createDatasetTabularGcs(); + // [END aiplatform_create_dataset_tabular_gcs] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-text.js b/ai-platform/snippets/create-dataset-text.js new file mode 100644 index 0000000000..3a7dcb0f8e --- /dev/null +++ b/ai-platform/snippets/create-dataset-text.js @@ -0,0 +1,79 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(datasetDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_dataset_text] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetText() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/text_1.0.0.yaml', + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset text response'); + console.log(`Name : ${result.name}`); + console.log(`Display name : ${result.displayName}`); + console.log(`Metadata schema uri : ${result.metadataSchemaUri}`); + console.log(`Metadata : ${JSON.stringify(result.metadata)}`); + console.log(`Labels : ${JSON.stringify(result.labels)}`); + } + createDatasetText(); + // [END aiplatform_create_dataset_text] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-video.js b/ai-platform/snippets/create-dataset-video.js new file mode 100644 index 0000000000..0d172127a9 --- /dev/null +++ b/ai-platform/snippets/create-dataset-video.js @@ -0,0 +1,79 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(datasetDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_dataset_video] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetVideo() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/video_1.0.0.yaml', + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset video response'); + console.log(`Name : ${result.name}`); + console.log(`Display name : ${result.displayName}`); + console.log(`Metadata schema uri : ${result.metadataSchemaUri}`); + console.log(`Metadata : ${JSON.stringify(result.metadata)}`); + console.log(`Labels : ${JSON.stringify(result.labels)}`); + } + createDatasetVideo(); + // [END aiplatform_create_dataset_video] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset.js b/ai-platform/snippets/create-dataset.js new file mode 100644 index 0000000000..d124bda46f --- /dev/null +++ b/ai-platform/snippets/create-dataset.js @@ -0,0 +1,89 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetDisplayName, + metadataSchemaUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = 'YOUR_DATASET_DISPLAY_NAME'; + // const metadataSchemaUri = 'YOUR_METADATA_SCHEMA_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDataset() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: metadataSchemaUri, + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [createDatasetResponse] = await response.promise(); + + console.log('Create dataset response'); + console.log(`\tName : ${createDatasetResponse.name}`); + console.log(`\tDisplay name : ${createDatasetResponse.displayName}`); + console.log( + `\tMetadata schema uri : ${createDatasetResponse.metadataSchemaUri}` + ); + console.log( + `\tMetadata : ${JSON.stringify(createDatasetResponse.metadata)}` + ); + console.log(`\tCreate time : ${createDatasetResponse.createTime}`); + console.log(`\tUpdate time : ${createDatasetResponse.updateTime}`); + console.log(`\tLabels : ${JSON.stringify(createDatasetResponse.labels)}`); + } + createDataset(); + // [END aiplatform_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-endpoint.js b/ai-platform/snippets/create-endpoint.js new file mode 100644 index 0000000000..6798a8ceba --- /dev/null +++ b/ai-platform/snippets/create-endpoint.js @@ -0,0 +1,77 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(endpointDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_endpoint] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const endpointDisplayName = 'YOUR_ENDPOINT_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function createEndpoint() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const endpoint = { + displayName: endpointDisplayName, + }; + const request = { + parent, + endpoint, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.createEndpoint(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create endpoint response'); + console.log(`\tName : ${result.name}`); + console.log(`\tDisplay name : ${result.displayName}`); + console.log(`\tDescription : ${result.description}`); + console.log(`\tLabels : ${JSON.stringify(result.labels)}`); + console.log(`\tCreate time : ${JSON.stringify(result.createTime)}`); + console.log(`\tUpdate time : ${JSON.stringify(result.updateTime)}`); + } + createEndpoint(); + // [END aiplatform_create_endpoint] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-image-classification.js b/ai-platform/snippets/create-training-pipeline-image-classification.js index 2f801ed8f7..e832c18069 100644 --- a/ai-platform/snippets/create-training-pipeline-image-classification.js +++ b/ai-platform/snippets/create-training-pipeline-image-classification.js @@ -73,18 +73,15 @@ function main( 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_classification_1.0.0.yaml'; const modelToUpload = {displayName: modelDisplayName}; - const inputDataConfig = {datasetId: datasetId}; + const inputDataConfig = {datasetId}; const trainingPipeline = { displayName: trainingPipelineDisplayName, trainingTaskDefinition, trainingTaskInputs, - inputDataConfig: inputDataConfig, - modelToUpload: modelToUpload, - }; - const request = { - parent, - trainingPipeline, + inputDataConfig, + modelToUpload, }; + const request = {parent, trainingPipeline}; // Create training pipeline request const [response] = await pipelineServiceClient.createTrainingPipeline( diff --git a/ai-platform/snippets/delete-batch-prediction-job.js b/ai-platform/snippets/delete-batch-prediction-job.js new file mode 100644 index 0000000000..da24cd9c57 --- /dev/null +++ b/ai-platform/snippets/delete-batch-prediction-job.js @@ -0,0 +1,62 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(batchPredictionJobId, project, location = 'us-central1') { + // [START aiplatform_delete_batch_prediction_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function deleteBatchPredictionJob() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`; + const request = { + name, + }; + + // Get and print out a list of all the endpoints for this resource + await jobServiceClient.deleteBatchPredictionJob(request); + + console.log('Delete batch prediction job response :'); + } + deleteBatchPredictionJob(); + // [END aiplatform_delete_batch_prediction_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-custom-job.js b/ai-platform/snippets/delete-custom-job.js new file mode 100644 index 0000000000..84c08a542e --- /dev/null +++ b/ai-platform/snippets/delete-custom-job.js @@ -0,0 +1,63 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(customJobId, project, location = 'us-central1') { + // [START aiplatform_delete_custom_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const customJobId = 'YOUR_CUSTOM_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function deleteCustomJob() { + // Configure the name resource + const name = jobServiceClient.customJobPath(project, location, customJobId); + const request = { + name, + }; + + // Delete custom job request + const [response] = await jobServiceClient.deleteCustomJob(request); + + console.log('Delete custom job response'); + console.log(`${response}`); + } + setTimeout(deleteCustomJob, 60000); + // [END aiplatform_delete_custom_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-dataset.js b/ai-platform/snippets/delete-dataset.js new file mode 100644 index 0000000000..e76f0b8ea6 --- /dev/null +++ b/ai-platform/snippets/delete-dataset.js @@ -0,0 +1,66 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(datasetId, project, location = 'us-central1') { + // [START aiplatform_delete_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function deleteDataset() { + // Configure the resource + const name = datasetServiceClient.datasetPath(project, location, datasetId); + const request = {name}; + + // Delete Dataset Request + const [response] = await datasetServiceClient.deleteDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Delete dataset response :'); + console.log(`${result}`); + } + deleteDataset(); + // [END aiplatform_delete_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-endpoint.js b/ai-platform/snippets/delete-endpoint.js new file mode 100644 index 0000000000..08b8cfb27b --- /dev/null +++ b/ai-platform/snippets/delete-endpoint.js @@ -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 + * + * https://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. + */ + +'use strict'; + +async function main(endpointId, project, location = 'us-central1') { + // [START aiplatform_delete_endpoint] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function deleteEndpoint() { + // Configure the parent resource + const endpoint = { + name: `projects/${project}/locations/${location}/endpoints/${endpointId}`, + }; + + // Delete endpoint request + const [response] = await endpointServiceClient.deleteEndpoint(endpoint); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Delete endpoint response :'); + console.log(`${result}`); + } + deleteEndpoint(); + // [END aiplatform_delete_endpoint] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-export-model.js b/ai-platform/snippets/delete-export-model.js new file mode 100644 index 0000000000..d2d91489d3 --- /dev/null +++ b/ai-platform/snippets/delete-export-model.js @@ -0,0 +1,56 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(bucketName, uriPrefix) { + // [START aiplatform_delete_export_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const bucketName = 'YOUR_BUCKET_NAME'; + // const uriPrefix = 'YOUR_GCS_URI_PREFIX' + + // Imports the Google Cloud Storage Client library + const {Storage} = require('@google-cloud/storage'); + + // Instantiates a client + const storageClient = new Storage(); + + async function deleteExportModel() { + const options = { + prefix: uriPrefix, + }; + const [files] = await storageClient + .bucket(`gs://${bucketName}`) + .getFiles(options); + for (const file of files) { + await storageClient.bucket(`gs://${bucketName}`).file(file.name).delete(); + } + console.log('Export model deleted'); + } + deleteExportModel(); + // [END aiplatform_delete_export_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-model.js b/ai-platform/snippets/delete-model.js new file mode 100644 index 0000000000..a0ba4aaf9e --- /dev/null +++ b/ai-platform/snippets/delete-model.js @@ -0,0 +1,65 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, project, location = 'us-central1') { + // [START aiplatform_delete_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function deleteModel() { + // Configure the resource + const name = modelServiceClient.modelPath(project, location, modelId); + const request = {name}; + + // Delete Model Request + const [response] = await modelServiceClient.deleteModel(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Delete model response :'); + console.log(`${result}`); + } + deleteModel(); + // [END aiplatform_delete_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/deploy-model.js b/ai-platform/snippets/deploy-model.js new file mode 100644 index 0000000000..6fab6b2b75 --- /dev/null +++ b/ai-platform/snippets/deploy-model.js @@ -0,0 +1,154 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + modelId, + deployedModelDisplayName, + endpointId, + project, + location = 'us-central1' +) { + // [START aiplatform_deploy_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = "YOUR_MODEL_ID"; + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const deployedModelDisplayName = 'YOUR_DEPLOYED_MODEL_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint: + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function deployModel() { + // Configure the parent resource + // 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 + const trafficSplit = {0: 100}; + const deployedModel = { + // format: 'projects/{project}/locations/{location}/models/{model}' + model: modelName, + displayName: deployedModelDisplayName, + // AutoML Vision models require `automatic_resources` field + // Other model types may require `dedicated_resources` field instead + automaticResources: {minReplicaCount: 1, maxReplicaCount: 1}, + }; + const request = { + endpoint, + deployedModel, + trafficSplit, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.deployModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Deploy model response'); + const modelDeployed = result.deployedModel; + console.log('\tDeployed model'); + if (!modelDeployed) { + console.log('\t\tId : {}'); + console.log('\t\tModel : {}'); + console.log('\t\tDisplay name : {}'); + console.log('\t\tCreate time : {}'); + + console.log('\t\tDedicated resources'); + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMachine spec {}'); + console.log('\t\t\t\tMachine type : {}'); + console.log('\t\t\t\tAccelerator type : {}'); + console.log('\t\t\t\tAccelerator count : {}'); + + console.log('\t\tAutomatic resources'); + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMax replica count : {}'); + } else { + console.log(`\t\tId : ${modelDeployed.id}`); + console.log(`\t\tModel : ${modelDeployed.model}`); + console.log(`\t\tDisplay name : ${modelDeployed.displayName}`); + console.log(`\t\tCreate time : ${modelDeployed.createTime}`); + + const dedicatedResources = modelDeployed.dedicatedResources; + console.log('\t\tDedicated resources'); + if (!dedicatedResources) { + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMachine spec {}'); + console.log('\t\t\t\tMachine type : {}'); + console.log('\t\t\t\tAccelerator type : {}'); + console.log('\t\t\t\tAccelerator count : {}'); + } else { + console.log( + `\t\t\tMin replica count : \ + ${dedicatedResources.minReplicaCount}` + ); + const machineSpec = dedicatedResources.machineSpec; + console.log('\t\t\tMachine spec'); + console.log(`\t\t\t\tMachine type : ${machineSpec.machineType}`); + console.log( + `\t\t\t\tAccelerator type : ${machineSpec.acceleratorType}` + ); + console.log( + `\t\t\t\tAccelerator count : ${machineSpec.acceleratorCount}` + ); + } + + const automaticResources = modelDeployed.automaticResources; + console.log('\t\tAutomatic resources'); + if (!automaticResources) { + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMax replica count : {}'); + } else { + console.log( + `\t\t\tMin replica count : \ + ${automaticResources.minReplicaCount}` + ); + console.log( + `\t\t\tMax replica count : \ + ${automaticResources.maxReplicaCount}` + ); + } + } + } + deployModel(); + // [END aiplatform_deploy_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/expensive-test/create-data-labeling-job-image.test.js b/ai-platform/snippets/expensive-test/create-data-labeling-job-image.test.js new file mode 100644 index 0000000000..c3381e16ef --- /dev/null +++ b/ai-platform/snippets/expensive-test/create-data-labeling-job-image.test.js @@ -0,0 +1,69 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_data_labeling_job_image_test_${uuid()}`; +const datasetId = '1905673553261363200'; +const instructionUri = + 'gs://ucaip-sample-resources/images/datalabeling_instructions.pdf'; +const annotationSpec = 'roses'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let dataLabelingJobId; + +describe('AI platform create data labeling job image', () => { + it('should create a new data labeling job image', async () => { + const stdout = execSync( + `node ./create-data-labeling-job-image.js ${displayName} ${datasetId} \ + ${instructionUri} \ + ${annotationSpec} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create data labeling job image response/); + dataLabelingJobId = stdout + .split('/locations/us-central1/dataLabelingJobs/')[1] + .split('\n')[0]; + }); + after('should cancel the data labeling job and delete it', async () => { + execSync( + `node ./cancel-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/expensive-test/create-data-labeling-job-video.test.js b/ai-platform/snippets/expensive-test/create-data-labeling-job-video.test.js new file mode 100644 index 0000000000..33800c8ea1 --- /dev/null +++ b/ai-platform/snippets/expensive-test/create-data-labeling-job-video.test.js @@ -0,0 +1,70 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_data_labeling_job_video_test_${uuid()}`; +const datasetId = '3459133949727473664'; +const instructionUri = + 'gs://ucaip-sample-resources/images/datalabeling_instructions.pdf'; +const annotationSpec = 'cartwheel'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let dataLabelingJobId; + +describe('AI platform create data labeling job video', () => { + it('should create a new data labeling job video', async () => { + const stdout = execSync( + `node ./create-data-labeling-job-video.js ${displayName} ${datasetId} \ + ${instructionUri} \ + ${annotationSpec} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create data labeling job video response/); + dataLabelingJobId = stdout + .split('/locations/us-central1/dataLabelingJobs/')[1] + .split('\n')[0]; + }); + after('should cancel the data labeling job and delete it', async () => { + execSync( + `node ./cancel-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/expensive-test/create-data-labeling-job.test.js b/ai-platform/snippets/expensive-test/create-data-labeling-job.test.js new file mode 100644 index 0000000000..342c54a554 --- /dev/null +++ b/ai-platform/snippets/expensive-test/create-data-labeling-job.test.js @@ -0,0 +1,73 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_data_labeling_job_test_${uuid()}`; +const datasetId = '8268327440875520000'; +const instructionUri = + 'gs://ucaip-sample-resources/images/datalabeling_instructions.pdf'; +const inputsSchemaUri = + 'gs://google-cloud-aiplatform/schema/datalabelingjob/inputs/image_classification.yaml'; +const annotationSpec = 'daisy'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let dataLabelingJobId; + +describe('AI platform create data labeling job', () => { + it('should create a new data labeling job', async () => { + const stdout = execSync( + `node ./create-data-labeling-job.js ${displayName} ${datasetId} \ + ${instructionUri} \ + ${inputsSchemaUri} \ + ${annotationSpec} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create data labeling job response/); + dataLabelingJobId = stdout + .split('/locations/us-central1/dataLabelingJobs/')[1] + .split('\n')[0]; + }); + after('should cancel the data labeling job and delete it', async () => { + execSync( + `node ./cancel-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/expensive-test/get-training-pipeline.test.js b/ai-platform/snippets/expensive-test/get-training-pipeline.test.js new file mode 100644 index 0000000000..1f553f8438 --- /dev/null +++ b/ai-platform/snippets/expensive-test/get-training-pipeline.test.js @@ -0,0 +1,42 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const trainingPipelineId = '1419759782528548864'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get training pipeline', () => { + it('should get the training pipeline', async () => { + const stdout = execSync( + `node ./get-training-pipeline.js ${trainingPipelineId} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get training pipeline response/); + }); +}); diff --git a/ai-platform/snippets/expensive-test/import-data-text-entity-extraction.test.js b/ai-platform/snippets/expensive-test/import-data-text-entity-extraction.test.js new file mode 100644 index 0000000000..bb6ca67862 --- /dev/null +++ b/ai-platform/snippets/expensive-test/import-data-text-entity-extraction.test.js @@ -0,0 +1,46 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '6203215905493614592'; +const gcsSourceUri = + 'gs://cloud-ml-data/NL-entity/AIPlatform-unified/entity_extraction_dataset.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data text entity extraction', () => { + it('should import data to text entity extraction dataset', async () => { + const stdout = execSync( + `node ./import-data-text-entity-extraction.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data text entity extraction response/); + }); +}); diff --git a/ai-platform/snippets/expensive-test/import-data-text-sentiment-analysis.test.js b/ai-platform/snippets/expensive-test/import-data-text-sentiment-analysis.test.js new file mode 100644 index 0000000000..8449d211f1 --- /dev/null +++ b/ai-platform/snippets/expensive-test/import-data-text-sentiment-analysis.test.js @@ -0,0 +1,46 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '5148529167758786560'; +const gcsSourceUri = + 'gs://cloud-ml-data/NL-sentiment/crowdflower-twitter-claritin-80-10-10.csv'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data text sentiment analysis', () => { + it('should import data text sentiment analysis to dataset', async () => { + const stdout = execSync( + `node ./import-data-text-sentiment-analysis.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data text sentiment analysis response/); + }); +}); diff --git a/ai-platform/snippets/expensive-test/import-data-video-object-tracking.test.js b/ai-platform/snippets/expensive-test/import-data-video-object-tracking.test.js new file mode 100644 index 0000000000..592390e924 --- /dev/null +++ b/ai-platform/snippets/expensive-test/import-data-video-object-tracking.test.js @@ -0,0 +1,46 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '1138566280794603520'; +const gcsSourceUri = + 'gs://ucaip-sample-resources/youtube_8m_videos_animal_full.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data video object tracking', () => { + it('should import video object tracking data to dataset', async () => { + const stdout = execSync( + `node ./import-data-video-object-tracking.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data video object tracking response/); + }); +}); diff --git a/ai-platform/snippets/export-model-tabular-classification.js b/ai-platform/snippets/export-model-tabular-classification.js new file mode 100644 index 0000000000..3a06120f9b --- /dev/null +++ b/ai-platform/snippets/export-model-tabular-classification.js @@ -0,0 +1,80 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + gcsDestinationOutputUriPrefix, + modelId, + project, + location = 'us-central1' +) { + // [START aiplatform_export_model_tabular_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const gcsDestinationOutputUriPrefix ='YOUR_GCS_DESTINATION_\ + // OUTPUT_URI_PREFIX'; eg. "gs:///destination_path" + // const modelId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function exportModelTabularClassification() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}`; + // Configure the outputConfig resources + const outputConfig = { + exportFormatId: 'tf-saved-model', + artifactDestination: { + outputUriPrefix: gcsDestinationOutputUriPrefix, + }, + }; + const request = { + name, + outputConfig, + }; + + // Export Model request + const [response] = await modelServiceClient.exportModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + console.log(`Export model response : ${JSON.stringify(response.result)}`); + } + exportModelTabularClassification(); + // [END aiplatform_export_model_tabular_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/export-model.js b/ai-platform/snippets/export-model.js new file mode 100644 index 0000000000..f3cdba3a69 --- /dev/null +++ b/ai-platform/snippets/export-model.js @@ -0,0 +1,84 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + modelId, + gcsDestinationOutputUriPrefix, + exportFormat, + project, + location = 'us-central1' +) { + // [START aiplatform_export_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const gcsDestinationOutputUriPrefix ='YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const exportFormat = 'YOUR_EXPORT_FORMAT'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function exportModel() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}`; + // Configure the outputConfig resources + const outputConfig = { + exportFormatId: exportFormat, + gcsDestination: { + outputUriPrefix: gcsDestinationOutputUriPrefix, + }, + }; + const request = { + name, + outputConfig, + }; + + // Export Model request + const [response] = await modelServiceClient.exportModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log(`Export model response : ${JSON.stringify(result)}`); + } + exportModel(); + // [END aiplatform_export_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-batch-prediction-job.js b/ai-platform/snippets/get-batch-prediction-job.js new file mode 100644 index 0000000000..0b934e3d64 --- /dev/null +++ b/ai-platform/snippets/get-batch-prediction-job.js @@ -0,0 +1,151 @@ +/* + * 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 + * + * https://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. + */ +'use strict'; + +async function main(batchPredictionJobId, project, location = 'us-central1') { + // [START aiplatform_get_batch_prediction_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function getBatchPredictionJob() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`; + const request = { + name, + }; + + // Get batch prediction request + const [response] = await jobServiceClient.getBatchPredictionJob(request); + + console.log('Get batch prediction job response'); + console.log(`\tName : ${response.name}`); + console.log(`\tDisplayName : ${response.displayName}`); + console.log(`\tModel : ${response.model}`); + console.log(`\tModel parameters : ${response.modelParameters}`); + console.log(`\tGenerate explanation : ${response.generateExplanation}`); + console.log(`\tState : ${response.state}`); + console.log(`\tCreate Time : ${JSON.stringify(response.createTime)}`); + console.log(`\tStart Time : ${JSON.stringify(response.startTime)}`); + console.log(`\tEnd Time : ${JSON.stringify(response.endTime)}`); + console.log(`\tUpdate Time : ${JSON.stringify(response.updateTime)}`); + console.log(`\tLabels : ${JSON.stringify(response.labels)}`); + + const inputConfig = response.inputConfig; + console.log('\tInput config'); + console.log(`\t\tInstances format : ${inputConfig.instancesFormat}`); + + const gcsSource = inputConfig.gcsSource; + console.log('\t\tGcs source'); + console.log(`\t\t\tUris : ${gcsSource.uris}`); + + const bigquerySource = inputConfig.bigquerySource; + console.log('\t\tBigQuery Source'); + if (!bigquerySource) { + console.log('\t\t\tInput Uri : {}'); + } else { + console.log(`\t\t\tInput Uri : ${bigquerySource.inputUri}`); + } + + const outputConfig = response.outputConfig; + console.log('\t\tOutput config'); + console.log(`\t\tPredictions format : ${outputConfig.predictionsFormat}`); + + const gcsDestination = outputConfig.gcsDestination; + console.log('\t\tGcs Destination'); + console.log(`\t\t\tOutput uri prefix : ${gcsDestination.outputUriPrefix}`); + + const bigqueryDestination = outputConfig.bigqueryDestination; + if (!bigqueryDestination) { + console.log('\t\tBigquery Destination'); + console.log('\t\t\tOutput uri : {}'); + } else { + console.log('\t\tBigquery Destination'); + console.log(`\t\t\tOutput uri : ${bigqueryDestination.outputUri}`); + } + + const outputInfo = response.outputInfo; + if (!outputInfo) { + console.log('\tOutput info'); + console.log('\t\tGcs output directory : {}'); + console.log('\t\tBigquery_output_dataset : {}'); + } else { + console.log('\tOutput info'); + console.log( + `\t\tGcs output directory : ${outputInfo.gcsOutputDirectory}` + ); + console.log(`\t\tBigquery_output_dataset : + ${outputInfo.bigqueryOutputDataset}`); + } + + const error = response.error; + console.log('\tError'); + console.log(`\t\tCode : ${error.code}`); + console.log(`\t\tMessage : ${error.message}`); + + const details = error.details; + console.log(`\t\tDetails : ${details}`); + + const partialFailures = response.partialFailures; + console.log('\tPartial failure'); + console.log(partialFailures); + + const resourcesConsumed = response.resourcesConsumed; + console.log('\tResource consumed'); + if (!resourcesConsumed) { + console.log('\t\tReplica Hours: {}'); + } else { + console.log(`\t\tReplica Hours: ${resourcesConsumed.replicaHours}`); + } + + const completionStats = response.completionStats; + console.log('\tCompletion status'); + if (!completionStats) { + console.log('\t\tSuccessful count: {}'); + console.log('\t\tFailed count: {}'); + console.log('\t\tIncomplete count: {}'); + } else { + console.log(`\t\tSuccessful count: ${completionStats.successfulCount}`); + console.log(`\t\tFailed count: ${completionStats.failedCount}`); + console.log(`\t\tIncomplete count: ${completionStats.incompleteCount}`); + } + } + getBatchPredictionJob(); + // [END aiplatform_get_batch_prediction_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-custom-job.js b/ai-platform/snippets/get-custom-job.js new file mode 100644 index 0000000000..717c6bb335 --- /dev/null +++ b/ai-platform/snippets/get-custom-job.js @@ -0,0 +1,62 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(customJobId, project, location = 'us-central1') { + // [START aiplatform_get_custom_job] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const customJobId = 'YOUR_CUSTOM_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function getCustomJob() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/customJobs/${customJobId}`; + const request = { + name, + }; + + // Get custom job request + const [response] = await jobServiceClient.getCustomJob(request); + + console.log('Get custom job response'); + console.log(`\t${JSON.stringify(response)}`); + } + getCustomJob(); + // [END aiplatform_get_custom_job] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-image-classification.js b/ai-platform/snippets/get-model-evaluation-image-classification.js new file mode 100644 index 0000000000..0d966bbc71 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-image-classification.js @@ -0,0 +1,99 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_image_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationImageClassification() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation image classification response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + if (modelExplanation === null) { + console.log(`\tModel explanation: ${JSON.stringify(modelExplanation)}`); + } else { + const meanAttributions = modelExplanation.meanAttributions; + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + getModelEvaluationImageClassification(); + // [END aiplatform_get_model_evaluation_image_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-image-object-detection.js b/ai-platform/snippets/get-model-evaluation-image-object-detection.js new file mode 100644 index 0000000000..a675d9aa12 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-image-object-detection.js @@ -0,0 +1,102 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_image_object_detection] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationImageObjectDetection() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation image object detection response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${meanAttribution.featureAttributions}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationImageObjectDetection(); + // [END aiplatform_get_model_evaluation_image_object_detection] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-slice.js b/ai-platform/snippets/get-model-evaluation-slice.js new file mode 100644 index 0000000000..e80cc66536 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-slice.js @@ -0,0 +1,79 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + modelId, + evaluationId, + sliceId, + project, + location = 'us-central1' +) { + // [START aiplatform_get_model_evaluation_slice] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const sliceId = 'YOUR_SLICE_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + // Specifies the location of the api endpoint + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationSlice() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}/slices/${sliceId}`; + const request = { + name, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await modelServiceClient.getModelEvaluationSlice( + request + ); + + console.log('Get model evaluation slice'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics_Schema_Uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + + console.log('Slice'); + const slice = response.slice; + console.log(`\tDimension :${slice.dimension}`); + console.log(`\tValue :${slice.value}`); + } + getModelEvaluationSlice(); + // [END aiplatform_get_model_evaluation_slice] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-tabular-classification.js b/ai-platform/snippets/get-model-evaluation-tabular-classification.js new file mode 100644 index 0000000000..a593c25241 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-tabular-classification.js @@ -0,0 +1,103 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_tabular_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTabularClassification() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation tabular classification response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTabularClassification(); + // [END aiplatform_get_model_evaluation_tabular_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-tabular-regression.js b/ai-platform/snippets/get-model-evaluation-tabular-regression.js new file mode 100644 index 0000000000..14821fd417 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-tabular-regression.js @@ -0,0 +1,104 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_tabular_regression] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTabularRegression() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation tabular regression response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTabularRegression(); + // [END aiplatform_get_model_evaluation_tabular_regression] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-text-classification.js b/ai-platform/snippets/get-model-evaluation-text-classification.js new file mode 100644 index 0000000000..846fab44b1 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-text-classification.js @@ -0,0 +1,102 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_text_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTextClassification() { + // Configure the resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation text classification response :'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTextClassification(); + // [END aiplatform_get_model_evaluation_text_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-text-entity-extraction.js b/ai-platform/snippets/get-model-evaluation-text-entity-extraction.js new file mode 100644 index 0000000000..fedf92085a --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-text-entity-extraction.js @@ -0,0 +1,102 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_text_entity_extraction] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTextEntityExtraction() { + // Configure the resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation text entity extraction response :'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTextEntityExtraction(); + // [END aiplatform_get_model_evaluation_text_entity_extraction] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-text-sentiment-analysis.js b/ai-platform/snippets/get-model-evaluation-text-sentiment-analysis.js new file mode 100644 index 0000000000..b086ef2d75 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-text-sentiment-analysis.js @@ -0,0 +1,102 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_text_sentiment_analysis] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTextSentimentAnalysis() { + // Configure the resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation text sentiment analysis response :'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTextSentimentAnalysis(); + // [END aiplatform_get_model_evaluation_text_sentiment_analysis] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-video-classification.js b/ai-platform/snippets/get-model-evaluation-video-classification.js new file mode 100644 index 0000000000..a8e9e70690 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-video-classification.js @@ -0,0 +1,68 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_video_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationVideoClassification() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation video classification response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluationVideoClassification(); + // [END aiplatform_get_model_evaluation_video_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-video-object-tracking.js b/ai-platform/snippets/get-model-evaluation-video-object-tracking.js new file mode 100644 index 0000000000..0720241f9c --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-video-object-tracking.js @@ -0,0 +1,68 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_video_object_tracking] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationVideoObjectTracking() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation video object tracking response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluationVideoObjectTracking(); + // [END aiplatform_get_model_evaluation_video_object_tracking] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation.js b/ai-platform/snippets/get-model-evaluation.js new file mode 100644 index 0000000000..44b6c6a50d --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation.js @@ -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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluation() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluation(); + // [END aiplatform_get_model_evaluation] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model.js b/ai-platform/snippets/get-model.js new file mode 100644 index 0000000000..11d9d24406 --- /dev/null +++ b/ai-platform/snippets/get-model.js @@ -0,0 +1,129 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, project, location = 'us-central1') { + // [START aiplatform_get_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModel() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/models/${modelId}`; + const request = { + name, + }; + // Get and print out a list of all the endpoints for this resource + const [response] = await modelServiceClient.getModel(request); + + console.log('Get model response'); + console.log(`\tName : ${response.name}`); + console.log(`\tDisplayName : ${response.displayName}`); + console.log(`\tDescription : ${response.description}`); + console.log(`\tMetadata schema uri : ${response.metadataSchemaUri}`); + console.log(`\tMetadata : ${JSON.stringify(response.metadata)}`); + console.log(`\tTraining pipeline : ${response.trainingPipeline}`); + console.log(`\tArtifact uri : ${response.artifactUri}`); + console.log( + `\tSupported deployment resource types : \ + ${response.supportedDeploymentResourceTypes}` + ); + console.log( + `\tSupported input storage formats : \ + ${response.supportedInputStorageFormats}` + ); + console.log( + `\tSupported output storage formats : \ + ${response.supportedOutputStoragFormats}` + ); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tUpdate time : ${JSON.stringify(response.updateTime)}`); + console.log(`\tLabels : ${JSON.stringify(response.labels)}`); + + const predictSchemata = response.predictSchemata; + console.log('\tPredict schemata'); + console.log(`\tInstance schema uri : ${predictSchemata.instanceSchemaUri}`); + console.log( + `\tParameters schema uri : ${predictSchemata.prametersSchemaUri}` + ); + console.log( + `\tPrediction schema uri : ${predictSchemata.predictionSchemaUri}` + ); + + const [supportedExportFormats] = response.supportedExportFormats; + console.log('\tSupported export formats'); + console.log(`\t${supportedExportFormats}`); + + const containerSpec = response.containerSpec; + console.log('\tContainer Spec'); + if (!containerSpec) { + console.log(`\t\t${JSON.stringify(containerSpec)}`); + console.log('\t\tImage uri : {}'); + console.log('\t\tCommand : {}'); + console.log('\t\tArgs : {}'); + console.log('\t\tPredict route : {}'); + console.log('\t\tHealth route : {}'); + console.log('\t\tEnv'); + console.log('\t\t\t{}'); + console.log('\t\tPort'); + console.log('\t\t{}'); + } else { + console.log(`\t\t${JSON.stringify(containerSpec)}`); + console.log(`\t\tImage uri : ${containerSpec.imageUri}`); + console.log(`\t\tCommand : ${containerSpec.command}`); + console.log(`\t\tArgs : ${containerSpec.args}`); + console.log(`\t\tPredict route : ${containerSpec.predictRoute}`); + console.log(`\t\tHealth route : ${containerSpec.healthRoute}`); + const env = containerSpec.env; + console.log('\t\tEnv'); + console.log(`\t\t\t${JSON.stringify(env)}`); + const ports = containerSpec.ports; + console.log('\t\tPort'); + console.log(`\t\t\t${JSON.stringify(ports)}`); + } + + const [deployedModels] = response.deployedModels; + console.log('\tDeployed models'); + console.log('\t\t', deployedModels); + } + getModel(); + // [END aiplatform_get_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-image-classification.js b/ai-platform/snippets/import-data-image-classification.js new file mode 100644 index 0000000000..c2e27748e2 --- /dev/null +++ b/ai-platform/snippets/import-data-image-classification.js @@ -0,0 +1,82 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_image_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataImageClassification() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/image_classification_single_label_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data image classification response : \ + ${JSON.stringify(importDataResponse)}` + ); + } + importDataImageClassification(); + // [END aiplatform_import_data_image_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-image-object-detection.js b/ai-platform/snippets/import-data-image-object-detection.js new file mode 100644 index 0000000000..c32e7bd856 --- /dev/null +++ b/ai-platform/snippets/import-data-image-object-detection.js @@ -0,0 +1,82 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_image_object_detection] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataImageObjectDetection() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/image_bounding_box_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log( + `Import data image object detection response : \ + ${JSON.stringify(response.result)}` + ); + } + importDataImageObjectDetection(); + // [END aiplatform_import_data_image_object_detection] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-text-classification-single-label.js b/ai-platform/snippets/import-data-text-classification-single-label.js new file mode 100644 index 0000000000..c0c797e2a4 --- /dev/null +++ b/ai-platform/snippets/import-data-text-classification-single-label.js @@ -0,0 +1,82 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_text_classification_single_label] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataTextClassificationSingleLabel() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_classification_single_label_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Import data request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data text classification single label response : \ + ${JSON.stringify(importDataResponse.result)}` + ); + } + importDataTextClassificationSingleLabel(); + // [END aiplatform_import_data_text_classification_single_label] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-text-entity-extraction.js b/ai-platform/snippets/import-data-text-entity-extraction.js new file mode 100644 index 0000000000..8ca3171242 --- /dev/null +++ b/ai-platform/snippets/import-data-text-entity-extraction.js @@ -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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_text_entity_extraction] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataTextEntityExtraction() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_extraction_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Import data request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data text entity extraction response : \ + ${JSON.stringify(importDataResponse.result)}` + ); + } + importDataTextEntityExtraction(); + // [END aiplatform_import_data_text_entity_extraction] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-text-sentiment-analysis.js b/ai-platform/snippets/import-data-text-sentiment-analysis.js new file mode 100644 index 0000000000..7f8dc91a0f --- /dev/null +++ b/ai-platform/snippets/import-data-text-sentiment-analysis.js @@ -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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_text_sentiment_analysis] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataTextSentimentAnalysis() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_sentiment_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Import data request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data text sentiment analysis response : \ + ${JSON.stringify(importDataResponse.result, null, 2)}` + ); + } + importDataTextSentimentAnalysis(); + // [END aiplatform_import_data_text_sentiment_analysis] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-video-classification.js b/ai-platform/snippets/import-data-video-classification.js new file mode 100644 index 0000000000..805ad8b35c --- /dev/null +++ b/ai-platform/snippets/import-data-video-classification.js @@ -0,0 +1,82 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_video_classification] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // eg. 'gs:////[file.csv/file.jsonl]' + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataVideoClassification() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/video_classification_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log( + `Import data video classification response : \ + ${JSON.stringify(response.result)}` + ); + } + importDataVideoClassification(); + // [END aiplatform_import_data_video_classification] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-video-object-tracking.js b/ai-platform/snippets/import-data-video-object-tracking.js new file mode 100644 index 0000000000..7e29b88385 --- /dev/null +++ b/ai-platform/snippets/import-data-video-object-tracking.js @@ -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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_video_object_tracking] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // eg. 'gs:////[file.csv/file.jsonl]' + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataVideoObjectTracking() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/video_object_tracking_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation: ${JSON.stringify(response.name)}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data video object tracking response : \ + ${JSON.stringify(importDataResponse)}` + ); + } + importDataVideoObjectTracking(); + // [END aiplatform_import_data_video_object_tracking] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data.js b/ai-platform/snippets/import-data.js new file mode 100644 index 0000000000..2c5ef33ea5 --- /dev/null +++ b/ai-platform/snippets/import-data.js @@ -0,0 +1,79 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + importSchemaUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const importSchemaUri = "YOUR_IMPORT_SCHEMA_URI"; + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importData() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: importSchemaUri, + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log(`Import data response : ${JSON.stringify(response.result)}`); + } + importData(); + // [END aiplatform_import_data] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-model-evaluation-slices.js b/ai-platform/snippets/list-model-evaluation-slices.js new file mode 100644 index 0000000000..e563c04fa3 --- /dev/null +++ b/ai-platform/snippets/list-model-evaluation-slices.js @@ -0,0 +1,65 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_list_model_evaluation_slices] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function listModelEvaluationSlices() { + // Configure the parent resources + const parent = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + parent, + }; + + // Get and print out a list of all the evaluation slices for this resource + const [response] = await modelServiceClient.listModelEvaluationSlices( + request + ); + console.log('List model evaluation response', response); + console.log(response); + } + listModelEvaluationSlices(); + // [END aiplatform_list_model_evaluation_slices] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/package.json b/ai-platform/snippets/package.json index 8098c74ae5..afdacdc698 100644 --- a/ai-platform/snippets/package.json +++ b/ai-platform/snippets/package.json @@ -13,11 +13,13 @@ "test": "mocha --timeout 800000 test/*.js" }, "dependencies": { - "@google-cloud/aiplatform": "^1.3.0" + "@google-cloud/aiplatform": "^1.3.0", + "@google-cloud/storage": "^5.5.0" }, "devDependencies": { "chai": "^4.2.0", "mocha": "^8.0.0", - "uuid": "^8.3.1" + "uuid": "^8.3.1", + "gts": "^3.0.2" } } diff --git a/ai-platform/snippets/predict-custom-trained-model.js b/ai-platform/snippets/predict-custom-trained-model.js new file mode 100644 index 0000000000..26a053af94 --- /dev/null +++ b/ai-platform/snippets/predict-custom-trained-model.js @@ -0,0 +1,105 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main(filename, endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_custom_trained_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const filename = "YOUR_PREDICTION_FILE_NAME"; + // const endpointId = "YOUR_ENDPOINT_ID"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const util = require('util'); + const {readFile} = require('fs'); + const readFileAsync = util.promisify(readFile); + + // Imports the Google Cloud Prediction Service Client library + const {PredictionServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-prediction-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictCustomTrainedModel() { + // Configure the parent resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + const parameters = { + structValue: { + fields: {}, + }, + }; + const instanceDict = await readFileAsync(filename, 'utf8'); + const instanceValue = JSON.parse(instanceDict); + const instance = { + structValue: { + fields: { + Age: {stringValue: instanceValue['Age']}, + Balance: {stringValue: instanceValue['Balance']}, + Campaign: {stringValue: instanceValue['Campaign']}, + Contact: {stringValue: instanceValue['Contact']}, + Day: {stringValue: instanceValue['Day']}, + Default: {stringValue: instanceValue['Default']}, + Deposit: {stringValue: instanceValue['Deposit']}, + Duration: {stringValue: instanceValue['Duration']}, + Housing: {stringValue: instanceValue['Housing']}, + Job: {stringValue: instanceValue['Job']}, + Loan: {stringValue: instanceValue['Loan']}, + MaritalStatus: {stringValue: instanceValue['MaritalStatus']}, + Month: {stringValue: instanceValue['Month']}, + PDays: {stringValue: instanceValue['PDays']}, + POutcome: {stringValue: instanceValue['POutcome']}, + Previous: {stringValue: instanceValue['Previous']}, + }, + }, + }; + + const instances = [instance]; + const request = { + endpoint, + instances, + parameters, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict custom trained model response'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + const predictions = response.predictions; + console.log('\tPredictions :'); + for (const prediction of predictions) { + console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`); + } + } + predictCustomTrainedModel(); + // [END aiplatform_predict_custom_trained_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/resources/caprese_salad.jpg b/ai-platform/snippets/resources/caprese_salad.jpg index 5800275dd6..7957ca94d9 100644 Binary files a/ai-platform/snippets/resources/caprese_salad.jpg and b/ai-platform/snippets/resources/caprese_salad.jpg differ diff --git a/ai-platform/snippets/test/create-custom-job.test.js b/ai-platform/snippets/test/create-custom-job.test.js new file mode 100644 index 0000000000..f6dfdb062f --- /dev/null +++ b/ai-platform/snippets/test/create-custom-job.test.js @@ -0,0 +1,75 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const customJobDisplayName = `temp_create_custom_job_test${uuid()}`; +const containerImageUri = 'gcr.io/ucaip-test/ucaip-training-test:latest'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +function parseResponse(stdout) { + let res = {}; + for (let i = 0; i < stdout.length; i++) { + if (stdout[i] === '{') { + res = JSON.parse(stdout.substr(i)); + break; + } + } + return res; +} + +let customJobId; + +describe('AI platform create custom job', () => { + it('should create a new custom job', async () => { + const stdout = execSync( + `node ./create-custom-job.js ${customJobDisplayName} \ + ${containerImageUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create custom job response/); + customJobId = parseResponse(stdout).name.split('/').pop(); + }); + + after('should cancel the customJob and delete it', async () => { + execSync( + `node ./cancel-custom-job.js ${customJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-custom-job.js ${customJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-image.test.js b/ai-platform/snippets/test/create-dataset-image.test.js new file mode 100644 index 0000000000..bd9ea481b7 --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-image.test.js @@ -0,0 +1,54 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_image_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset image', () => { + it('should create a new image dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-image.js ${datasetDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset image response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete the created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-tabular-bigquery.test.js b/ai-platform/snippets/test/create-dataset-tabular-bigquery.test.js new file mode 100644 index 0000000000..beb0c2922b --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-tabular-bigquery.test.js @@ -0,0 +1,57 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_tables_bigquery_test_${uuid()}`; +const bigquerySourceUri = + 'bq://prj-ucaip-tutorials.bigquery_dataset.walmart_triptrain_train'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset tabular bigquery', () => { + it('should create a new bigquery tabular dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-tabular-bigquery.js ${datasetDisplayName} \ + ${bigquerySourceUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset tabular bigquery response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('/')[0] + .split('/')[0]; + }); + after('should delete created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-tabular-gcs.test.js b/ai-platform/snippets/test/create-dataset-tabular-gcs.test.js new file mode 100644 index 0000000000..08ab73a8c2 --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-tabular-gcs.test.js @@ -0,0 +1,56 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_tables_gcs_test_${uuid()}`; +const gcsSourceUri = 'gs://cloud-ml-tables-data/bank-marketing.csv'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset tabular gcs', () => { + it('should create a new gcs tabular dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-tabular-gcs.js ${datasetDisplayName} \ + ${gcsSourceUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset tabular gcs response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('/')[0] + .split('/')[0]; + }); + after('should delete created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-text.test.js b/ai-platform/snippets/test/create-dataset-text.test.js new file mode 100644 index 0000000000..43e84656cc --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-text.test.js @@ -0,0 +1,53 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_dataset_text_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset text', () => { + it('should create a new dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-text.js ${displayName} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset text response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete the created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-video.test.js b/ai-platform/snippets/test/create-dataset-video.test.js new file mode 100644 index 0000000000..cae62a2aff --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-video.test.js @@ -0,0 +1,54 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_video_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset video', () => { + it('should create a new video dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-video.js ${datasetDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset video response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete the created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset.test.js b/ai-platform/snippets/test/create-dataset.test.js new file mode 100644 index 0000000000..99075b54db --- /dev/null +++ b/ai-platform/snippets/test/create-dataset.test.js @@ -0,0 +1,56 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_test_${uuid()}`; +const metadataSchemaUri = + 'gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset', () => { + it('should create a new dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset.js ${datasetDisplayName} ${metadataSchemaUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-endpoint.test.js b/ai-platform/snippets/test/create-endpoint.test.js new file mode 100644 index 0000000000..b989d96a8a --- /dev/null +++ b/ai-platform/snippets/test/create-endpoint.test.js @@ -0,0 +1,53 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const endpointDisplayName = `temp_create_endpoint_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; +let endpointId; + +describe('AI platform create endpoint', () => { + it('should create a new endpoint', async () => { + const stdout = execSync( + `node ./create-endpoint.js ${endpointDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create endpoint response/); + endpointId = stdout + .split('/locations/us-central1/endpoints/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('delete created endpoint', async () => { + execSync(`node ./delete-endpoint.js ${endpointId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/deploy-model.test.js b/ai-platform/snippets/test/deploy-model.test.js new file mode 100644 index 0000000000..5f460f037c --- /dev/null +++ b/ai-platform/snippets/test/deploy-model.test.js @@ -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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const endpointDisplayName = `temp_create_endpoint_test_${uuid()}`; + +const modelId = '4190810559500779520'; +const deployedModelDisplayName = `temp_deploy_model_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; +let deployedModelId; +let endpointId; + +describe('AI platform deploy model', () => { + it('should deploy the model in the specified endpoint', async () => { + const endOut = execSync( + `node ./create-endpoint.js ${endpointDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + endpointId = endOut + .split('/locations/us-central1/endpoints/')[1] + .split('\n')[0] + .split('/')[0]; + const stdout = execSync( + `node ./deploy-model.js ${modelId} ${deployedModelDisplayName} \ + ${endpointId} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Deploy model response/); + deployedModelId = stdout.split('Id : ')[1].split('\n')[0]; + }); + + after('should undeploy the deployed model', async () => { + execSync( + `node ./undeploy-model.js ${deployedModelId} ${endpointId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync(`node ./delete-endpoint.js ${endpointId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/export-model-tabular-classification.test.js b/ai-platform/snippets/test/export-model-tabular-classification.test.js new file mode 100644 index 0000000000..c929b4d2d6 --- /dev/null +++ b/ai-platform/snippets/test/export-model-tabular-classification.test.js @@ -0,0 +1,46 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output'; +const modelId = '6036688272397172736'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform export data tabular classification', () => { + it('should export model', async () => { + const stdout = execSync( + `node ./export-model-tabular-classification.js \ + ${gcsDestinationOutputUriPrefix} \ + ${modelId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Export model response/); + }); +}); diff --git a/ai-platform/snippets/test/get-custom-job.test.js b/ai-platform/snippets/test/get-custom-job.test.js new file mode 100644 index 0000000000..f51fa2f9a2 --- /dev/null +++ b/ai-platform/snippets/test/get-custom-job.test.js @@ -0,0 +1,41 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const customJobId = '7980906305281851392'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get custom job', () => { + it('should get the specified custom job', async () => { + const stdout = execSync( + `node ./get-custom-job.js ${customJobId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get custom job response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-slice.test.js b/ai-platform/snippets/test/get-model-evaluation-slice.test.js new file mode 100644 index 0000000000..85033539c3 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-slice.test.js @@ -0,0 +1,45 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '3512561418744365056'; +const evaluationId = '9035588644970168320'; +const sliceId = '6481571820677004173'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get model evaluation slice', () => { + it('should get the evaluation slice from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-slice.js ${modelId} ${evaluationId} \ + ${sliceId} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation slice/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-tabular-classification.test.js b/ai-platform/snippets/test/get-model-evaluation-tabular-classification.test.js new file mode 100644 index 0000000000..b4ea49e905 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-tabular-classification.test.js @@ -0,0 +1,48 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '6036688272397172736'; +const evaluationId = '1866113044163962838'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get tabular classification model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-tabular-classification.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Get model evaluation tabular classification response/ + ); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-tabular-regression.test.js b/ai-platform/snippets/test/get-model-evaluation-tabular-regression.test.js new file mode 100644 index 0000000000..8bed83613c --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-tabular-regression.test.js @@ -0,0 +1,45 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '8842430840248991744'; +const evaluationId = '4944816689650806017'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get tabular regression model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-tabular-regression.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation tabular regression response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-video-classification.test.js b/ai-platform/snippets/test/get-model-evaluation-video-classification.test.js new file mode 100644 index 0000000000..8f0cc4f1ed --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-video-classification.test.js @@ -0,0 +1,45 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '8596984660557299712'; +const evaluationId = '7092045712224944128'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get video classification model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-video-classification.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location} `, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation video classification response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-video-object-tracking.test.js b/ai-platform/snippets/test/get-model-evaluation-video-object-tracking.test.js new file mode 100644 index 0000000000..01e3cdedb9 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-video-object-tracking.test.js @@ -0,0 +1,45 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '8609932509485989888'; +const evaluationId = '6016811301190238208'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get video object tracking model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-video-object-tracking.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location} `, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation video object tracking response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model.test.js b/ai-platform/snippets/test/get-model.test.js new file mode 100644 index 0000000000..f2fd901df8 --- /dev/null +++ b/ai-platform/snippets/test/get-model.test.js @@ -0,0 +1,41 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '3512561418744365056'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get model', () => { + it('should get the specified model', async () => { + const stdout = execSync( + `node ./get-model.js ${modelId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get model response/); + }); +}); diff --git a/ai-platform/snippets/test/import-data-video-classification.test.js b/ai-platform/snippets/test/import-data-video-classification.test.js new file mode 100644 index 0000000000..9c4334f130 --- /dev/null +++ b/ai-platform/snippets/test/import-data-video-classification.test.js @@ -0,0 +1,46 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '3757409464110546944'; +const gcsSourceUri = + 'gs://ucaip-sample-resources/hmdb_split1_5classes_train.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data video classification', () => { + it('should import video classification data to dataset', async () => { + const stdout = execSync( + `node ./import-data-video-classification.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data video classification response/); + }); +}); diff --git a/ai-platform/snippets/test/list-model-evaluation-slices.test.js b/ai-platform/snippets/test/list-model-evaluation-slices.test.js new file mode 100644 index 0000000000..10e0ad3af3 --- /dev/null +++ b/ai-platform/snippets/test/list-model-evaluation-slices.test.js @@ -0,0 +1,46 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '3512561418744365056'; +const evaluationId = '9035588644970168320'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform list model evaluation slices', () => { + it('should list all the evaluation slices from the \ + specified model', async () => { + const stdout = execSync( + `node ./list-model-evaluation-slices.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /List model evaluation response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-image-classification.test.js b/ai-platform/snippets/test/predict-image-classification.test.js index 9711675eec..4947dbd97a 100644 --- a/ai-platform/snippets/test/predict-image-classification.test.js +++ b/ai-platform/snippets/test/predict-image-classification.test.js @@ -22,11 +22,8 @@ const {describe, it} = require('mocha'); const cp = require('child_process'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); - -const filename = 'daisy.jpg'; -const local_file = path.resolve( - path.join(__dirname, `../resources/${filename}`) -); +const cwd = path.join(__dirname, '..'); +const filename = 'resources/daisy.jpg'; const endpointId = '71213169107795968'; const project = process.env.CAIP_PROJECT_ID; const location = 'us-central1'; @@ -35,7 +32,13 @@ describe('AI platform predict image classification', async function () { this.retries(2); it('should make predictions using the image classification model', async () => { const stdout = execSync( - `node ./predict-image-classification.js ${local_file} ${endpointId} ${project} ${location}` + `node ./predict-image-classification.js ${filename} \ + ${endpointId} \ + ${project} \ + ${location}`, + { + cwd, + } ); assert.match(stdout, /Predict image classification response/); }); diff --git a/ai-platform/snippets/test/quickstart.test.js b/ai-platform/snippets/test/quickstart.test.js index 524df65f57..25e3bce29c 100644 --- a/ai-platform/snippets/test/quickstart.test.js +++ b/ai-platform/snippets/test/quickstart.test.js @@ -15,9 +15,9 @@ 'use strict'; -const {describe, it} = require('mocha'); const assert = require('assert'); const cp = require('child_process'); +const {describe, it} = require('mocha'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); diff --git a/ai-platform/snippets/test/upload-model.test.js b/ai-platform/snippets/test/upload-model.test.js new file mode 100644 index 0000000000..b06c3eb1a9 --- /dev/null +++ b/ai-platform/snippets/test/upload-model.test.js @@ -0,0 +1,61 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelDisplayName = `temp_upload_model_test${uuid()}`; +const imageUri = + 'gcr.io/cloud-ml-service-public/cloud-ml-online-prediction-model-server-cpu:v1_15py3cmle_op_images_20200229_0210_RC00'; +const artifactUri = 'gs://ucaip-samples-us-central1/model/explain/'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let modelId; + +describe('AI platform upload model', () => { + it('should upload the specified model', async () => { + const stdout = execSync( + `node ./upload-model.js ${modelDisplayName} \ + ${imageUri} \ + ${artifactUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + console.log(stdout); + assert.match(stdout, /Upload model response/); + modelId = stdout + .split('/locations/us-central1/models/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('delete the model', async () => { + execSync(`node ./delete-model.js ${modelId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/undeploy-model.js b/ai-platform/snippets/undeploy-model.js new file mode 100644 index 0000000000..682d239d7f --- /dev/null +++ b/ai-platform/snippets/undeploy-model.js @@ -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 + * + * https://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. + */ + +'use strict'; + +async function main( + deployedModelId, + endpointId, + project, + location = 'us-central1' +) { + // [START aiplatform_undeploy_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const deployedModelId = "YOUR_MODEL_ID"; + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint: + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function undeployModel() { + // Configure the parent resource + const request = { + deployedModelId, + endpoint, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.undeployModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log('Undeploy model response'); + console.log(response); + } + undeployModel(); + // [END aiplatform_undeploy_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/upload-model.js b/ai-platform/snippets/upload-model.js new file mode 100644 index 0000000000..83c9571780 --- /dev/null +++ b/ai-platform/snippets/upload-model.js @@ -0,0 +1,94 @@ +/* + * 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 + * + * https://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. + */ + +'use strict'; + +async function main( + modelDisplayName, + imageUri, + artifactUri, + project, + location = 'us-central1' +) { + // [START aiplatform_upload_model] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const metadataSchemaUri = 'YOUR_METADATA_SCHEMA_URI'; + // const imageUri = 'YOUR_IMAGE_URI'; + // const artifactUri = 'YOUR_ARTIFACT_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function uploadModel() { + // Configure the parent resources + const parent = `projects/${project}/locations/${location}`; + // Configure the model resources + const model = { + displayName: modelDisplayName, + metadataSchemaUri: '', + artifactUri: artifactUri, + containerSpec: { + imageUri: imageUri, + command: [], + args: [], + env: [], + ports: [], + predictRoute: '', + healthRoute: '', + }, + }; + const request = { + parent, + model, + }; + + console.log('PARENT AND MODEL'); + console.log(parent, model); + // Upload Model request + const [response] = await modelServiceClient.uploadModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Upload model response '); + console.log(`\tModel : ${result.model}`); + } + uploadModel(); + // [END aiplatform_upload_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2));