Skip to content

Commit 1e560c1

Browse files
nnegreyAhrar Monsur
authored and
Ahrar Monsur
committed
docs: move samples out of branch (#338)
* docs: move samples out of branch * fix assert * remove empty line
1 parent 2843010 commit 1e560c1

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

automl/beta/batch_predict.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
modelId = 'YOUR_MODEL_ID',
21+
inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl',
22+
outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/'
23+
) {
24+
// [START automl_batch_predict_beta]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'YOUR_PROJECT_ID';
29+
// const location = 'us-central1';
30+
// const modelId = 'YOUR_MODEL_ID';
31+
// const inputUri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl';
32+
// const outputUri = 'gs://YOUR_BUCKET_ID/path_to_save_results/';
33+
34+
// Imports the Google Cloud AutoML library
35+
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1beta1;
36+
37+
// Instantiates a client
38+
const client = new PredictionServiceClient();
39+
40+
async function batchPredict() {
41+
// Construct request
42+
const request = {
43+
name: client.modelPath(projectId, location, modelId),
44+
inputConfig: {
45+
gcsSource: {
46+
inputUris: [inputUri],
47+
},
48+
},
49+
outputConfig: {
50+
gcsDestination: {
51+
outputUriPrefix: outputUri,
52+
},
53+
},
54+
};
55+
56+
const [operation] = await client.batchPredict(request);
57+
58+
console.log(`Waiting for operation to complete...`);
59+
// Wait for operation to complete.
60+
const [response] = await operation.promise();
61+
console.log(
62+
`Batch Prediction results saved to Cloud Storage bucket. ${response}`
63+
);
64+
}
65+
66+
batchPredict();
67+
// [END automl_batch_predict_beta]
68+
}
69+
70+
main(...process.argv.slice(2));
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1beta1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const BATCH_PREDICT_REGION_TAG = 'beta/batch_predict';
26+
const LOCATION = 'us-central1';
27+
const MODEL_ID = 'TEN0000000000000000000';
28+
29+
describe('Automl Batch Predict Test', () => {
30+
const client = new AutoMlClient();
31+
32+
it('should batch predict', async () => {
33+
// As batch prediction can take a long time, instead try to batch predict
34+
// on a nonexistent model and confirm that the model was not found, but
35+
// other elements of the request were valid.
36+
const projectId = await client.getProjectId();
37+
const inputUri = `gs://${projectId}-lcm/entity_extraction/input.jsonl`;
38+
const outputUri = `gs://${projectId}-lcm/TEST_BATCH_PREDICT/`;
39+
40+
const args = [BATCH_PREDICT_REGION_TAG, projectId, LOCATION, MODEL_ID, inputUri, outputUri];
41+
const output = cp.spawnSync('node', args, {encoding: 'utf8'});
42+
43+
assert.match(output.stderr, /does not exist/);
44+
});
45+
});

0 commit comments

Comments
 (0)