Skip to content

Commit

Permalink
docs: move samples out of branch (#338)
Browse files Browse the repository at this point in the history
* docs: move samples out of branch

* fix assert

* remove empty line
  • Loading branch information
nnegrey authored Mar 12, 2020
1 parent 33e00e0 commit 601b57e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
70 changes: 70 additions & 0 deletions automl/beta/batch_predict.js
Original file line number Diff line number Diff line change
@@ -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));
45 changes: 45 additions & 0 deletions automl/test/batch_predict.beta.test.js
Original file line number Diff line number Diff line change
@@ -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/);
});
});

0 comments on commit 601b57e

Please sign in to comment.