diff --git a/.github/workflows/automl.yaml b/.github/workflows/automl.yaml new file mode 100644 index 0000000000..d5fe8c8e25 --- /dev/null +++ b/.github/workflows/automl.yaml @@ -0,0 +1,74 @@ +name: automl +on: + push: + branches: + - main + paths: + - 'automl/**' + pull_request: + paths: + - 'automl/**' + pull_request_target: + types: [labeled] + schedule: + - cron: '0 0 * * 0' +jobs: + test: + if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: 'write' + pull-requests: 'write' + id-token: 'write' + steps: + - uses: actions/checkout@v3 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - uses: google-github-actions/auth@v1.0.0 + with: + workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider' + service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com' + create_credentials_file: 'true' + credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}' + access_token_lifetime: 600s + - id: secrets + uses: 'google-github-actions/get-secretmanager-secrets@v0' + with: + secrets: |- + table_model_id:nodejs-docs-samples-tests/nodejs-docs-samples-table-model-id + - uses: actions/setup-node@v3 + with: + node-version: 14 + - run: npm install + working-directory: automl + - run: npm test + working-directory: automl + env: + MOCHA_REPORTER_SUITENAME: automl + MOCHA_REPORTER_OUTPUT: automl_sponge_log.xml + MOCHA_REPORTER: xunit + TABLE_MODEL_ID: ${{ steps.secrets.outputs.table_model_id }} + - if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + try { + await github.rest.issues.removeLabel({ + name: 'actions:force-run', + owner: 'GoogleCloudPlatform', + repo: 'nodejs-docs-samples', + issue_number: context.payload.pull_request.number + }); + } catch (e) { + if (!e.message.includes('Label does not exist')) { + throw e; + } + } + - if: ${{ github.event_name == 'schedule' && always() }} + run: | + curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L + chmod +x ./flakybot + ./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/.github/workflows/workflows.json b/.github/workflows/workflows.json index 14f526c4c5..eb1ec006e9 100644 --- a/.github/workflows/workflows.json +++ b/.github/workflows/workflows.json @@ -14,6 +14,7 @@ "appengine/storage/flexible", "appengine/storage/standard", "auth", + "automl", "appengine/typescript", "appengine/websockets", "appengine/twilio", diff --git a/automl/.eslintrc.yml b/automl/.eslintrc.yml new file mode 100644 index 0000000000..282535f55f --- /dev/null +++ b/automl/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + no-console: off diff --git a/automl/batch_predict.js b/automl/batch_predict.js new file mode 100644 index 0000000000..e39c2299ba --- /dev/null +++ b/automl/batch_predict.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID', + inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl', + outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/' +) { + // [START automl_batch_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl'; + // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/'; + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new PredictionServiceClient(); + + async function batchPredict() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + inputConfig: { + gcsSource: { + inputUris: [inputUri], + }, + }, + outputConfig: { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }, + }; + + const [operation] = await client.batchPredict(request); + + console.log('Waiting for operation to complete...'); + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log( + `Batch Prediction results saved to Cloud Storage bucket. ${response}` + ); + } + + batchPredict(); + // [END automl_batch_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/batch_predict.js b/automl/beta/batch_predict.js new file mode 100644 index 0000000000..832ee7c63d --- /dev/null +++ b/automl/beta/batch_predict.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID', + inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl', + outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/' +) { + // [START automl_batch_predict_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl'; + // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/'; + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new PredictionServiceClient(); + + async function batchPredict() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + inputConfig: { + gcsSource: { + inputUris: [inputUri], + }, + }, + outputConfig: { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }, + }; + + const [operation] = await client.batchPredict(request); + + console.log('Waiting for operation to complete...'); + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log( + `Batch Prediction results saved to Cloud Storage bucket. ${response}` + ); + } + + batchPredict(); + // [END automl_batch_predict_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/cancel_operation.js b/automl/beta/cancel_operation.js new file mode 100644 index 0000000000..b6066a3b7d --- /dev/null +++ b/automl/beta/cancel_operation.js @@ -0,0 +1,51 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +async function main(operationFullId) { + // [START automl_cancel_operation_beta] + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const operationFullId = 'projects/YOUR_PROJECT_ID/locations/YOUR_LOCATIOIN/operations/OPERATION_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function cancelOperation() { + client.operationsClient.cancelOperation({ + name: operationFullId, + }); + + // Wait for operation to complete. + console.log('Cancelled operation'); + } + + cancelOperation(); + // [END automl_cancel_operation_beta] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/beta/delete-dataset.js b/automl/beta/delete-dataset.js new file mode 100644 index 0000000000..4aae984f38 --- /dev/null +++ b/automl/beta/delete-dataset.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_delete_dataset_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const datasetId = 'YOUR_DATASET_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function deleteDataset() { + // Construct request + const request = { + name: client.datasetPath(projectId, location, datasetId), + }; + + const [operation] = await client.deleteDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Dataset deleted: ${response}`); + } + + deleteDataset(); + // [END automl_delete_dataset_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/delete-model.js b/automl/beta/delete-model.js new file mode 100644 index 0000000000..0a3d61c420 --- /dev/null +++ b/automl/beta/delete-model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_delete_model_beta] + /** + * 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').v1beta1; + + // 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_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/get-model-evaluation.js b/automl/beta/get-model-evaluation.js new file mode 100644 index 0000000000..8ab4dac4c5 --- /dev/null +++ b/automl/beta/get-model-evaluation.js @@ -0,0 +1,85 @@ +// 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_video_classification_get_model_evaluation_beta] + // [START automl_video_object_tracking_get_model_evaluation_beta] + /** + * 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').v1beta1; + + // 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_video_object_tracking_get_model_evaluation_beta] + // [END automl_video_classification_get_model_evaluation_beta] + + // [START automl_video_classification_get_model_evaluation_beta] + console.log( + `Video classification model evaluation metrics: ${response.videoClassificationEvaluationMetrics}` + ); + // [END automl_video_classification_get_model_evaluation_beta] + + // [START automl_video_object_tracking_get_model_evaluation_beta] + console.log( + `Video object tracking model evaluation metrics: ${response.videoObjectTrackingEvaluationMetrics}` + ); + + // [START automl_video_classification_get_model_evaluation_beta] + } + + getModelEvaluation(); + // [END automl_video_classification_get_model_evaluation_beta] + // [END automl_video_object_tracking_get_model_evaluation_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/get-model.js b/automl/beta/get-model.js new file mode 100644 index 0000000000..16583398da --- /dev/null +++ b/automl/beta/get-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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_get_model_beta] + /** + * 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').v1beta1; + + // 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_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/get-operation-status.js b/automl/beta/get-operation-status.js new file mode 100644 index 0000000000..d11ec2506c --- /dev/null +++ b/automl/beta/get-operation-status.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', + operationId = 'YOUR_OPERATION_ID' +) { + // [START automl_get_operation_status_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const operationId = 'YOUR_OPERATION_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function getOperationStatus() { + // Construct request + const request = { + name: `projects/${projectId}/locations/${location}/operations/${operationId}`, + }; + + const [response] = await client.operationsClient.getOperation(request); + + console.log(`Name: ${response.name}`); + console.log('Operation details:'); + console.log(response); + } + + getOperationStatus(); + // [END automl_get_operation_status_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/import-dataset.js b/automl/beta/import-dataset.js new file mode 100644 index 0000000000..37d7651214 --- /dev/null +++ b/automl/beta/import-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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DISPLAY_ID', + path = 'gs://BUCKET_ID/path_to_training_data.csv' +) { + // [START automl_import_dataset_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const datasetId = 'YOUR_DISPLAY_ID'; + // const path = 'gs://BUCKET_ID/path_to_training_data.csv'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function importDataset() { + // Construct request + const request = { + name: client.datasetPath(projectId, location, datasetId), + inputConfig: { + gcsSource: { + inputUris: path.split(','), + }, + }, + }; + + // Import dataset + console.log('Proccessing import'); + const [operation] = await client.importData(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Dataset imported: ${response}`); + } + + importDataset(); + // [END automl_import_dataset_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/list-datasets.js b/automl/beta/list-datasets.js new file mode 100644 index 0000000000..4668638102 --- /dev/null +++ b/automl/beta/list-datasets.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { + // [START automl_video_classification_list_datasets_beta] + // [START automl_video_object_tracking_list_datasets_beta] + /** + * 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').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function listDatasets() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + filter: 'translation_dataset_metadata:*', + }; + + const [response] = await client.listDatasets(request); + + console.log('List of datasets:'); + for (const dataset of response) { + console.log(`Dataset name: ${dataset.name}`); + console.log( + `Dataset id: ${ + dataset.name.split('/')[dataset.name.split('/').length - 1] + }` + ); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log('Dataset create time'); + console.log(`\tseconds ${dataset.createTime.seconds}`); + console.log(`\tnanos ${dataset.createTime.nanos / 1e9}`); + + // [END automl_video_object_tracking_list_datasets_beta] + console.log( + `Video classification dataset metadata: ${dataset.videoClassificationDatasetMetadata}` + ); + // [END automl_video_classification_list_datasets_beta] + + // [START automl_video_object_tracking_list_datasets_beta] + console.log( + `Video object tracking dataset metadata: ${dataset.videoObjectTrackingDatasetMetadata}` + ); + // [START automl_video_classification_list_datasets_beta] + } + } + + listDatasets(); + // [END automl_video_classification_list_datasets_beta] + // [END automl_video_object_tracking_list_datasets_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/list-models.js b/automl/beta/list-models.js new file mode 100644 index 0000000000..9f392acdfa --- /dev/null +++ b/automl/beta/list-models.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') { + // [START automl_list_models_beta] + /** + * 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').v1beta1; + + // 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_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/setEndpoint.js b/automl/beta/setEndpoint.js new file mode 100644 index 0000000000..5c45768d41 --- /dev/null +++ b/automl/beta/setEndpoint.js @@ -0,0 +1,46 @@ +// Copyright 2019 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 setEndpoint(projectId) { + // [START automl_set_endpoint] + const automl = require('@google-cloud/automl').v1beta1; + + // You must first create a dataset, using the `eu` endpoint, before you can + // call other operations such as: list, get, import, delete, etc. + const clientOptions = {apiEndpoint: 'eu-automl.googleapis.com'}; + + // Instantiates a client + const client = new automl.AutoMlClient(clientOptions); + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, 'eu'); + // [END automl_set_endpoint] + + // Grabs the list of datasets in a given project location. + // Note: create a dataset in `eu` before calling `listDatasets`. + const responses = await client.listDatasets({parent: projectLocation}); + + // Prints out each dataset. + const datasets = responses[0]; + datasets.forEach(dataset => { + console.log(dataset); + }); +} + +setEndpoint(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/automl/beta/video-classification-create-dataset.js b/automl/beta/video-classification-create-dataset.js new file mode 100644 index 0000000000..d345c8209c --- /dev/null +++ b/automl/beta/video-classification-create-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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_video_classification_create_dataset_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + videoClassificationDatasetMetadata: {}, + }, + }; + + // Create dataset + const [response] = await client.createDataset(request); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_video_classification_create_dataset_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/video-classification-create-model.js b/automl/beta/video-classification-create-model.js new file mode 100644 index 0000000000..ca7c1eb04c --- /dev/null +++ b/automl/beta/video-classification-create-model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_video_classification_create_model_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + videoClassificationModelMetadata: {}, + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_video_classification_create_model_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/video-object-tracking-create-dataset.js b/automl/beta/video-object-tracking-create-dataset.js new file mode 100644 index 0000000000..561e65fe77 --- /dev/null +++ b/automl/beta/video-object-tracking-create-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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_video_object_tracking_create_dataset_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + videoObjectTrackingDatasetMetadata: {}, + }, + }; + + // Create dataset + const [response] = await client.createDataset(request); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_video_object_tracking_create_dataset_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/beta/video-object-tracking-create-model.js b/automl/beta/video-object-tracking-create-model.js new file mode 100644 index 0000000000..7a93864d73 --- /dev/null +++ b/automl/beta/video-object-tracking-create-model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_video_object_tracking_create_model_beta] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + videoObjectTrackingModelMetadata: {}, + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_video_object_tracking_create_model_beta] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/delete_dataset.js b/automl/delete_dataset.js new file mode 100644 index 0000000000..ef882e524a --- /dev/null +++ b/automl/delete_dataset.js @@ -0,0 +1,57 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_delete_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const datasetId = 'YOUR_DATASET_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function deleteDataset() { + // Construct request + const request = { + name: client.datasetPath(projectId, location, datasetId), + }; + + const [operation] = await client.deleteDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Dataset deleted: ${response}`); + } + + deleteDataset(); + // [END automl_delete_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/delete_model.js b/automl/delete_model.js new file mode 100644 index 0000000000..686d662190 --- /dev/null +++ b/automl/delete_model.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'; + +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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/deploy_model.js b/automl/deploy_model.js new file mode 100644 index 0000000000..b51c43f5f5 --- /dev/null +++ b/automl/deploy_model.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', + 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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/export_dataset.js b/automl/export_dataset.js new file mode 100644 index 0000000000..89f8710d28 --- /dev/null +++ b/automl/export_dataset.js @@ -0,0 +1,63 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + gcsUri = 'gs://BUCKET_ID/path_to_export/' +) { + // [START automl_export_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const datasetId = 'YOUR_DATASET_ID'; + // const gcsUri = 'gs://BUCKET_ID/path_to_export/'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function exportDataset() { + // Construct request + const request = { + name: client.datasetPath(projectId, location, datasetId), + outputConfig: { + gcsDestination: { + outputUriPrefix: gcsUri, + }, + }, + }; + + const [operation] = await client.exportData(request); + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Dataset exported: ${response}`); + } + + exportDataset(); + // [END automl_export_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/get_dataset.js b/automl/get_dataset.js new file mode 100644 index 0000000000..8cee268630 --- /dev/null +++ b/automl/get_dataset.js @@ -0,0 +1,101 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_vision_classification_get_dataset] + // [START automl_vision_object_detection_get_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const datasetId = 'YOUR_DATASET_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function getDataset() { + // Construct request + const request = { + name: client.datasetPath(projectId, location, datasetId), + }; + + const [response] = await client.getDataset(request); + + console.log(`Dataset name: ${response.name}`); + console.log( + `Dataset id: ${ + response.name.split('/')[response.name.split('/').length - 1] + }` + ); + console.log(`Dataset display name: ${response.displayName}`); + console.log('Dataset create time'); + console.log(`\tseconds ${response.createTime.seconds}`); + console.log(`\tnanos ${response.createTime.nanos / 1e9}`); + // [END automl_vision_classification_get_dataset] + // [END automl_vision_object_detection_get_dataset] + console.log( + `Text extraction dataset metadata: ${response.textExtractionDatasetMetadata}` + ); + + console.log( + `Text sentiment dataset metadata: ${response.textSentimentDatasetMetadata}` + ); + + console.log( + `Text classification dataset metadata: ${response.textClassificationDatasetMetadata}` + ); + + if (response.translationDatasetMetadata !== undefined) { + console.log('Translation dataset metadata:'); + console.log( + `\tSource language code: ${response.translationDatasetMetadata.sourceLanguageCode}` + ); + console.log( + `\tTarget language code: ${response.translationDatasetMetadata.targetLanguageCode}` + ); + } + + // [START automl_vision_classification_get_dataset] + console.log( + `Image classification dataset metadata: ${response.imageClassificationDatasetMetadata}` + ); + // [END automl_vision_classification_get_dataset] + + // [START automl_vision_object_detection_get_dataset] + console.log( + `Image object detection dataset metatdata: ${response.imageObjectDetectionDatasetMetatdata}` + ); + // [START automl_vision_classification_get_dataset] + } + + getDataset(); + // [END automl_vision_classification_get_dataset] + // [END automl_vision_object_detection_get_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/get_model.js b/automl/get_model.js new file mode 100644 index 0000000000..1ebc265c09 --- /dev/null +++ b/automl/get_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'; + +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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/get_model_evaluation.js b/automl/get_model_evaluation.js new file mode 100644 index 0000000000..724c6cf224 --- /dev/null +++ b/automl/get_model_evaluation.js @@ -0,0 +1,117 @@ +// 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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/get_operation_status.js b/automl/get_operation_status.js new file mode 100644 index 0000000000..a503550e90 --- /dev/null +++ b/automl/get_operation_status.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', + operationId = 'YOUR_OPERATION_ID' +) { + // [START automl_get_operation_status] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const operationId = 'YOUR_OPERATION_ID'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function getOperationStatus() { + // Construct request + const request = { + name: `projects/${projectId}/locations/${location}/operations/${operationId}`, + }; + + const [response] = await client.operationsClient.getOperation(request); + + console.log(`Name: ${response.name}`); + console.log('Operation details:'); + console.log(`${response}`); + } + + getOperationStatus(); + // [END automl_get_operation_status] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/import_dataset.js b/automl/import_dataset.js new file mode 100644 index 0000000000..72815f68b7 --- /dev/null +++ b/automl/import_dataset.js @@ -0,0 +1,66 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DISPLAY_ID', + path = 'gs://BUCKET_ID/path_to_training_data.csv' +) { + // [START automl_import_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const datasetId = 'YOUR_DISPLAY_ID'; + // const path = 'gs://BUCKET_ID/path_to_training_data.csv'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function importDataset() { + // Construct request + const request = { + name: client.datasetPath(projectId, location, datasetId), + inputConfig: { + gcsSource: { + inputUris: path.split(','), + }, + }, + }; + + // Import dataset + console.log('Proccessing import'); + const [operation] = await client.importData(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Dataset imported: ${response}`); + } + + importDataset(); + // [END automl_import_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_entity_extraction_create_dataset.js b/automl/language_entity_extraction_create_dataset.js new file mode 100644 index 0000000000..ba31bee259 --- /dev/null +++ b/automl/language_entity_extraction_create_dataset.js @@ -0,0 +1,69 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_language_entity_extraction_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + textExtractionDatasetMetadata: {}, + }, + }; + + // Create dataset + const [operation] = await client.createDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_language_entity_extraction_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_entity_extraction_create_model.js b/automl/language_entity_extraction_create_model.js new file mode 100644 index 0000000000..53b4dea68d --- /dev/null +++ b/automl/language_entity_extraction_create_model.js @@ -0,0 +1,63 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_language_entity_extraction_create_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + textExtractionModelMetadata: {}, // Leave unset, to use the default base model + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_language_entity_extraction_create_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_entity_extraction_predict.js b/automl/language_entity_extraction_predict.js new file mode 100644 index 0000000000..f98b8a2010 --- /dev/null +++ b/automl/language_entity_extraction_predict.js @@ -0,0 +1,72 @@ +// Copyright 2019 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', + content = 'text to predict' +) { + // [START automl_language_entity_extraction_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const content = 'text to predict' + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new PredictionServiceClient(); + + async function predict() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + payload: { + textSnippet: { + content: content, + mimeType: 'text/plain', // Types: 'test/plain', 'text/html' + }, + }, + }; + + const [response] = await client.predict(request); + + for (const annotationPayload of response.payload) { + console.log( + `Text Extract Entity Types: ${annotationPayload.displayName}` + ); + console.log(`Text Score: ${annotationPayload.textExtraction.score}`); + const textSegment = annotationPayload.textExtraction.textSegment; + console.log(`Text Extract Entity Content: ${textSegment.content}`); + console.log(`Text Start Offset: ${textSegment.startOffset}`); + console.log(`Text End Offset: ${textSegment.endOffset}`); + } + } + + predict(); + // [END automl_language_entity_extraction_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_sentiment_analysis_create_dataset.js b/automl/language_sentiment_analysis_create_dataset.js new file mode 100644 index 0000000000..8f406b5c71 --- /dev/null +++ b/automl/language_sentiment_analysis_create_dataset.js @@ -0,0 +1,71 @@ +// Copyright 2019 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', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_language_sentiment_analysis_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + textSentimentDatasetMetadata: { + sentimentMax: 4, // Possible max sentiment score: 1-10 + }, + }, + }; + + // Create dataset + const [operation] = await client.createDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_language_sentiment_analysis_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_sentiment_analysis_create_model.js b/automl/language_sentiment_analysis_create_model.js new file mode 100644 index 0000000000..76f559ab0c --- /dev/null +++ b/automl/language_sentiment_analysis_create_model.js @@ -0,0 +1,63 @@ +// Copyright 2019 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', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_language_sentiment_analysis_create_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + textSentimentModelMetadata: {}, // Leave unset, to use the default base model + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_language_sentiment_analysis_create_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_sentiment_analysis_predict.js b/automl/language_sentiment_analysis_predict.js new file mode 100644 index 0000000000..44d8a20959 --- /dev/null +++ b/automl/language_sentiment_analysis_predict.js @@ -0,0 +1,68 @@ +// Copyright 2019 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', + content = 'text to predict' +) { + // [START automl_language_sentiment_analysis_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const content = 'text to predict' + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new PredictionServiceClient(); + + async function predict() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + payload: { + textSnippet: { + content: content, + mimeType: 'text/plain', // Types: 'test/plain', 'text/html' + }, + }, + }; + + const [response] = await client.predict(request); + + for (const annotationPayload of response.payload) { + console.log(`Predicted class name: ${annotationPayload.displayName}`); + console.log( + `Predicted sentiment score: ${annotationPayload.textSentiment.sentiment}` + ); + } + } + + predict(); + // [END automl_language_sentiment_analysis_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_text_classification_create_dataset.js b/automl/language_text_classification_create_dataset.js new file mode 100644 index 0000000000..afe3ffdb74 --- /dev/null +++ b/automl/language_text_classification_create_dataset.js @@ -0,0 +1,71 @@ +// 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', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_language_text_classification_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + textClassificationDatasetMetadata: { + classificationType: 'MULTICLASS', + }, + }, + }; + + // Create dataset + const [operation] = await client.createDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_language_text_classification_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_text_classification_create_model.js b/automl/language_text_classification_create_model.js new file mode 100644 index 0000000000..8292e2ebaa --- /dev/null +++ b/automl/language_text_classification_create_model.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_language_text_classification_create_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + textClassificationModelMetadata: {}, // Leave unset, to use the default base model + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_language_text_classification_create_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/language_text_classification_predict.js b/automl/language_text_classification_predict.js new file mode 100644 index 0000000000..db49bdf08f --- /dev/null +++ b/automl/language_text_classification_predict.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID', + content = 'text to predict' +) { + // [START automl_language_text_classification_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const content = 'text to predict' + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new PredictionServiceClient(); + + async function predict() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + payload: { + textSnippet: { + content: content, + mimeType: 'text/plain', // Types: 'text/plain', 'text/html' + }, + }, + }; + + const [response] = await client.predict(request); + + for (const annotationPayload of response.payload) { + console.log(`Predicted class name: ${annotationPayload.displayName}`); + console.log( + `Predicted class score: ${annotationPayload.classification.score}` + ); + } + } + + predict(); + // [END automl_language_text_classification_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/list_datasets.js b/automl/list_datasets.js new file mode 100644 index 0000000000..555d823093 --- /dev/null +++ b/automl/list_datasets.js @@ -0,0 +1,112 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { + // [START automl_language_text_classification_list_datasets] + // [START automl_translate_list_datasets] + // [START automl_vision_classification_list_datasets] + // [START automl_vision_object_detection_list_datasets] + /** + * 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 listDatasets() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + filter: 'translation_dataset_metadata:*', + }; + + const [response] = await client.listDatasets(request); + + console.log('List of datasets:'); + for (const dataset of response) { + console.log(`Dataset name: ${dataset.name}`); + console.log( + `Dataset id: ${ + dataset.name.split('/')[dataset.name.split('/').length - 1] + }` + ); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log('Dataset create time'); + console.log(`\tseconds ${dataset.createTime.seconds}`); + console.log(`\tnanos ${dataset.createTime.nanos / 1e9}`); + // [END automl_language_text_classification_list_datasets] + // [END automl_translate_list_datasets] + // [END automl_vision_classification_list_datasets] + // [END automl_vision_object_detection_list_datasets] + console.log( + `Text extraction dataset metadata: ${dataset.textExtractionDatasetMetadata}` + ); + + console.log( + `Text sentiment dataset metadata: ${dataset.textSentimentDatasetMetadata}` + ); + + // [START automl_language_text_classification_list_datasets] + console.log( + `Text classification dataset metadata: ${dataset.textClassificationDatasetMetadata}` + ); + // [END automl_language_text_classification_list_datasets] + + // [START automl_translate_list_datasets] + if (dataset.translationDatasetMetadata !== undefined) { + console.log('Translation dataset metadata:'); + console.log( + `\tSource language code: ${dataset.translationDatasetMetadata.sourceLanguageCode}` + ); + console.log( + `\tTarget language code: ${dataset.translationDatasetMetadata.targetLanguageCode}` + ); + } + // [END automl_translate_list_datasets] + + // [START automl_vision_classification_list_datasets] + console.log( + `Image classification dataset metadata: ${dataset.imageClassificationDatasetMetadata}` + ); + // [END automl_vision_classification_list_datasets] + + // [START automl_vision_object_detection_list_datasets] + console.log( + `Image object detection dataset metatdata: ${dataset.imageObjectDetectionDatasetMetatdata}` + ); + // [START automl_language_text_classification_list_datasets] + // [START automl_translate_list_datasets] + // [START automl_vision_classification_list_datasets] + } + } + + listDatasets(); + // [END automl_language_text_classification_list_datasets] + // [END automl_translate_list_datasets] + // [END automl_vision_classification_list_datasets] + // [END automl_vision_object_detection_list_datasets] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/list_model_evaluations.js b/automl/list_model_evaluations.js new file mode 100644 index 0000000000..d8cd6bb070 --- /dev/null +++ b/automl/list_model_evaluations.js @@ -0,0 +1,116 @@ +// 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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/list_models.js b/automl/list_models.js new file mode 100644 index 0000000000..1a6a4600ea --- /dev/null +++ b/automl/list_models.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') { + // [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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/list_operation_status.js b/automl/list_operation_status.js new file mode 100644 index 0000000000..9d1630d2a6 --- /dev/null +++ b/automl/list_operation_status.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'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { + // [START automl_list_operation_status] + /** + * 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 listOperationStatus() { + // Construct request + const request = { + name: client.locationPath(projectId, location), + filter: `worksOn=projects/${projectId}/locations/${location}/models/*`, + }; + + const [response] = await client.operationsClient.listOperations(request); + + console.log('List of operation status:'); + for (const operation of response) { + console.log(`Name: ${operation.name}`); + console.log('Operation details:'); + console.log(`${operation}`); + } + } + + listOperationStatus(); + // [END automl_list_operation_status] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/package.json b/automl/package.json new file mode 100644 index 0000000000..7196e6e663 --- /dev/null +++ b/automl/package.json @@ -0,0 +1,30 @@ +{ + "name": "@google-cloud/automl-samples", + "description": "Samples for the Cloud AutoML Client Library for Node.js.", + "license": "Apache-2.0", + "author": "Google LLC", + "engines": { + "node": ">=12.0.0" + }, + "repository": "googleapis/nodejs-automl", + "private": true, + "scripts": { + "test": "mocha --timeout 9000000" + }, + "files": [ + "**/*.js", + "!test/" + ], + "dependencies": { + "@google-cloud/automl": "^3.1.2", + "csv": "^6.0.0", + "mathjs": "^11.0.0", + "yargs": "^16.0.0" + }, + "devDependencies": { + "@google-cloud/storage": "^6.0.0", + "chai": "^4.2.0", + "mocha": "^8.0.0", + "uuid": "^9.0.0" + } +} \ No newline at end of file diff --git a/automl/resources/input.txt b/automl/resources/input.txt new file mode 100644 index 0000000000..acea938083 --- /dev/null +++ b/automl/resources/input.txt @@ -0,0 +1 @@ +Tell me how this ends diff --git a/automl/resources/salad.jpg b/automl/resources/salad.jpg new file mode 100644 index 0000000000..a7f960b503 Binary files /dev/null and b/automl/resources/salad.jpg differ diff --git a/automl/resources/test.png b/automl/resources/test.png new file mode 100644 index 0000000000..653342a46e Binary files /dev/null and b/automl/resources/test.png differ diff --git a/automl/tables/create-dataset.v1beta1.js b/automl/tables/create-dataset.v1beta1.js new file mode 100644 index 0000000000..ae440409a5 --- /dev/null +++ b/automl/tables/create-dataset.v1beta1.js @@ -0,0 +1,66 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetName = 'YOUR_DATASET_NAME' +) { + // [START automl_tables_create_dataset] + const automl = require('@google-cloud/automl'); + const util = require('util'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to create a dataset + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetName = '[DATASET_NAME]' e.g., “myDataset”; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Set dataset name and metadata. + const myDataset = { + displayName: datasetName, + tablesDatasetMetadata: {}, + }; + + // Create a dataset with the dataset metadata in the region. + client + .createDataset({parent: projectLocation, dataset: myDataset}) + .then(responses => { + const dataset = responses[0]; + // Display the dataset information. + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset Id: ${dataset.name.split('/').pop(-1)}`); + console.log(`Dataset display name: ${dataset.displayName}`); + console.log(`Dataset example count: ${dataset.exampleCount}`); + console.log( + `Tables dataset metadata: ${util.inspect( + dataset.tablesDatasetMetadata, + false, + null + )}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_create_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/create-model.v1beta1.js b/automl/tables/create-model.v1beta1.js new file mode 100644 index 0000000000..bbed806cd7 --- /dev/null +++ b/automl/tables/create-model.v1beta1.js @@ -0,0 +1,83 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID', + columnId = 'COLUMN_ID', + modelName = 'MODEL_NAME', + trainBudget = 'TRAIN_BUDGET' +) { + // [START automl_tables_create_model] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to create a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"; + // const columnId = '[COLUMN_ID]' e.g., "773141392279994368"; + // const modelName = '[MODEL_NAME]' e.g., "testModel"; + // const trainBudget = '[TRAIN_BUDGET]' e.g., "1000", + // `Train budget in milli node hours`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // Get the full path of the column. + const columnSpecId = client.columnSpecPath( + projectId, + computeRegion, + datasetId, + tableId, + columnId + ); + + // Set target column to train the model. + const targetColumnSpec = {name: columnSpecId}; + + // Set tables model metadata. + const tablesModelMetadata = { + targetColumnSpec: targetColumnSpec, + trainBudgetMilliNodeHours: trainBudget, + }; + + // Set datasetId, model name and model metadata for the dataset. + const myModel = { + datasetId: datasetId, + displayName: modelName, + tablesModelMetadata: tablesModelMetadata, + }; + + // Create a model with the model metadata in the region. + client + .createModel({parent: projectLocation, model: myModel}) + .then(responses => { + const initialApiResponse = responses[1]; + console.log(`Training operation name: ${initialApiResponse.name}`); + console.log('Training started...'); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_create_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/delete-dataset.v1beta1.js b/automl/tables/delete-dataset.v1beta1.js new file mode 100644 index 0000000000..3e93801609 --- /dev/null +++ b/automl/tables/delete-dataset.v1beta1.js @@ -0,0 +1,58 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID' +) { + // [START automl_tables_delete_dataset] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to delete a dataset. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + // Delete a dataset. + client + .deleteDataset({name: datasetFullId}) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the dataset delete details. + console.log('Dataset delete details:'); + console.log('\tOperation details:'); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_delete_dataset] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/delete-model.v1beta1.js b/automl/tables/delete-model.v1beta1.js new file mode 100644 index 0000000000..679e6b1741 --- /dev/null +++ b/automl/tables/delete-model.v1beta1.js @@ -0,0 +1,58 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_delete_model] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to delete a model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Delete a model. + client + .deleteModel({name: modelFullId}) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the Model delete details. + console.log('Model delete details:'); + console.log('\tOperation details:'); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_delete_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/deploy-model.v1beta1.js b/automl/tables/deploy-model.v1beta1.js new file mode 100644 index 0000000000..741e1deb86 --- /dev/null +++ b/automl/tables/deploy-model.v1beta1.js @@ -0,0 +1,52 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_deploy_model] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to deploy model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Deploy a model with the deploy model request. + client + .deployModel({name: modelFullId}) + .then(responses => { + const response = responses[0]; + console.log('Deployment Details:'); + console.log(`\tName: ${response.name}`); + console.log('\tMetadata:'); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_deploy_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-column-spec.v1beta1.js b/automl/tables/get-column-spec.v1beta1.js new file mode 100644 index 0000000000..3f5ca23c8c --- /dev/null +++ b/automl/tables/get-column-spec.v1beta1.js @@ -0,0 +1,67 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + tableId = 'TABLE_ID', + columnId = 'COLUMN_ID' +) { + // [START automl_tables_get_column_spec] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get all column specs + * information in table colums. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const tableId = '[TABLE_ID]' e.g., "1991013247762825216"`; + // const columnId = '[COLUMN_ID]' e.g., "773141392279994368"; + + // Get the full path of the column. + const columnSpecId = client.columnSpecPath( + projectId, + computeRegion, + datasetId, + tableId, + columnId + ); + + // Get all the information about a given columnSpec of a particular + // table in a dataset. + client + .getColumnSpec({name: columnSpecId}) + .then(responses => { + const column = responses[0]; + // Display the column spec information. + console.log(`Column name: ${column.name}`); + console.log(`Column Id: ${column.name.split('/').pop(-1)}`); + console.log(`Column display name: ${column.displayName}`); + console.log(`Column datatype: ${column.dataType.typeCode}`); + console.log( + `Column distinct value count: ${column.dataStats.distinctValueCount}` + ); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_column_spec] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/get-model.v1beta1.js b/automl/tables/get-model.v1beta1.js new file mode 100644 index 0000000000..4989e465d7 --- /dev/null +++ b/automl/tables/get-model.v1beta1.js @@ -0,0 +1,61 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_get_model] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to get model details. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Get complete detail of the model. + client + .getModel({name: modelFullId}) + .then(responses => { + const model = responses[0]; + + // Display the model information. + console.log(`Model name: ${model.name}`); + console.log(`Model Id: ${model.name.split('/').pop(-1)}`); + console.log(`Model display name: ${model.displayName}`); + console.log(`Dataset Id: ${model.datasetId}`); + console.log('Tables model metadata: '); + console.log( + `\tTraining budget: ${model.tablesModelMetadata.trainBudgetMilliNodeHours}` + ); + console.log( + `\tTraining cost: ${model.tablesModelMetadata.trainCostMilliNodeHours}` + ); + console.log(`Model deployment state: ${model.deploymentState}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_get_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/import-data.v1beta1.js b/automl/tables/import-data.v1beta1.js new file mode 100644 index 0000000000..345a45f187 --- /dev/null +++ b/automl/tables/import-data.v1beta1.js @@ -0,0 +1,82 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + datasetId = 'YOUR_DATASET_ID', + path = 'GCS_PATH or BIGQUERY_PATH' +) { + // [START automl_tables_import_data] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to import data. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const datasetId = '[DATASET_ID]' e.g., "TBL2246891593778855936"; + // const path = '[GCS_PATH]' | '[BIGQUERY_PATH]' + // e.g., "gs:///" or + // "bq://..", + // `string or array of paths in AutoML Tables format`; + + // Get the full path of the dataset. + const datasetFullId = client.datasetPath(projectId, computeRegion, datasetId); + + let inputConfig = {}; + if (path.startsWith('bq')) { + // Get Bigquery URI. + inputConfig = { + bigquerySource: { + inputUri: path, + }, + }; + } else { + // Get the multiple Google Cloud Storage URIs. + const inputUris = path.split(','); + inputConfig = { + gcsSource: { + inputUris: inputUris, + }, + }; + } + + // Import the dataset from the input URI. + client + .importData({name: datasetFullId, inputConfig: inputConfig}) + .then(responses => { + const operation = responses[0]; + console.log('Processing import...'); + return operation.promise(); + }) + .then(responses => { + // The final result of the operation. + const operationDetails = responses[2]; + + // Get the data import details. + console.log('Data import details:'); + console.log('\tOperation details:'); + console.log(`\t\tName: ${operationDetails.name}`); + console.log(`\t\tDone: ${operationDetails.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_import_data] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-datasets.v1beta1.js b/automl/tables/list-datasets.v1beta1.js new file mode 100644 index 0000000000..ae716d9a0f --- /dev/null +++ b/automl/tables/list-datasets.v1beta1.js @@ -0,0 +1,79 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_datasets] + const automl = require('@google-cloud/automl'); + const util = require('util'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all datasets. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesDatasetMetadata:*"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the datasets available in the region by applying filter. + client + .listDatasets({parent: projectLocation, filter: filter}) + .then(responses => { + const dataset = responses[0]; + + // Display the dataset information. + console.log('List of datasets:'); + for (let i = 0; i < dataset.length; i++) { + const tablesDatasetMetadata = dataset[i].tablesDatasetMetadata; + + console.log(`Dataset name: ${dataset[i].name}`); + console.log(`Dataset Id: ${dataset[i].name.split('/').pop(-1)}`); + console.log(`Dataset display name: ${dataset[i].displayName}`); + console.log(`Dataset example count: ${dataset[i].exampleCount}`); + console.log('Tables dataset metadata:'); + console.log( + `\tTarget column correlations: ${util.inspect( + tablesDatasetMetadata.targetColumnCorrelations, + false, + null + )}` + ); + console.log( + `\tPrimary table spec Id: ${tablesDatasetMetadata.primaryTableSpecId}` + ); + console.log( + `\tTarget column spec Id: ${tablesDatasetMetadata.targetColumnSpecId}` + ); + console.log( + `\tWeight column spec Id: ${tablesDatasetMetadata.weightColumnSpecId}` + ); + console.log( + `\tMl use column spec Id: ${tablesDatasetMetadata.mlUseColumnSpecId}` + ); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_datasets] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-model-evaluations.v1beta1.js b/automl/tables/list-model-evaluations.v1beta1.js new file mode 100644 index 0000000000..b5bcb5b594 --- /dev/null +++ b/automl/tables/list-model-evaluations.v1beta1.js @@ -0,0 +1,167 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_model_evaluations] + const automl = require('@google-cloud/automl'); + const math = require('mathjs'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list model evaluations. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesModelMetadata:*"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // List all the model evaluations in the model by applying filter. + client + .listModelEvaluations({parent: modelFullId, filter: filter}) + .then(responses => { + const element = responses[0]; + console.log('List of model evaluations:'); + for (let i = 0; i < element.length; i++) { + const classMetrics = element[i].classificationEvaluationMetrics; + const regressionMetrics = element[i].regressionEvaluationMetrics; + const evaluationId = element[i].name.split('/')[7].split('`')[0]; + + console.log(`Model evaluation name: ${element[i].name}`); + console.log(`Model evaluation Id: ${evaluationId}`); + console.log( + `Model evaluation annotation spec Id: ${element[i].annotationSpecId}` + ); + console.log(`Model evaluation display name: ${element[i].displayName}`); + console.log( + `Model evaluation example count: ${element[i].evaluatedExampleCount}` + ); + + if (classMetrics) { + const confidenceMetricsEntries = classMetrics.confidenceMetricsEntry; + + console.log('Table classification evaluation metrics:'); + console.log(`\tModel auPrc: ${math.round(classMetrics.auPrc, 6)}`); + console.log(`\tModel auRoc: ${math.round(classMetrics.auRoc, 6)}`); + console.log( + `\tModel log loss: ${math.round(classMetrics.logLoss, 6)}` + ); + + if (confidenceMetricsEntries.length > 0) { + console.log('\tConfidence metrics entries:'); + + for (const confidenceMetricsEntry of confidenceMetricsEntries) { + console.log( + `\t\tModel confidence threshold: ${math.round( + confidenceMetricsEntry.confidenceThreshold, + 6 + )}` + ); + console.log( + `\t\tModel position threshold: ${math.round( + confidenceMetricsEntry.positionThreshold, + 4 + )}` + ); + console.log( + `\t\tModel recall: ${math.round( + confidenceMetricsEntry.recall * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision: ${math.round( + confidenceMetricsEntry.precision * 100, + 2 + )} %` + ); + console.log( + `\t\tModel false positive rate: ${confidenceMetricsEntry.falsePositiveRate}` + ); + console.log( + `\t\tModel f1 score: ${math.round( + confidenceMetricsEntry.f1Score * 100, + 2 + )} %` + ); + console.log( + `\t\tModel recall@1: ${math.round( + confidenceMetricsEntry.recallAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel precision@1: ${math.round( + confidenceMetricsEntry.precisionAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel false positive rate@1: ${confidenceMetricsEntry.falsePositiveRateAt1}` + ); + console.log( + `\t\tModel f1 score@1: ${math.round( + confidenceMetricsEntry.f1ScoreAt1 * 100, + 2 + )} %` + ); + console.log( + `\t\tModel true positive count: ${confidenceMetricsEntry.truePositiveCount}` + ); + console.log( + `\t\tModel false positive count: ${confidenceMetricsEntry.falsePositiveCount}` + ); + console.log( + `\t\tModel false negative count: ${confidenceMetricsEntry.falseNegativeCount}` + ); + console.log( + `\t\tModel true negative count: ${confidenceMetricsEntry.trueNegativeCount}` + ); + console.log('\n'); + } + } + console.log( + `\tModel annotation spec Id: ${classMetrics.annotationSpecId}` + ); + } else if (regressionMetrics) { + console.log('Table regression evaluation metrics:'); + console.log( + `\tModel root mean squared error: ${regressionMetrics.rootMeanSquaredError}` + ); + console.log( + `\tModel mean absolute error: ${regressionMetrics.meanAbsoluteError}` + ); + console.log( + `\tModel mean absolute percentage error: ${regressionMetrics.meanAbsolutePercentageError}` + ); + console.log(`\tModel rSquared: ${regressionMetrics.rSquared}`); + } + console.log('\n'); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_model_evaluations] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/list-models.v1beta1.js b/automl/tables/list-models.v1beta1.js new file mode 100644 index 0000000000..345740cdd1 --- /dev/null +++ b/automl/tables/list-models.v1beta1.js @@ -0,0 +1,64 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + filter = 'FILTER_EXPRESSION' +) { + // [START automl_tables_list_models] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to list all models. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const filter_ = '[FILTER_EXPRESSIONS]' e.g., "tablesModelMetadata:*"; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + // List all the models available in the region by applying filter. + client + .listModels({parent: projectLocation, filter: filter}) + .then(responses => { + const model = responses[0]; + + // Display the model information. + console.log('List of models:'); + for (let i = 0; i < model.length; i++) { + console.log(`\nModel name: ${model[i].name}`); + console.log(`Model Id: ${model[i].name.split('/').pop(-1)}`); + console.log(`Model display name: ${model[i].displayName}`); + console.log(`Dataset Id: ${model[i].datasetId}`); + console.log('Tables model metadata:'); + console.log( + `\tTraining budget: ${model[i].tablesModelMetadata.trainBudgetMilliNodeHours}` + ); + console.log( + `\tTraining cost: ${model[i].tablesModelMetadata.trainCostMilliNodeHours}` + ); + console.log(`Model deployment state: ${model[i].deploymentState}`); + } + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_list_models] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/tables/predict-bq-source-bq-dest.v1beta1.js b/automl/tables/predict-bq-source-bq-dest.v1beta1.js new file mode 100644 index 0000000000..5f5eeec542 --- /dev/null +++ b/automl/tables/predict-bq-source-bq-dest.v1beta1.js @@ -0,0 +1,85 @@ +// 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID', + inputUri = 'BIGQUERY_PATH', + outputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_batch_predict_bq] + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using bigQuery. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[BIGQUERY_PATH]' + // e.g., "bq://..", + // `The Big Query URI containing the inputs`; + // const outputUri = '[BIGQUERY_PATH]' e.g., "bq://", + // `The destination Big Query URI for storing outputs`; + + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); + + async function batchPredict() { + // Construct request + // Get the Big Query input URI. + const inputConfig = { + bigquerySource: { + inputUri: inputUri, + }, + }; + + // Get the Big Query output URI. + const outputConfig = { + bigqueryDestination: { + outputUri: outputUri, + }, + }; + + const [, operation] = await automlClient.batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }); + + // Get the latest state of long-running operation. + console.log(`Operation name: ${operation.name}`); + } + + batchPredict(); + // [END automl_tables_batch_predict_bq] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/tables/predict-gcs-source-bq-dest.v1beta1.js b/automl/tables/predict-gcs-source-bq-dest.v1beta1.js new file mode 100644 index 0000000000..2520925482 --- /dev/null +++ b/automl/tables/predict-gcs-source-bq-dest.v1beta1.js @@ -0,0 +1,82 @@ +// Copyright 2019 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( + projectId = 'YOUR_GCP_PROJECT_ID', + computeRegion = 'REGION', + modelId = 'MODEL_ID', + inputUri = 'GCS_PATH', + outputUri = 'BIGQUERY_DIRECTORY' +) { + // [START automl_tables_predict_using_gcs_source_and_bq_dest] + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using GCS + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[GCS_PATH]' e.g., "gs:///", + // `The Google Cloud Storage URI containing the inputs`; + // const outputUri = '[BIGQUERY_PATH]' e.g., "bq://", + // `The destination Big Query URI for storing outputs`; + + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); + + async function batchPredict() { + const inputConfig = { + gcsSource: { + inputUris: [inputUri], + }, + }; + + // Get the Big Query output URIs. + const outputConfig = { + bigqueryDestination: { + outputUri: outputUri, + }, + }; + + const [, operation] = await automlClient.batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }); + + // Get the latest state of long-running operation. + console.log(`Operation name: ${operation.name}`); + } + + batchPredict(); + // [END automl_tables_predict_using_gcs_source_and_bq_dest] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js b/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js new file mode 100644 index 0000000000..9b08c9bf57 --- /dev/null +++ b/automl/tables/predict-gcs-source-gcs-dest.v1beta1.js @@ -0,0 +1,85 @@ +// Copyright 2019 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( + projectId = 'YOUR_GCP_PROJECT_ID', + computeRegion = 'REGION', + modelId = 'YOUR_MODEL_ID', + inputUri = 'gs://your-bucket-uri/file.csv', + outputUriPrefix = 'gs://your-bucket-uri/OUTPUT_PREFIX/' +) { + // [START automl_tables_batch_predict] + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using GCS. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + // const inputUri = '[GCS_PATH]' e.g., "gs:///", + // `The Google Cloud Storage URI containing the inputs`; + // const outputUriPrefix = '[GCS_PATH]' + // e.g., "gs:///", + // `The destination Google Cloud Storage URI for storing outputs`; + + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); + + async function batchPredict() { + // Construct request + const inputConfig = { + gcsSource: { + inputUris: [inputUri], + }, + }; + + // Get the Google Cloud Storage output URI. + const outputConfig = { + gcsDestination: { + outputUriPrefix: outputUriPrefix, + }, + }; + + const [, operation] = await automlClient.batchPredict({ + name: modelFullId, + inputConfig: inputConfig, + outputConfig: outputConfig, + }); + + // Get the latest state of long-running operation. + console.log(`Operation name: ${operation.name}`); + return operation; + } + + batchPredict(); + // [END automl_tables_batch_predict] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/tables/predict.v1beta1.js b/automl/tables/predict.v1beta1.js new file mode 100644 index 0000000000..2e14414809 --- /dev/null +++ b/automl/tables/predict.v1beta1.js @@ -0,0 +1,95 @@ +// Copyright 2019 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( + projectId = 'YOUR_GCP_PROJECT_ID', + computeRegion = 'REGION', + modelId = 'YOUR_MODEL_ID', + inputs = '[{"numberValue": 1}, {"stringValue": "value"}]' +) { + inputs = JSON.parse(inputs); + + // [START automl_tables_predict] + + /** + * Demonstrates using the AutoML client to request prediction from + * automl tables using csv. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL000000000000"; + // const inputs = [{ numberValue: 1 }, { stringValue: 'value' }, { stringValue: 'value2' } ...] + + const automl = require('@google-cloud/automl'); + + // Create client for prediction service. + const automlClient = new automl.v1beta1.PredictionServiceClient(); + + // Get the full path of the model. + const modelFullId = automlClient.modelPath(projectId, computeRegion, modelId); + + async function predict() { + // Set the payload by giving the row values. + const payload = { + row: { + values: inputs, + }, + }; + + // Params is additional domain-specific parameters. + // Currently there is no additional parameters supported. + const [response] = await automlClient.predict({ + name: modelFullId, + payload: payload, + params: {feature_importance: true}, + }); + console.log('Prediction results:'); + + for (const result of response.payload) { + console.log(`Predicted class name: ${result.displayName}`); + console.log(`Predicted class score: ${result.tables.score}`); + + // Get features of top importance + const featureList = result.tables.tablesModelColumnInfo.map( + columnInfo => { + return { + importance: columnInfo.featureImportance, + displayName: columnInfo.columnDisplayName, + }; + } + ); + // Sort features by their importance, highest importance first + featureList.sort((a, b) => { + return b.importance - a.importance; + }); + + // Print top 10 important features + console.log('Features of top importance'); + console.log(featureList.slice(0, 10)); + } + } + predict(); + // [END automl_tables_predict] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/tables/undeploy-model.v1beta1.js b/automl/tables/undeploy-model.v1beta1.js new file mode 100644 index 0000000000..b2a3144c30 --- /dev/null +++ b/automl/tables/undeploy-model.v1beta1.js @@ -0,0 +1,52 @@ +// Copyright 2019 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( + projectId = 'YOUR_PROJECT_ID', + computeRegion = 'YOUR_REGION_NAME', + modelId = 'MODEL_ID' +) { + // [START automl_tables_undeploy_model] + const automl = require('@google-cloud/automl'); + const client = new automl.v1beta1.AutoMlClient(); + + /** + * Demonstrates using the AutoML client to undelpoy model. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const computeRegion = '[REGION_NAME]' e.g., "us-central1"; + // const modelId = '[MODEL_ID]' e.g., "TBL4704590352927948800"; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Undeploy a model with the undeploy model request. + client + .undeployModel({name: modelFullId}) + .then(responses => { + const response = responses[0]; + console.log('Undeployment Details:'); + console.log(`\tName: ${response.name}`); + console.log('\tMetadata:'); + console.log(`\t\tType Url: ${response.metadata.typeUrl}`); + console.log(`\tDone: ${response.done}`); + }) + .catch(err => { + console.error(err); + }); + // [END automl_tables_undeploy_model] +} +main(...process.argv.slice(2)).catch(console.error()); diff --git a/automl/test/.eslintrc.yml b/automl/test/.eslintrc.yml new file mode 100644 index 0000000000..fccd877b3e --- /dev/null +++ b/automl/test/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + no-warning-comments: off diff --git a/automl/test/automlTablesPredict.v1beta1.test.js b/automl/test/automlTablesPredict.v1beta1.test.js new file mode 100644 index 0000000000..77940a5a34 --- /dev/null +++ b/automl/test/automlTablesPredict.v1beta1.test.js @@ -0,0 +1,118 @@ +/** + * Copyright 2019 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const {delay} = require('./util'); +const {describe, it} = require('mocha'); +const {execSync} = require('child_process'); + +/** Tests for AutoML Tables "Prediction API" sample. */ + +const projectId = process.env.AUTOML_PROJECT_ID || 'cdpe-automl-tests'; +const region = 'us-central1'; +const modelId = process.env.TABLE_MODEL_ID; +const gcsInputUri = `gs://${projectId}-tables/predictTest.csv`; +const gcsOutputUriPrefix = `gs://${projectId}-tables/test_outputs/`; +const bqInputUri = `bq://${projectId}.automl_test.bank_marketing`; +const bqOutputUriPrefix = `bq://${projectId}`; + +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); + +describe('Tables PredictionAPI', () => { + it('should perform single prediction', async function () { + this.retries(5); + await delay(this.test); + + const inputs = [ + {numberValue: 39}, // Age + {stringValue: 'technician'}, // Job + {stringValue: 'married'}, // MaritalStatus + {stringValue: 'secondary'}, // Education + {stringValue: 'no'}, // Default + {numberValue: 52}, // Balance + {stringValue: 'no'}, // Housing + {stringValue: 'no'}, // Loan + {stringValue: 'cellular'}, // Contact + {numberValue: 12}, // Day + {stringValue: 'aug'}, // Month + {numberValue: 96}, // Duration + {numberValue: 2}, //Campaign + {numberValue: -1}, // PDays + {numberValue: 0}, // Previous + {stringValue: 'unknown'}, // POutcome + ]; + + const output = exec( + `node tables/predict.v1beta1.js "${projectId}" "${region}" "${modelId}" '${JSON.stringify( + inputs + )}'` + ); + + assert.include(output, 'Prediction results:'); + }); + + it(`should perform batch prediction using GCS as source and + GCS as destination`, async function () { + this.retries(5); + await delay(this.test); + + // Run batch prediction using GCS as source and GCS as destination + const output = exec( + `node tables/predict-gcs-source-gcs-dest.v1beta1.js "${projectId}" "${region}" "${modelId}" "${gcsInputUri}" "${gcsOutputUriPrefix}"` + ); + assert.include(output, 'Operation name:'); + }); + + it.skip(`should perform batch prediction using BQ as source and + GCS as destination`, async function () { + this.retries(5); + await delay(this.test); + + // Run batch prediction using BQ as source and GCS as destination + const output = exec( + `node tables/predict-gcs-source-bq-dest.v1beta1.js predict-using-bq-source-and-gcs-dest "${modelId}"` + + ` "${bqInputUri}" "${gcsOutputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); + + it(`should perform batch prediction using GCS as source and + BQ as destination`, async function () { + this.retries(5); + await delay(this.test); + + // Run batch prediction using GCS as source and BQ as destination + const output = exec( + `node tables/predict-gcs-source-bq-dest.v1beta1.js "${projectId}" "${region}" "${modelId}" ` + + ` "${gcsInputUri}" "${bqOutputUriPrefix}"` + ); + assert.include(output, 'Operation name:'); + }); + + it(`should perform batch prediction using BQ as source and + BQ as destination`, async function () { + this.retries(5); + await delay(this.test); + + // Run batch prediction using BQ as source and BQ as destination + const output = exec( + `node tables/predict-bq-source-bq-dest.v1beta1.js "${projectId}" "${region}" "${modelId}"` + + ` "${bqInputUri}" "${bqOutputUriPrefix}"` + ); + assert.match(output, /Operation name:/); + }); +}); diff --git a/automl/test/batch_predict.beta.test.js b/automl/test/batch_predict.beta.test.js new file mode 100644 index 0000000000..cc3fca26e6 --- /dev/null +++ b/automl/test/batch_predict.beta.test.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'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); + +const BATCH_PREDICT_REGION_TAG = 'beta/batch_predict'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TEN0000000000000000000'; + +describe('Automl Batch Predict Test', () => { + const client = new AutoMlClient(); + + it('should batch predict', async () => { + // As batch prediction can take a long time, instead try to batch predict + // on 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 inputUri = `gs://${projectId}-lcm/entity_extraction/input.jsonl`; + const outputUri = `gs://${projectId}-lcm/TEST_BATCH_PREDICT/`; + + const args = [ + BATCH_PREDICT_REGION_TAG, + projectId, + LOCATION, + MODEL_ID, + inputUri, + outputUri, + ]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /does not exist/); + }); +}); diff --git a/automl/test/batch_predict.test.js b/automl/test/batch_predict.test.js new file mode 100644 index 0000000000..6b66605499 --- /dev/null +++ b/automl/test/batch_predict.test.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'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const BATCH_PREDICT_REGION_TAG = 'batch_predict'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TEN0000000000000000000'; + +describe('Automl Batch Predict Test', () => { + const client = new AutoMlClient(); + + it('should batch predict', async () => { + // As batch prediction can take a long time, instead try to batch predict + // on 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 inputUri = `gs://${projectId}-lcm/entity_extraction/input.jsonl`; + const outputUri = `gs://${projectId}-lcm/TEST_BATCH_PREDICT/`; + + const args = [ + BATCH_PREDICT_REGION_TAG, + projectId, + LOCATION, + MODEL_ID, + inputUri, + outputUri, + ]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /does not exist/); + }); +}); diff --git a/automl/test/delete-dataset.beta.test.js b/automl/test/delete-dataset.beta.test.js new file mode 100644 index 0000000000..d91070d285 --- /dev/null +++ b/automl/test/delete-dataset.beta.test.js @@ -0,0 +1,59 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it, before} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const DELETE_DATASET_REGION_TAG = 'beta/delete-dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Translate Delete Dataset Tests', () => { + const client = new AutoMlClient(); + let datasetId; + + before('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + const request = { + parent: client.locationPath(projectId, LOCATION), + dataset: { + displayName: displayName, + translationDatasetMetadata: { + sourceLanguageCode: 'en', + targetLanguageCode: 'ja', + }, + }, + }; + const [response] = await client.createDataset(request); + datasetId = response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0]; + }); + + it('should delete a dataset', async () => { + const projectId = await client.getProjectId(); + const delete_output = execSync( + `node ${DELETE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${datasetId}` + ); + assert.match(delete_output, /Dataset deleted/); + }); +}); diff --git a/automl/test/delete-model.beta.test.js b/automl/test/delete-model.beta.test.js new file mode 100644 index 0000000000..b0ee46d96d --- /dev/null +++ b/automl/test/delete-model.beta.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').v1beta1; + +const cp = require('child_process'); + +const DELETE_MODEL_REGION_TAG = 'beta/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/delete_dataset.test.js b/automl/test/delete_dataset.test.js new file mode 100644 index 0000000000..5a035c36f0 --- /dev/null +++ b/automl/test/delete_dataset.test.js @@ -0,0 +1,61 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it, before} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const DELETE_DATASET_REGION_TAG = 'delete_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Translate Delete Dataset Tests', () => { + const client = new AutoMlClient(); + let datasetId; + + before('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + const request = { + parent: client.locationPath(projectId, LOCATION), + dataset: { + displayName: displayName, + translationDatasetMetadata: { + sourceLanguageCode: 'en', + targetLanguageCode: 'ja', + }, + }, + }; + const [operation] = await client.createDataset(request); + const [response] = await operation.promise(); + datasetId = response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0]; + }); + + it('should delete a dataset', async () => { + const projectId = await client.getProjectId(); + // delete + const delete_output = execSync( + `node ${DELETE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${datasetId}` + ); + assert.match(delete_output, /Dataset deleted/); + }); +}); diff --git a/automl/test/delete_model.test.js b/automl/test/delete_model.test.js new file mode 100644 index 0000000000..72fac7f66e --- /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 0000000000..561a13a2fe --- /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/export_dataset.test.js b/automl/test/export_dataset.test.js new file mode 100644 index 0000000000..64963844ab --- /dev/null +++ b/automl/test/export_dataset.test.js @@ -0,0 +1,64 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it, after} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; +const {Storage} = require('@google-cloud/storage'); + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const DATASET_ID = 'TRL8522556519449886720'; +const EXPORT_DATASET_REGION_TAG = 'export_dataset'; +const LOCATION = 'us-central1'; + +const {delay} = require('./util'); + +describe('Automl Translate Dataset Tests', () => { + const client = new AutoMlClient(); + const prefix = 'TEST_EXPORT_OUTPUT'; + + it('should export a datset', async function () { + this.retries(4); + await delay(this.test); + const projectId = await client.getProjectId(); + const bucketName = `${projectId}-automl-translate`; + const export_output = execSync( + `node ${EXPORT_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} gs://${bucketName}/${prefix}/` + ); + + assert.match(export_output, /Dataset exported/); + }); + + after('delete created files', async () => { + const projectId = await client.getProjectId(); + const bucketName = `${projectId}-automl-translate`; + + const storageClient = new Storage(); + const options = { + prefix: prefix, + }; + const [files] = await storageClient + .bucket(`gs://${bucketName}`) + .getFiles(options); + + for (const file of files) { + await storageClient.bucket(`gs://${bucketName}`).file(file.name).delete(); + } + }); +}); diff --git a/automl/test/get-model-evaluation.beta.test.js b/automl/test/get-model-evaluation.beta.test.js new file mode 100644 index 0000000000..8a34d7b092 --- /dev/null +++ b/automl/test/get-model-evaluation.beta.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').v1beta1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const GET_MODEL_EVALUATION_REGION_TAG = 'beta/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/get-model.beta.test.js b/automl/test/get-model.beta.test.js new file mode 100644 index 0000000000..b64295d354 --- /dev/null +++ b/automl/test/get-model.beta.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').v1beta1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const GET_MODEL_REGION_TAG = 'beta/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-operation-status.beta.test.js b/automl/test/get-operation-status.beta.test.js new file mode 100644 index 0000000000..2c1e60a7b3 --- /dev/null +++ b/automl/test/get-operation-status.beta.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').v1beta1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const GET_OPERATION_STATUS_REGION_TAG = 'beta/get-operation-status'; +const LOCATION = 'us-central1'; +const OPERATION_ID = 'TRL5980949629938696192'; + +describe('Automl Get Operation Status Tests', () => { + const client = new AutoMlClient(); + + it('should get operation status', async () => { + const projectId = await client.getProjectId(); + + const get_output = execSync( + `node ${GET_OPERATION_STATUS_REGION_TAG}.js ${projectId} ${LOCATION} ${OPERATION_ID}` + ); + assert.match(get_output, /Operation details/); + }); +}); diff --git a/automl/test/get_dataset.test.js b/automl/test/get_dataset.test.js new file mode 100644 index 0000000000..974ca10cee --- /dev/null +++ b/automl/test/get_dataset.test.js @@ -0,0 +1,40 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'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_DATASET_REGION_TAG = 'get_dataset'; +const DATASET_ID = 'TRL8522556519449886720'; +const LOCATION = 'us-central1'; + +describe('Automl Get Dataset Tests', () => { + const client = new AutoMlClient(); + + it('should get a dataset', async () => { + const projectId = await client.getProjectId(); + const get_output = execSync( + `node ${GET_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID}` + ); + + assert.match(get_output, /Dataset id/); + }); +}); diff --git a/automl/test/get_model.test.js b/automl/test/get_model.test.js new file mode 100644 index 0000000000..f014f3fd97 --- /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 0000000000..9ea43af945 --- /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/get_operation_status.test.js b/automl/test/get_operation_status.test.js new file mode 100644 index 0000000000..c58e188141 --- /dev/null +++ b/automl/test/get_operation_status.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_OPERATION_STATUS_REGION_TAG = 'get_operation_status'; +const LOCATION = 'us-central1'; +const OPERATION_ID = 'TRL5980949629938696192'; + +describe('Automl Get Operation Status Tests', () => { + const client = new AutoMlClient(); + + it('should get operation status', async () => { + const projectId = await client.getProjectId(); + + const get_output = execSync( + `node ${GET_OPERATION_STATUS_REGION_TAG}.js ${projectId} ${LOCATION} ${OPERATION_ID}` + ); + assert.match(get_output, /Operation details/); + }); +}); diff --git a/automl/test/import-dataset.beta.test.js b/automl/test/import-dataset.beta.test.js new file mode 100644 index 0000000000..ac2deb3130 --- /dev/null +++ b/automl/test/import-dataset.beta.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); + +const IMPORT_DATASET_REGION_TAG = 'beta/import-dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Import Dataset Test', () => { + const client = new AutoMlClient(); + + it('should import a dataset', async () => { + // As importing a dataset can take a long time, instead try to import to a + // nonexistent dataset and confirm that the dataset was not found, but + // other elements of the request were valid. + const projectId = await client.getProjectId(); + const data = `gs://${projectId}-automl-translate/en-ja-short.csv`; + const args = [ + IMPORT_DATASET_REGION_TAG, + projectId, + LOCATION, + 'TEN0000000000000000000', + data, + ]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + }); +}); diff --git a/automl/test/import_dataset.test.js b/automl/test/import_dataset.test.js new file mode 100644 index 0000000000..afbb0fbe68 --- /dev/null +++ b/automl/test/import_dataset.test.js @@ -0,0 +1,115 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it, before} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const IMPORT_DATASET_REGION_TAG = 'import_dataset'; +const LOCATION = 'us-central1'; +const TWENTY_MINUTES_IN_SECONDS = 60 * 20; +const {delay} = require('./util'); + +describe('Automl Import Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + before(async () => { + await cleanupOldDatasets(); + }); + + async function cleanupOldDatasets() { + const projectId = await client.getProjectId(); + let request = { + parent: client.locationPath(projectId, LOCATION), + filter: 'translation_dataset_metadata:*', + }; + const [response] = await client.listDatasets(request); + for (const dataset of response) { + try { + const id = dataset.name + .split('/') + [dataset.name.split('/').length - 1].split('\n')[0]; + console.info(`checking dataset ${id}`); + if (id.match(/test_[0-9a-f]{8}/)) { + console.info(`deleting dataset ${id}`); + if ( + dataset.createTime.seconds - Date.now() / 1000 > + TWENTY_MINUTES_IN_SECONDS + ) { + console.info(`dataset ${id} is greater than 20 minutes old`); + request = { + name: client.datasetPath(projectId, LOCATION, id), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + } + } + } catch (err) { + console.warn(err); + } + } + } + + it('should create a dataset', async function () { + this.retries(5); + await delay(this.test); + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + const request = { + parent: client.locationPath(projectId, LOCATION), + dataset: { + displayName: displayName, + translationDatasetMetadata: { + sourceLanguageCode: 'en', + targetLanguageCode: 'ja', + }, + }, + }; + const [operation] = await client.createDataset(request); + const [response] = await operation.promise(); + datasetId = response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0]; + }); + + it('should import dataset', async function () { + this.retries(5); + await delay(this.test); + const projectId = await client.getProjectId(); + const data = `gs://${projectId}-automl-translate/en-ja-short.csv`; + const import_output = execSync( + `node ${IMPORT_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${datasetId} ${data}` + ); + assert.match(import_output, /Dataset imported/); + }); + + it('should delete created dataset', async function () { + this.retries(5); + await delay(this.test); + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/language_entity_extraction_create_dataset.test.js b/automl/test/language_entity_extraction_create_dataset.test.js new file mode 100644 index 0000000000..ed5dcd8e9c --- /dev/null +++ b/automl/test/language_entity_extraction_create_dataset.test.js @@ -0,0 +1,54 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it, after} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'language_entity_extraction_create_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Natural Language Entity Extraction Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/language_entity_extraction_create_model.test.js b/automl/test/language_entity_extraction_create_model.test.js new file mode 100644 index 0000000000..5584816fd2 --- /dev/null +++ b/automl/test/language_entity_extraction_create_model.test.js @@ -0,0 +1,43 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const CREATE_MODEL_REGION_TAG = 'language_entity_extraction_create_model.js'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'TEN0000000000000000000'; + +describe('Automl Natural Language Entity Extraction Create Model Test', () => { + const client = new AutoMlClient(); + // let operationId; + + // Natural language entity extraction models are non cancellable operations + it('should create a model', async () => { + // As entity extraction does not let you cancel model creation, instead try + // to create a model from a nonexistent dataset, but other elements of the + // request were valid. + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /Dataset does not exist./); + }); +}); diff --git a/automl/test/language_entity_extraction_predict.test.js b/automl/test/language_entity_extraction_predict.test.js new file mode 100644 index 0000000000..500bf8ef83 --- /dev/null +++ b/automl/test/language_entity_extraction_predict.test.js @@ -0,0 +1,61 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it, before} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const PREDICT_REGION_TAG = 'language_entity_extraction_predict'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TEN2238627664384491520'; + +describe('Automl Natural Language Entity Extraction Predict Test', () => { + const client = new AutoMlClient(); + + before('should verify the model is deployed', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [response] = await client.getModel(request); + if (response.deploymentState === 'UNDEPLOYED') { + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + await operation.promise(); + } + }); + + it('should predict', async () => { + const projectId = await client.getProjectId(); + const content = + "'Constitutional mutations in the WT1 gene in patients with Denys-Drash syndrome.'"; + + const predictOutput = execSync( + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${content}` + ); + assert.match(predictOutput, /Text Extract Entity Types/); + }); +}); diff --git a/automl/test/language_sentiment_analysis_create_dataset.test.js b/automl/test/language_sentiment_analysis_create_dataset.test.js new file mode 100644 index 0000000000..d0e8ccd262 --- /dev/null +++ b/automl/test/language_sentiment_analysis_create_dataset.test.js @@ -0,0 +1,54 @@ +// Copyright 2019 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, after} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'language_sentiment_analysis_create_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Natural Language Sentiment Analysis Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/language_sentiment_analysis_create_model.test.js b/automl/test/language_sentiment_analysis_create_model.test.js new file mode 100644 index 0000000000..39994297c5 --- /dev/null +++ b/automl/test/language_sentiment_analysis_create_model.test.js @@ -0,0 +1,41 @@ +// Copyright 2019 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 CREATE_MODEL_REGION_TAG = 'language_sentiment_analysis_create_model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'TST00000000000000000'; + +describe('Automl Natural Language Sentiment Analysis Create Model Test', () => { + const client = new AutoMlClient(); + + it('should create a model', async () => { + // As sentimental analysis does not let you cancel model creation, instead try + // to create a model from a nonexistent dataset, but other elements of the + // request were valid. + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /does not exist/); + }); +}); diff --git a/automl/test/language_sentiment_analysis_predict.test.js b/automl/test/language_sentiment_analysis_predict.test.js new file mode 100644 index 0000000000..e6748ee28c --- /dev/null +++ b/automl/test/language_sentiment_analysis_predict.test.js @@ -0,0 +1,60 @@ +// Copyright 2019 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, before} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const PREDICT_REGION_TAG = 'language_sentiment_analysis_predict'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'TST3171435737203605504'; + +describe('Automl Natural Language Sentiment Analysis Predict Test', () => { + const client = new AutoMlClient(); + + before('should verify the model is deployed', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [response] = await client.getModel(request); + if (response.deploymentState === 'UNDEPLOYED') { + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + await operation.promise(); + } + }); + + it('should predict', async () => { + const projectId = await client.getProjectId(); + const content = "'Hopefully this Claritin kicks in soon'"; + + const predictOutput = execSync( + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${content}` + ); + assert.match(predictOutput, /Predicted sentiment score/); + }); +}); diff --git a/automl/test/language_text_classification_create_dataset.test.js b/automl/test/language_text_classification_create_dataset.test.js new file mode 100644 index 0000000000..f913d91bba --- /dev/null +++ b/automl/test/language_text_classification_create_dataset.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 {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'language_text_classification_create_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Natural Language Text Classification Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/language_text_classification_create_model.test.js b/automl/test/language_text_classification_create_model.test.js new file mode 100644 index 0000000000..d90352e680 --- /dev/null +++ b/automl/test/language_text_classification_create_model.test.js @@ -0,0 +1,38 @@ +// 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 CREATE_MODEL_REGION_TAG = 'language_text_classification_create_model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'TCN00000000000000000'; + +describe('Automl Natural Language Text Classification Create Model Test', () => { + const client = new AutoMlClient(); + + it('should create a model', async () => { + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /Dataset does not exist./); + }); +}); diff --git a/automl/test/language_text_classification_predict.test.js b/automl/test/language_text_classification_predict.test.js new file mode 100644 index 0000000000..7a86284e89 --- /dev/null +++ b/automl/test/language_text_classification_predict.test.js @@ -0,0 +1,60 @@ +// 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 {before, 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 MODEL_ID = 'TCN7483069430457434112'; +const PREDICT_REGION_TAG = 'language_text_classification_predict'; +const LOCATION = 'us-central1'; + +describe('Automl Natural Language Text Classification Predict Test', () => { + const client = new AutoMlClient(); + + before('should verify the model is deployed', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [response] = await client.getModel(request); + if (response.deploymentState === 'UNDEPLOYED') { + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + await operation.promise(); + } + }); + + it('should predict', async () => { + const projectId = await client.getProjectId(); + const content = "'Fruit and nut flavour'"; + + const predictOutput = execSync( + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${content}` + ); + assert.match(predictOutput, /Predicted class name/); + }); +}); diff --git a/automl/test/list-datasets.beta.test.js b/automl/test/list-datasets.beta.test.js new file mode 100644 index 0000000000..362e052f6a --- /dev/null +++ b/automl/test/list-datasets.beta.test.js @@ -0,0 +1,39 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LIST_DATASET_REGION_TAG = 'beta/list-datasets'; +const LOCATION = 'us-central1'; + +describe('Automl List Dataset Tests', () => { + const client = new AutoMlClient(); + + it('should list datasets', async () => { + const projectId = await client.getProjectId(); + const list_output = execSync( + `node ${LIST_DATASET_REGION_TAG}.js ${projectId} ${LOCATION}` + ); + + assert.match(list_output, /Dataset id/); + }); +}); diff --git a/automl/test/list-models.beta.test.js b/automl/test/list-models.beta.test.js new file mode 100644 index 0000000000..928812580d --- /dev/null +++ b/automl/test/list-models.beta.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').v1beta1; + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LIST_MODEL_REGION_TAG = 'beta/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/list_datasets.test.js b/automl/test/list_datasets.test.js new file mode 100644 index 0000000000..9bc3dce219 --- /dev/null +++ b/automl/test/list_datasets.test.js @@ -0,0 +1,39 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'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_DATASET_REGION_TAG = 'list_datasets'; +const LOCATION = 'us-central1'; + +describe('Automl List Dataset Tests', () => { + const client = new AutoMlClient(); + + it('should list datasets', async () => { + const projectId = await client.getProjectId(); + const list_output = execSync( + `node ${LIST_DATASET_REGION_TAG}.js ${projectId} ${LOCATION}` + ); + + assert.match(list_output, /Dataset id/); + }); +}); diff --git a/automl/test/list_model_evaluations.test.js b/automl/test/list_model_evaluations.test.js new file mode 100644 index 0000000000..33aab77574 --- /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 0000000000..2657a38b94 --- /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/list_operation_status.test.js b/automl/test/list_operation_status.test.js new file mode 100644 index 0000000000..f8ec88528e --- /dev/null +++ b/automl/test/list_operation_status.test.js @@ -0,0 +1,39 @@ +// 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_OPERATION_STATUS_REGION_TAG = 'list_operation_status'; +const LOCATION = 'us-central1'; + +describe('Automl List Operation Status Tests', () => { + const client = new AutoMlClient(); + + it('should list operation status', async () => { + const projectId = await client.getProjectId(); + + const list_output = execSync( + `node ${LIST_OPERATION_STATUS_REGION_TAG}.js ${projectId} ${LOCATION} ` + ); + assert.match(list_output, /List of operation status:/); + }); +}); diff --git a/automl/test/setEndpoint.test.js b/automl/test/setEndpoint.test.js new file mode 100644 index 0000000000..4996c742e4 --- /dev/null +++ b/automl/test/setEndpoint.test.js @@ -0,0 +1,30 @@ +// Copyright 2019 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 cp = require('child_process'); + +const projectId = process.env.GCLOUD_PROJECT; + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +describe('set endpoint for automl', () => { + it('should list all datasets in `eu`', async () => { + const stdout = execSync(`node beta/setEndpoint.js "${projectId}"`); + assert.match(stdout, /do_not_delete_eu/); + }); +}); diff --git a/automl/test/translate_create_dataset.test.js b/automl/test/translate_create_dataset.test.js new file mode 100644 index 0000000000..0d6ae52abe --- /dev/null +++ b/automl/test/translate_create_dataset.test.js @@ -0,0 +1,54 @@ +// Copyright 2019 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, after} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'translate_create_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Translate Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/translate_create_model.test.js b/automl/test/translate_create_model.test.js new file mode 100644 index 0000000000..7925b38625 --- /dev/null +++ b/automl/test/translate_create_model.test.js @@ -0,0 +1,45 @@ +// Copyright 2019 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 CREATE_MODEL_REGION_TAG = 'translate_create_model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'TRL8522556519449886720'; + +const {delay} = require('./util'); + +describe('Automl Translate Create Model Tests', () => { + const client = new AutoMlClient(); + + it.skip('should create a model', async function () { + this.retries(5); + await delay(this.test); + + const projectId = await client.getProjectId(); + const create_output = execSync( + `node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} translation_test_create_model` + ); + + assert.match(create_output, /Training started/); + }); +}); diff --git a/automl/test/translate_predict.test.js b/automl/test/translate_predict.test.js new file mode 100644 index 0000000000..cb9c71e742 --- /dev/null +++ b/automl/test/translate_predict.test.js @@ -0,0 +1,40 @@ +// Copyright 2019 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 MODEL_ID = 'TRL1218052175389786112'; +const PREDICT_REGION_TAG = 'translate_predict'; +const LOCATION = 'us-central1'; + +describe('Automl Translate Predict Test', () => { + const client = new AutoMlClient(); + + it('should predict', async () => { + const projectId = await client.getProjectId(); + + const list_output = execSync( + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} resources/input.txt` + ); + assert.match(list_output, /Translated content:/); + }); +}); diff --git a/automl/test/undeploy_model.test.js b/automl/test/undeploy_model.test.js new file mode 100644 index 0000000000..583a213b67 --- /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/test/util.js b/automl/test/util.js new file mode 100644 index 0000000000..97b804a43c --- /dev/null +++ b/automl/test/util.js @@ -0,0 +1,28 @@ +// 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. + +// ML tests frequently run into concurrency and quota issues, for which +// retrying with a backoff is a good strategy: +module.exports = { + async delay(test) { + const retries = test.currentRetry(); + if (retries === 0) return; // no retry on the first failure. + // see: https://cloud.google.com/storage/docs/exponential-backoff: + const ms = Math.pow(2, retries) * 1000 + Math.random() * 2000; + return new Promise(done => { + console.info(`retrying "${test.title}" in ${ms}ms`); + setTimeout(done, ms); + }); + }, +}; diff --git a/automl/test/video-classification-create-dataset.beta.test.js b/automl/test/video-classification-create-dataset.beta.test.js new file mode 100644 index 0000000000..683bbeba4d --- /dev/null +++ b/automl/test/video-classification-create-dataset.beta.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 {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'beta/video-classification-create-dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Video Classification Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/video-classification-create-model.beta.test.js b/automl/test/video-classification-create-model.beta.test.js new file mode 100644 index 0000000000..6864228135 --- /dev/null +++ b/automl/test/video-classification-create-model.beta.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); + +const CREATE_MODEL_REGION_TAG = 'beta/video-classification-create-model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'VCN0000000000000000000'; + +describe('Automl Video Classification Create Model Test', () => { + const client = new AutoMlClient(); + + it('should create a model', async () => { + // As video classification does not let you cancel model creation, instead try + // to create a model from a nonexistent dataset, but other elements of the + // request were valid. + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /Dataset does not exist./); + }); +}); diff --git a/automl/test/video-object-tracking-create-dataset.beta.test.js b/automl/test/video-object-tracking-create-dataset.beta.test.js new file mode 100644 index 0000000000..64cf1f3a62 --- /dev/null +++ b/automl/test/video-object-tracking-create-dataset.beta.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 {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'beta/video-object-tracking-create-dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Video Object Tracking Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/video-object-tracking-create-model.beta.test.js b/automl/test/video-object-tracking-create-model.beta.test.js new file mode 100644 index 0000000000..68021fe9a8 --- /dev/null +++ b/automl/test/video-object-tracking-create-model.beta.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1beta1; + +const cp = require('child_process'); + +const CREATE_MODEL_REGION_TAG = 'beta/video-object-tracking-create-model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'VOT0000000000000000000'; + +describe('Automl Video Object Tracking Create Model Test', () => { + const client = new AutoMlClient(); + + it('should create a model', async () => { + // As object detection does not let you cancel model creation, instead try + // to create a model from a nonexistent dataset, but other elements of the + // request were valid. + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /Dataset does not exist./); + }); +}); diff --git a/automl/test/vision_classification_create_dataset.test.js b/automl/test/vision_classification_create_dataset.test.js new file mode 100644 index 0000000000..cda62f9805 --- /dev/null +++ b/automl/test/vision_classification_create_dataset.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 {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'vision_classification_create_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Vision Classification Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/vision_classification_create_model.test.js b/automl/test/vision_classification_create_model.test.js new file mode 100644 index 0000000000..59b9a79e28 --- /dev/null +++ b/automl/test/vision_classification_create_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 CREATE_MODEL_REGION_TAG = 'vision_classification_create_model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'ICN000000000000000000'; + +describe('Automl Vision Classification Create Model Tests', () => { + const client = new AutoMlClient(); + + it('should create a model', async () => { + // As vision classification does not let you cancel model creation, instead try + // to create a model from a nonexistent dataset, but other elements of the + // request were valid. + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /Dataset does not exist./); + }); +}); diff --git a/automl/test/vision_classification_deploy_model_node_count.test.js b/automl/test/vision_classification_deploy_model_node_count.test.js new file mode 100644 index 0000000000..bda2e48360 --- /dev/null +++ b/automl/test/vision_classification_deploy_model_node_count.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 {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 = + 'vision_classification_deploy_model_node_count.js'; +const LOCATION = 'us-central1'; +const MODEL_ID = 'ICN0000000000000000000'; + +describe('Automl Vision Classification Deploy Model Test', () => { + const client = new AutoMlClient(); + + it('should deploy a model with a specified node count', 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/vision_classification_predict.test.js b/automl/test/vision_classification_predict.test.js new file mode 100644 index 0000000000..71acdbf9b0 --- /dev/null +++ b/automl/test/vision_classification_predict.test.js @@ -0,0 +1,60 @@ +// 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 {before, 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 MODEL_ID = 'ICN5317963909599068160'; +const PREDICT_REGION_TAG = 'vision_classification_predict'; +const LOCATION = 'us-central1'; + +describe('Automl Vision Classification Predict Test', () => { + const client = new AutoMlClient(); + + before('should verify the model is deployed', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [response] = await client.getModel(request); + if (response.deploymentState === 'UNDEPLOYED') { + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + await operation.promise(); + } + }); + + it('should predict', async () => { + const projectId = await client.getProjectId(); + const filePath = 'resources/test.png'; + + const predictOutput = execSync( + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${filePath}` + ); + assert.match(predictOutput, /Predicted class name/); + }); +}); diff --git a/automl/test/vision_object_detection_create_dataset.test.js b/automl/test/vision_object_detection_create_dataset.test.js new file mode 100644 index 0000000000..5516af3997 --- /dev/null +++ b/automl/test/vision_object_detection_create_dataset.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const CREATE_DATASET_REGION_TAG = 'vision_object_detection_create_dataset'; +const LOCATION = 'us-central1'; + +describe('Automl Vision Object Detection Create Dataset Test', () => { + const client = new AutoMlClient(); + let datasetId; + + it('should create a dataset', async () => { + const projectId = await client.getProjectId(); + const displayName = `test_${uuid.v4().replace(/-/g, '_').substring(0, 26)}`; + + // create + const create_output = await execSync( + `node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}` + ); + assert.match(create_output, /Dataset id:/); + + datasetId = create_output.split('Dataset id: ')[1].split('\n')[0]; + }); + + after('delete created dataset', async () => { + const projectId = await client.getProjectId(); + const request = { + name: client.datasetPath(projectId, LOCATION, datasetId), + }; + const [operation] = await client.deleteDataset(request); + await operation.promise(); + }); +}); diff --git a/automl/test/vision_object_detection_create_model.test.js b/automl/test/vision_object_detection_create_model.test.js new file mode 100644 index 0000000000..a867bc1e53 --- /dev/null +++ b/automl/test/vision_object_detection_create_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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const {AutoMlClient} = require('@google-cloud/automl').v1; + +const cp = require('child_process'); + +const CREATE_MODEL_REGION_TAG = 'vision_object_detection_create_model'; +const LOCATION = 'us-central1'; +const DATASET_ID = 'IOD0000000000000000000'; + +describe('Automl Vision Object Detection Create Model Test', () => { + const client = new AutoMlClient(); + + it('should create a model', async () => { + // As object detection does not let you cancel model creation, instead try + // to create a model from a nonexistent dataset, but other elements of the + // request were valid. + const projectId = await client.getProjectId(); + const args = [CREATE_MODEL_REGION_TAG, projectId, LOCATION, DATASET_ID]; + const output = cp.spawnSync('node', args, {encoding: 'utf8'}); + + assert.match(output.stderr, /NOT_FOUND/); + assert.match(output.stderr, /Dataset does not exist./); + }); +}); diff --git a/automl/test/vision_object_detection_deploy_model_node_count.test.js b/automl/test/vision_object_detection_deploy_model_node_count.test.js new file mode 100644 index 0000000000..a3203812ee --- /dev/null +++ b/automl/test/vision_object_detection_deploy_model_node_count.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'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 = + 'vision_object_detection_deploy_model_node_count'; +const LOCATION = 'us-central1'; +const MODEL_ID = '0000000000000000000000'; + +describe('Automl Vision Object Detection Deploy Model Test', () => { + const client = new AutoMlClient(); + + it('should deploy a model with a specified node count', 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/vision_object_detection_predict.test.js b/automl/test/vision_object_detection_predict.test.js new file mode 100644 index 0000000000..3842a2a403 --- /dev/null +++ b/automl/test/vision_object_detection_predict.test.js @@ -0,0 +1,64 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'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 MODEL_ID = 'IOD1656537412546854912'; +const PREDICT_REGION_TAG = 'vision_object_detection_predict'; +const LOCATION = 'us-central1'; + +const {delay} = require('./util'); + +describe('Automl Vision Object Detection Predict Test', () => { + const client = new AutoMlClient(); + + it('should verify the model is deployed', async function () { + this.retries(5); + await delay(this.test); + const projectId = await client.getProjectId(); + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [response] = await client.getModel(request); + if (response.deploymentState === 'DEPLOYED') { + const request = { + name: client.modelPath(projectId, LOCATION, MODEL_ID), + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + await operation.promise(); + } + }); + + it('should predict', async () => { + const projectId = await client.getProjectId(); + const filePath = 'resources/salad.jpg'; + + const predictOutput = execSync( + `node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${filePath}` + ); + assert.match(predictOutput, /Predicted class name/); + }); +}); diff --git a/automl/translate/automlTranslateCreateDataset.js b/automl/translate/automlTranslateCreateDataset.js new file mode 100644 index 0000000000..b42f68e34a --- /dev/null +++ b/automl/translate/automlTranslateCreateDataset.js @@ -0,0 +1,76 @@ +// 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(projectId, displayName) { + // [START automl_translation_create_dataset] + + /** + * Demonstrates using the AutoML client to request to create dataset for + * automl translation. + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project"; + // const displayName = '[DATASET_DISPLAY_NAME]' e.g., "my-dataset-name"; + + const automl = require('@google-cloud/automl'); + + // Create client for automl service. + const client = new automl.AutoMlClient(); + const computeRegion = 'us-central1'; + const source = 'en'; + const target = 'ja'; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + async function createDataset() { + // Specify the source and target language. + const datasetSpec = { + sourceLanguageCode: source, + targetLanguageCode: target, + }; + + // Set dataset name and dataset specification. + const datasetInfo = { + displayName: displayName, + translationDatasetMetadata: datasetSpec, + }; + + // Create a dataset with the dataset specification in the region. + const [operation] = await client.createDataset({ + parent: projectLocation, + dataset: datasetInfo, + }); + + // wait for lro to finish + const [dataset] = await operation.promise(); + // Display the dataset information + console.log(`Dataset name: ${dataset.name}`); + console.log(`Dataset id: ${dataset.name.split('/').pop(-1)}`); + } + + createDataset(); + // [END automl_translation_create_dataset] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/translate/automlTranslateCreateModel.js b/automl/translate/automlTranslateCreateModel.js new file mode 100644 index 0000000000..85fb5e75cd --- /dev/null +++ b/automl/translate/automlTranslateCreateModel.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +async function main(projectId, computeRegion, datasetId, modelName) { + // [START automl_translation_create_model] + const automl = require('@google-cloud/automl'); + + const client = new automl.AutoMlClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const datasetId = `Id of the dataset`; + // const modelName = `Name of the model, e.g. "myModel"`; + + // A resource that represents Google Cloud Platform location. + const projectLocation = client.locationPath(projectId, computeRegion); + + async function createModel() { + // Set model name and dataset. + const myModel = { + displayName: modelName, + datasetId: datasetId, + translationModelMetadata: {}, + }; + + // Create a model with the model metadata in the region. + const [, response] = await client.createModel({ + parent: projectLocation, + model: myModel, + }); + + console.log('Training operation name: ', response.name); + console.log('Training started...'); + } + + createModel(); + // [END automl_translation_create_model] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/translate/automlTranslatePredict.js b/automl/translate/automlTranslatePredict.js new file mode 100644 index 0000000000..b544913e23 --- /dev/null +++ b/automl/translate/automlTranslatePredict.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +async function main(projectId, computeRegion, modelId, filePath) { + // [START automl_translation_predict] + const automl = require('@google-cloud/automl'); + const fs = require('fs'); + + // Create client for prediction service. + const client = new automl.PredictionServiceClient(); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`; + // const computeRegion = `region-name, e.g. "us-central1"`; + // const modelId = `id of the model, e.g. “ICN12345”`; + // const filePath = `local text file path of content to be classified, e.g. "./resources/test.txt"`; + // const translationAllowFallback = `use Google translation model as fallback, e.g. "False" or "True"`; + + // Get the full path of the model. + const modelFullId = client.modelPath(projectId, computeRegion, modelId); + + // Read the file content for translation. + const content = fs.readFileSync(filePath, 'utf8'); + + async function predict() { + // Set the payload by giving the content of the file. + const payload = { + textSnippet: { + content: content, + }, + }; + + const [response] = await client.predict({ + name: modelFullId, + payload: payload, + }); + + console.log( + 'Translated Content: ', + response.payload[0].translation.translatedContent.content + ); + } + + predict(); + // [END automl_translation_predict] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/automl/translate/resources/testInput.txt b/automl/translate/resources/testInput.txt new file mode 100644 index 0000000000..acea938083 --- /dev/null +++ b/automl/translate/resources/testInput.txt @@ -0,0 +1 @@ +Tell me how this ends diff --git a/automl/translate_create_dataset.js b/automl/translate_create_dataset.js new file mode 100644 index 0000000000..b4be3e624d --- /dev/null +++ b/automl/translate_create_dataset.js @@ -0,0 +1,72 @@ +// Copyright 2019 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', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_translate_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + translationDatasetMetadata: { + sourceLanguageCode: 'en', + targetLanguageCode: 'ja', + }, + }, + }; + + // Create dataset + const [operation] = await client.createDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_translate_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/translate_create_model.js b/automl/translate_create_model.js new file mode 100644 index 0000000000..6f495814e3 --- /dev/null +++ b/automl/translate_create_model.js @@ -0,0 +1,63 @@ +// Copyright 2019 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', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_translate_create_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + translationModelMetadata: {}, + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log('Training started...'); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_translate_create_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/translate_predict.js b/automl/translate_predict.js new file mode 100644 index 0000000000..cbdcfb127b --- /dev/null +++ b/automl/translate_predict.js @@ -0,0 +1,69 @@ +// Copyright 2019 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', + filePath = 'path_to_local_file.txt' +) { + // [START automl_translate_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const filePath = 'path_to_local_file.txt'; + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + const fs = require('fs'); + + // Instantiates a client + const client = new PredictionServiceClient(); + + // Read the file content for translation. + const content = fs.readFileSync(filePath, 'utf8'); + + async function predict() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + payload: { + textSnippet: { + content: content, + }, + }, + }; + + const [response] = await client.predict(request); + + console.log( + 'Translated content: ', + response.payload[0].translation.translatedContent.content + ); + } + + predict(); + // [END automl_translate_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/undeploy_model.js b/automl/undeploy_model.js new file mode 100644 index 0000000000..35ac978189 --- /dev/null +++ b/automl/undeploy_model.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', + 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] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_classification_create_dataset.js b/automl/vision_classification_create_dataset.js new file mode 100644 index 0000000000..b2e8184bbb --- /dev/null +++ b/automl/vision_classification_create_dataset.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_vision_classification_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + // Specify the classification type + // Types: + // MultiLabel: Multiple labels are allowed for one example. + // MultiClass: At most one label is allowed per example. + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + imageClassificationDatasetMetadata: { + classificationType: 'MULTILABEL', + }, + }, + }; + + // Create dataset + const [operation] = await client.createDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_vision_classification_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_classification_create_model.js b/automl/vision_classification_create_model.js new file mode 100644 index 0000000000..fefa29648c --- /dev/null +++ b/automl/vision_classification_create_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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_vision_classification_create_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + imageClassificationModelMetadata: { + trainBudgetMilliNodeHours: 24000, + }, + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_vision_classification_create_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_classification_deploy_model_node_count.js b/automl/vision_classification_deploy_model_node_count.js new file mode 100644 index 0000000000..80971ba1e6 --- /dev/null +++ b/automl/vision_classification_deploy_model_node_count.js @@ -0,0 +1,60 @@ +// 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_vision_classification_deploy_model_node_count] + /** + * 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 deployModelWithNodeCount() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + imageClassificationModelDeploymentMetadata: { + nodeCount: 2, + }, + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Model deployment finished. ${response}`); + } + + deployModelWithNodeCount(); + // [END automl_vision_classification_deploy_model_node_count] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_classification_predict.js b/automl/vision_classification_predict.js new file mode 100644 index 0000000000..a46fac2591 --- /dev/null +++ b/automl/vision_classification_predict.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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID', + filePath = 'path_to_local_file.jpg' +) { + // [START automl_vision_classification_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const filePath = 'path_to_local_file.jpg'; + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + const fs = require('fs'); + + // Instantiates a client + const client = new PredictionServiceClient(); + + // Read the file content for translation. + const content = fs.readFileSync(filePath); + + async function predict() { + // Construct request + // params is additional domain-specific parameters. + // score_threshold is used to filter the result + const request = { + name: client.modelPath(projectId, location, modelId), + payload: { + image: { + imageBytes: content, + }, + }, + }; + + const [response] = await client.predict(request); + + for (const annotationPayload of response.payload) { + console.log(`Predicted class name: ${annotationPayload.displayName}`); + console.log( + `Predicted class score: ${annotationPayload.classification.score}` + ); + } + } + + predict(); + // [END automl_vision_classification_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_object_detection_create_dataset.js b/automl/vision_object_detection_create_dataset.js new file mode 100644 index 0000000000..bfa74cff13 --- /dev/null +++ b/automl/vision_object_detection_create_dataset.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_vision_object_detection_create_dataset] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createDataset() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + dataset: { + displayName: displayName, + imageObjectDetectionDatasetMetadata: {}, + }, + }; + + // Create dataset + const [operation] = await client.createDataset(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Dataset name: ${response.name}`); + console.log(` + Dataset id: ${ + response.name + .split('/') + [response.name.split('/').length - 1].split('\n')[0] + }`); + } + + createDataset(); + // [END automl_vision_object_detection_create_dataset] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_object_detection_create_model.js b/automl/vision_object_detection_create_model.js new file mode 100644 index 0000000000..ef8922c177 --- /dev/null +++ b/automl/vision_object_detection_create_model.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + datasetId = 'YOUR_DATASET_ID', + displayName = 'YOUR_DISPLAY_NAME' +) { + // [START automl_vision_object_detection_create_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const dataset_id = 'YOUR_DATASET_ID'; + // const displayName = 'YOUR_DISPLAY_NAME'; + + // Imports the Google Cloud AutoML library + const {AutoMlClient} = require('@google-cloud/automl').v1; + + // Instantiates a client + const client = new AutoMlClient(); + + async function createModel() { + // Construct request + const request = { + parent: client.locationPath(projectId, location), + model: { + displayName: displayName, + datasetId: datasetId, + imageObjectDetectionModelMetadata: {}, + }, + }; + + // Don't wait for the LRO + const [operation] = await client.createModel(request); + console.log(`Training started... ${operation}`); + console.log(`Training operation name: ${operation.name}`); + } + + createModel(); + // [END automl_vision_object_detection_create_model] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_object_detection_deploy_model_node_count.js b/automl/vision_object_detection_deploy_model_node_count.js new file mode 100644 index 0000000000..90fb1d66b5 --- /dev/null +++ b/automl/vision_object_detection_deploy_model_node_count.js @@ -0,0 +1,60 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID' +) { + // [START automl_vision_object_detection_deploy_model_node_count] + /** + * 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 deployModelWithNodeCount() { + // Construct request + const request = { + name: client.modelPath(projectId, location, modelId), + imageObjectDetectionModelDeploymentMetadata: { + nodeCount: 2, + }, + }; + + const [operation] = await client.deployModel(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + console.log(`Model deployment finished. ${response}`); + } + + deployModelWithNodeCount(); + // [END automl_vision_object_detection_deploy_model_node_count] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2)); diff --git a/automl/vision_object_detection_predict.js b/automl/vision_object_detection_predict.js new file mode 100644 index 0000000000..dcf24a726d --- /dev/null +++ b/automl/vision_object_detection_predict.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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'YOUR_MODEL_ID', + filePath = 'path_to_local_file.jpg' +) { + // [START automl_vision_object_detection_predict] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const filePath = 'path_to_local_file.jpg'; + + // Imports the Google Cloud AutoML library + const {PredictionServiceClient} = require('@google-cloud/automl').v1; + const fs = require('fs'); + + // Instantiates a client + const client = new PredictionServiceClient(); + + // Read the file content for translation. + const content = fs.readFileSync(filePath); + + async function predict() { + // Construct request + // params is additional domain-specific parameters. + // score_threshold is used to filter the result + const request = { + name: client.modelPath(projectId, location, modelId), + payload: { + image: { + imageBytes: content, + }, + }, + params: { + score_threshold: '0.8', + }, + }; + + const [response] = await client.predict(request); + + for (const annotationPayload of response.payload) { + console.log(`Predicted class name: ${annotationPayload.displayName}`); + console.log( + `Predicted class score: ${annotationPayload.imageObjectDetection.score}` + ); + console.log('Normalized vertices:'); + for (const vertex of annotationPayload.imageObjectDetection.boundingBox + .normalizedVertices) { + console.log(`\tX: ${vertex.x}, Y: ${vertex.y}`); + } + } + } + + predict(); + // [END automl_vision_object_detection_predict] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); +main(...process.argv.slice(2));