Skip to content

Commit f1dde39

Browse files
nnegreybcoe
andauthored
docs: add base samples for automl ga (#293)
* docs: add base samples for automl ga * update package.json * lint fix * License header update * Fix cleanup on GCS prefix * Lint Co-authored-by: Benjamin E. Coe <[email protected]>
1 parent c1430ea commit f1dde39

6 files changed

+337
-0
lines changed

automl/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]
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`).v1;
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]
68+
}
69+
70+
main(...process.argv.slice(2));

automl/get_operation_status.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
operationId = 'YOUR_OPERATION_ID'
21+
) {
22+
// [START automl_get_operation_status]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const operationId = 'YOUR_OPERATION_ID';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function getOperationStatus() {
37+
// Construct request
38+
const request = {
39+
name: `projects/${projectId}/locations/${location}/operations/${operationId}`,
40+
};
41+
42+
const [response] = await client.operationsClient.getOperation(request);
43+
44+
console.log(`Name: ${response.name}`);
45+
console.log(`Operation details:`);
46+
console.log(`${response}`);
47+
}
48+
49+
getOperationStatus();
50+
// [END automl_get_operation_status]
51+
}
52+
53+
main(...process.argv.slice(2));

automl/list_operation_status.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') {
18+
// [START automl_list_operation_status]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'YOUR_PROJECT_ID';
23+
// const location = 'us-central1';
24+
25+
// Imports the Google Cloud AutoML library
26+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
27+
28+
// Instantiates a client
29+
const client = new AutoMlClient();
30+
31+
async function listOperationStatus() {
32+
// Construct request
33+
const request = {
34+
name: client.locationPath(projectId, location),
35+
filter: '',
36+
};
37+
38+
const [response] = await client.operationsClient.listOperations(request);
39+
40+
console.log(`List of operation status:`);
41+
for (const operation of response) {
42+
console.log(`Name: ${operation.name}`);
43+
console.log(`Operation details:`);
44+
console.log(`${operation}`);
45+
}
46+
}
47+
48+
listOperationStatus();
49+
// [END automl_list_operation_status]
50+
}
51+
52+
main(...process.argv.slice(2));

automl/test/batch_predict.test.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 {after, before, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
const {Storage} = require('@google-cloud/storage');
21+
22+
const cp = require('child_process');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const BATCH_PREDICT_REGION_TAG = 'batch_predict';
27+
const LOCATION = 'us-central1';
28+
const MODEL_ID = 'TEN2238627664384491520';
29+
const PREFIX = 'TEST_BATCH_PREDICT';
30+
31+
describe('Automl Batch Predict Test', () => {
32+
const client = new AutoMlClient();
33+
34+
before('should verify the model is deployed', async () => {
35+
const projectId = await client.getProjectId();
36+
const request = {
37+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
38+
};
39+
40+
const [response] = await client.getModel(request);
41+
if (response.deploymentState === 'UNDEPLOYED') {
42+
const request = {
43+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
44+
};
45+
46+
const [operation] = await client.deployModel(request);
47+
48+
// Wait for operation to complete.
49+
await operation.promise();
50+
}
51+
});
52+
53+
it('should batch predict', async () => {
54+
const projectId = await client.getProjectId();
55+
const inputUri = `gs://${projectId}-lcm/entity_extraction/input.jsonl`;
56+
const outputUri = `gs://${projectId}-lcm/${PREFIX}/`;
57+
58+
const batchPredictOutput = execSync(
59+
`node ${BATCH_PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${inputUri} ${outputUri}`
60+
);
61+
assert.match(
62+
batchPredictOutput,
63+
/Batch Prediction results saved to Cloud Storage bucket/
64+
);
65+
});
66+
67+
after('delete created files', async () => {
68+
const projectId = await client.getProjectId();
69+
const storageClient = new Storage();
70+
const options = {
71+
prefix: PREFIX,
72+
};
73+
const [files] = await storageClient
74+
.bucket(`gs://${projectId}-lcm`)
75+
.getFiles(options);
76+
files.forEach(file => {
77+
storageClient
78+
.bucket(`gs://${projectId}-lcm`)
79+
.file(file.name)
80+
.delete();
81+
});
82+
});
83+
});
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const GET_OPERATION_STATUS_REGION_TAG = 'get_operation_status';
26+
const LOCATION = 'us-central1';
27+
const OPERATION_ID = 'TRL5980949629938696192';
28+
29+
describe('Automl Get Operation Status Tests', () => {
30+
const client = new AutoMlClient();
31+
32+
it('should get operation status', async () => {
33+
const projectId = await client.getProjectId();
34+
35+
const get_output = execSync(
36+
`node ${GET_OPERATION_STATUS_REGION_TAG}.js ${projectId} ${LOCATION} ${OPERATION_ID}`
37+
);
38+
assert.match(get_output, /Operation details/);
39+
});
40+
});
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const LIST_OPERATION_STATUS_REGION_TAG = 'list_operation_status';
26+
const LOCATION = 'us-central1';
27+
28+
describe('Automl List Operation Status Tests', () => {
29+
const client = new AutoMlClient();
30+
31+
it('should list operation status', async () => {
32+
const projectId = await client.getProjectId();
33+
34+
const list_output = execSync(
35+
`node ${LIST_OPERATION_STATUS_REGION_TAG}.js ${projectId} ${LOCATION} `
36+
);
37+
assert.match(list_output, /Operation details/);
38+
});
39+
});

0 commit comments

Comments
 (0)