From f23ab10e8cf2a3d3e7a973c6e7da0b53d8688b11 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 9 Jan 2020 14:16:39 -0700 Subject: [PATCH] docs: add the base samples for model tasks (#291) * Add the base samples for model tasks * update package.json * lint fix * Update tests and license headers * use spawnsync to get stderr, update license headers Co-authored-by: Benjamin E. Coe --- automl/delete_model.js | 50 +++++++++ automl/deploy_model.js | 53 ++++++++++ automl/get_model.js | 61 +++++++++++ automl/get_model_evaluation.js | 113 +++++++++++++++++++++ automl/list_model_evaluations.js | 112 ++++++++++++++++++++ automl/list_models.js | 57 +++++++++++ automl/test/delete_model.test.js | 45 ++++++++ automl/test/deploy_model.test.js | 41 ++++++++ automl/test/get_model.test.js | 40 ++++++++ automl/test/get_model_evaluation.test.js | 40 ++++++++ automl/test/list_model_evaluations.test.js | 40 ++++++++ automl/test/list_models.test.js | 40 ++++++++ automl/test/undeploy_model.test.js | 41 ++++++++ automl/undeploy_model.js | 53 ++++++++++ 14 files changed, 786 insertions(+) create mode 100644 automl/delete_model.js create mode 100644 automl/deploy_model.js create mode 100644 automl/get_model.js create mode 100644 automl/get_model_evaluation.js create mode 100644 automl/list_model_evaluations.js create mode 100644 automl/list_models.js create mode 100644 automl/test/delete_model.test.js create mode 100644 automl/test/deploy_model.test.js create mode 100644 automl/test/get_model.test.js create mode 100644 automl/test/get_model_evaluation.test.js create mode 100644 automl/test/list_model_evaluations.test.js create mode 100644 automl/test/list_models.test.js create mode 100644 automl/test/undeploy_model.test.js create mode 100644 automl/undeploy_model.js diff --git a/automl/delete_model.js b/automl/delete_model.js new file mode 100644 index 00000000000..805752fe21d --- /dev/null +++ b/automl/delete_model.js @@ -0,0 +1,50 @@ +// 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_delete_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function deleteModel() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + }; + + const [response] = await client.deleteModel(request); + console.log(`Model deleted: ${response}`); + } + + deleteModel(); + // [END automl_delete_model] +} + +main(...process.argv.slice(2)); diff --git a/automl/deploy_model.js b/automl/deploy_model.js new file mode 100644 index 00000000000..49a4c2bb350 --- /dev/null +++ b/automl/deploy_model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_deploy_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function deployModel() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Model deployment finished. ${response}`); + } + + deployModel(); + // [END automl_deploy_model] +} + +main(...process.argv.slice(2)); diff --git a/automl/get_model.js b/automl/get_model.js new file mode 100644 index 00000000000..ee4372d29ec --- /dev/null +++ b/automl/get_model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_get_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function getModel() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + }; + + const [response] = await client.getModel(request); + + console.log(`Model name: ${response.name}`); + console.log( + `Model id: ${ + response.name.split('/')[response.name.split('/').length - 1] + }` + ); + console.log(`Model display name: ${response.displayName}`); + console.log(`Model create time`); + console.log(`\tseconds ${response.createTime.seconds}`); + console.log(`\tnanos ${response.createTime.nanos / 1e9}`); + console.log(`Model deployment state: ${response.deploymentState}`); + } + + getModel(); + // [END automl_get_model] +} + +main(...process.argv.slice(2)); diff --git a/automl/get_model_evaluation.js b/automl/get_model_evaluation.js new file mode 100644 index 00000000000..eb134bd1e94 --- /dev/null +++ b/automl/get_model_evaluation.js @@ -0,0 +1,113 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID', + modelEvaluationId = 'YOUR_MODEL_EVALUATION_ID' +) { + // [START automl_language_entity_extraction_get_model_evaluation] + // [START automl_language_sentiment_analysis_get_model_evaluation] + // [START automl_language_text_classification_get_model_evaluation] + // [START automl_translate_get_model_evaluation] + // [START automl_vision_classification_get_model_evaluation] + // [START automl_vision_object_detection_get_model_evaluation] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const modelEvaluationId = 'YOUR_MODEL_EVALUATION_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function getModelEvaluation() { + // Construct request + const request = { + name: client.modelEvaluationPath( + projectId, + location, + modelId, + modelEvaluationId + ), + }; + + const [response] = await client.getModelEvaluation(request); + + console.log(`Model evaluation name: ${response.name}`); + console.log(`Model annotation spec id: ${response.annotationSpecId}`); + console.log(`Model display name: ${response.displayName}`); + console.log(`Model create time`); + console.log(`\tseconds ${response.createTime.seconds}`); + console.log(`\tnanos ${response.createTime.nanos / 1e9}`); + console.log(`Evaluation example count: ${response.evaluatedExampleCount}`); + // [END automl_language_sentiment_analysis_get_model_evaluation] + // [END automl_language_text_classification_get_model_evaluation] + // [END automl_translate_get_model_evaluation] + // [END automl_vision_classification_get_model_evaluation] + // [END automl_vision_object_detection_get_model_evaluation] + console.log( + `Entity extraction model evaluation metrics: ${response.textExtractionEvaluationMetrics}` + ); + // [END automl_language_entity_extraction_get_model_evaluation] + + // [START automl_language_sentiment_analysis_get_model_evaluation] + console.log( + `Sentiment analysis model evaluation metrics: ${response.textSentimentEvaluationMetrics}` + ); + // [END automl_language_sentiment_analysis_get_model_evaluation] + + // [START automl_language_text_classification_get_model_evaluation] + // [START automl_vision_classification_get_model_evaluation] + console.log( + `Classification model evaluation metrics: ${response.classificationEvaluationMetrics}` + ); + // [END automl_language_text_classification_get_model_evaluation] + // [END automl_vision_classification_get_model_evaluation] + + // [START automl_translate_get_model_evaluation] + console.log( + `Translation model evaluation metrics: ${response.translationEvaluationMetrics}` + ); + // [END automl_translate_get_model_evaluation] + + // [START automl_vision_object_detection_get_model_evaluation] + console.log( + `Object detection model evaluation metrics: ${response.imageObjectDetectionEvaluationMetrics}` + ); + // [START automl_language_entity_extraction_get_model_evaluation] + // [START automl_language_sentiment_analysis_get_model_evaluation] + // [START automl_language_text_classification_get_model_evaluation] + // [START automl_translate_get_model_evaluation] + // [START automl_vision_classification_get_model_evaluation] + } + + getModelEvaluation(); + // [END automl_language_entity_extraction_get_model_evaluation] + // [END automl_language_sentiment_analysis_get_model_evaluation] + // [END automl_language_text_classification_get_model_evaluation] + // [END automl_translate_get_model_evaluation] + // [END automl_vision_classification_get_model_evaluation] + // [END automl_vision_object_detection_get_model_evaluation] +} + +main(...process.argv.slice(2)); diff --git a/automl/list_model_evaluations.js b/automl/list_model_evaluations.js new file mode 100644 index 00000000000..133221265be --- /dev/null +++ b/automl/list_model_evaluations.js @@ -0,0 +1,112 @@ +// 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_language_entity_extraction_list_model_evaluations] + // [START automl_language_sentiment_analysis_list_model_evaluations] + // [START automl_language_text_classification_list_model_evaluations] + // [START automl_translate_list_model_evaluations] + // [START automl_vision_classification_list_model_evaluations] + // [START automl_vision_object_detection_list_model_evaluations] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function listModelEvaluations() { + // Construct request + const request = { + parent: client.modelPath(projectId, location, modelId), + filter: '', + }; + + const [response] = await client.listModelEvaluations(request); + + console.log(`List of model evaluations:`); + for (const evaluation of response) { + console.log(`Model evaluation name: ${evaluation.name}`); + console.log(`Model annotation spec id: ${evaluation.annotationSpecId}`); + console.log(`Model display name: ${evaluation.displayName}`); + console.log(`Model create time`); + console.log(`\tseconds ${evaluation.createTime.seconds}`); + console.log(`\tnanos ${evaluation.createTime.nanos / 1e9}`); + console.log( + `Evaluation example count: ${evaluation.evaluatedExampleCount}` + ); + // [END automl_language_sentiment_analysis_list_model_evaluations] + // [END automl_language_text_classification_list_model_evaluations] + // [END automl_translate_list_model_evaluations] + // [END automl_vision_classification_list_model_evaluations] + // [END automl_vision_object_detection_list_model_evaluations] + console.log( + `Entity extraction model evaluation metrics: ${evaluation.textExtractionEvaluationMetrics}` + ); + // [END automl_language_entity_extraction_list_model_evaluations] + + // [START automl_language_sentiment_analysis_list_model_evaluations] + console.log( + `Sentiment analysis model evaluation metrics: ${evaluation.textSentimentEvaluationMetrics}` + ); + // [END automl_language_sentiment_analysis_list_model_evaluations] + + // [START automl_language_text_classification_list_model_evaluations] + // [START automl_vision_classification_list_model_evaluations] + console.log( + `Classification model evaluation metrics: ${evaluation.classificationEvaluationMetrics}` + ); + // [END automl_language_text_classification_list_model_evaluations] + // [END automl_vision_classification_list_model_evaluations] + + // [START automl_translate_list_model_evaluations] + console.log( + `Translation model evaluation metrics: ${evaluation.translationEvaluationMetrics}` + ); + // [END automl_translate_list_model_evaluations] + + // [START automl_vision_object_detection_list_model_evaluations] + console.log( + `Object detection model evaluation metrics: ${evaluation.imageObjectDetectionEvaluationMetrics}` + ); + // [START automl_language_entity_extraction_list_model_evaluations] + // [START automl_language_sentiment_analysis_list_model_evaluations] + // [START automl_language_text_classification_list_model_evaluations] + // [START automl_translate_list_model_evaluations] + // [START automl_vision_classification_list_model_evaluations] + } + } + + listModelEvaluations(); + // [END automl_language_entity_extraction_list_model_evaluations] + // [END automl_language_sentiment_analysis_list_model_evaluations] + // [END automl_language_text_classification_list_model_evaluations] + // [END automl_translate_list_model_evaluations] + // [END automl_vision_classification_list_model_evaluations] + // [END automl_vision_object_detection_list_model_evaluations] +} + +main(...process.argv.slice(2)); diff --git a/automl/list_models.js b/automl/list_models.js new file mode 100644 index 00000000000..31837f8dde0 --- /dev/null +++ b/automl/list_models.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'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { + // [START automl_list_models] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function listModels() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + filter: 'translation_model_metadata:*', + }; + + const [response] = await client.listModels(request); + + console.log(`List of models:`); + for (const model of response) { + console.log(`Model name: ${model.name}`); + console.log(` + Model id: ${model.name.split('/')[model.name.split('/').length - 1]}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Model create time`); + console.log(`\tseconds ${model.createTime.seconds}`); + console.log(`\tnanos ${model.createTime.nanos / 1e9}`); + console.log(`Model deployment state: ${model.deploymentState}`); + } + } + + listModels(); + // [END automl_list_models] +} + +main(...process.argv.slice(2)); diff --git a/automl/test/delete_model.test.js b/automl/test/delete_model.test.js new file mode 100644 index 00000000000..72fac7f66e5 --- /dev/null +++ b/automl/test/delete_model.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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const DELETE_MODEL_REGION_TAG = 'delete_model.js'; +const LOCATION = 'us-central1'; + +describe('Automl Delete Model Tests', () => { + const client = new AutoMlClient(); + + it('should delete a model', async () => { + // As model creation can take many hours, instead try to delete a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + const projectId = await client.getProjectId(); + const args = [ + DELETE_MODEL_REGION_TAG, + projectId, + LOCATION, + 'TRL0000000000000000000', + ]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /The model does not exist./); + }); +}); diff --git a/automl/test/deploy_model.test.js b/automl/test/deploy_model.test.js new file mode 100644 index 00000000000..561a13a2fe8 --- /dev/null +++ b/automl/test/deploy_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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const DEPLOY_MODEL_REGION_TAG = 'deploy_model.js'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TEN0000000000000000000'; + +describe('Automl Deploy Model Test', () => { + const client = new AutoMlClient(); + + it('should deploy a model', async () => { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + const projectId = await client.getProjectId(); + const args = [DEPLOY_MODEL_REGION_TAG, projectId, LOCATION, MODEL_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /The model does not exist./); + }); +}); diff --git a/automl/test/get_model.test.js b/automl/test/get_model.test.js new file mode 100644 index 00000000000..f014f3fd973 --- /dev/null +++ b/automl/test/get_model.test.js @@ -0,0 +1,40 @@ +// 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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const GET_MODEL_REGION_TAG = 'get_model'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TRL1218052175389786112'; + +describe('Automl Get Model Tests', () => { + const client = new AutoMlClient(); + + it('should get a model', async () => { + const projectId = await client.getProjectId(); + + const get_model_output = execSync( + `node ${GET_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID}` + ); + assert.match(get_model_output, /Model id/); + }); +}); diff --git a/automl/test/get_model_evaluation.test.js b/automl/test/get_model_evaluation.test.js new file mode 100644 index 00000000000..9ea43af9456 --- /dev/null +++ b/automl/test/get_model_evaluation.test.js @@ -0,0 +1,40 @@ +// 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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const GET_MODEL_EVALUATION_REGION_TAG = 'get_model_evaluation'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TRL1218052175389786112'; +const MODEL_EVALUATION_ID = '6800627877826816909'; + +describe('Automl Translate Model Tests', () => { + const client = new AutoMlClient(); + + it('should get model evaluations', async () => { + const projectId = await client.getProjectId(); + const get_model_eval_output = execSync( + `node ${GET_MODEL_EVALUATION_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${MODEL_EVALUATION_ID}` + ); + assert.match(get_model_eval_output, /Model evaluation name/); + }); +}); diff --git a/automl/test/list_model_evaluations.test.js b/automl/test/list_model_evaluations.test.js new file mode 100644 index 00000000000..33aab77574a --- /dev/null +++ b/automl/test/list_model_evaluations.test.js @@ -0,0 +1,40 @@ +// 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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LIST_MODEL_EVALUATION_REGION_TAG = 'list_model_evaluations'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TRL1218052175389786112'; + +describe('Automl List Model Evaluation Tests', () => { + const client = new AutoMlClient(); + + it('should list model evaluations', async () => { + const projectId = await client.getProjectId(); + // list model evaluations + const list_model_eval_output = execSync( + `node ${LIST_MODEL_EVALUATION_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID}` + ); + assert.match(list_model_eval_output, /Model evaluation name/); + }); +}); diff --git a/automl/test/list_models.test.js b/automl/test/list_models.test.js new file mode 100644 index 00000000000..2657a38b945 --- /dev/null +++ b/automl/test/list_models.test.js @@ -0,0 +1,40 @@ +// 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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LIST_MODEL_REGION_TAG = 'list_models'; +const LOCATION = 'us-central1'; + +describe('Automl List Model Tests', () => { + const client = new AutoMlClient(); + + it('should list models', async () => { + const projectId = await client.getProjectId(); + + // list models + const list_model_output = execSync( + `node ${LIST_MODEL_REGION_TAG}.js ${projectId} ${LOCATION}` + ); + assert.match(list_model_output, /Model id:/); + }); +}); diff --git a/automl/test/undeploy_model.test.js b/automl/test/undeploy_model.test.js new file mode 100644 index 00000000000..583a213b670 --- /dev/null +++ b/automl/test/undeploy_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 {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const DEPLOY_MODEL_REGION_TAG = 'undeploy_model.js'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TEN0000000000000000000'; + +describe('Automl Undeploy Model Test', () => { + const client = new AutoMlClient(); + + it('should undeploy a model', async () => { + // As model undeployment can take a long time, instead try to undeploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + const projectId = await client.getProjectId(); + const args = [DEPLOY_MODEL_REGION_TAG, projectId, LOCATION, MODEL_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /The model does not exist./); + }); +}); diff --git a/automl/undeploy_model.js b/automl/undeploy_model.js new file mode 100644 index 00000000000..d50e3d2e4fe --- /dev/null +++ b/automl/undeploy_model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_undeploy_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require(`@google-cloud/automl`).v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function undeployModel() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + }; + + const [operation] = await client.undeployModel(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Model undeployment finished. ${response}`); + } + + undeployModel(); + // [END automl_undeploy_model] +} + +main(...process.argv.slice(2));