From 1e560c11c34018b8279a9fc66b9885dcc5a86b3d Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 12 Mar 2020 11:04:47 -0600 Subject: [PATCH] docs: move samples out of branch (#338) * docs: move samples out of branch * fix assert * remove empty line --- automl/beta/batch_predict.js | 70 ++++++++++++++++++++++++++ automl/test/batch_predict.beta.test.js | 45 +++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 automl/beta/batch_predict.js create mode 100644 automl/test/batch_predict.beta.test.js diff --git a/automl/beta/batch_predict.js b/automl/beta/batch_predict.js new file mode 100644 index 0000000000..c08a112499 --- /dev/null +++ b/automl/beta/batch_predict.js @@ -0,0 +1,70 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +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] +} + +main(...process.argv.slice(2)); diff --git a/automl/test/batch_predict.beta.test.js b/automl/test/batch_predict.beta.test.js new file mode 100644 index 0000000000..1ec333d914 --- /dev/null +++ b/automl/test/batch_predict.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 execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +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/); + }); +});