Skip to content

Commit 37cdd4f

Browse files
telpiriongcf-owl-bot[bot]
authored andcommitted
samples: adds batch prediction samples for text (#119)
* samples: adds batch prediction samples for text * samples: added tests * 🦉 Updates from OwlBot * fix: lint * fix: lint * fix: docs test Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d692b1c commit 37cdd4f

6 files changed

+501
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(
20+
batchPredictionDisplayName,
21+
modelId,
22+
gcsSourceUri,
23+
gcsDestinationOutputUriPrefix,
24+
project,
25+
location = 'us-central1'
26+
) {
27+
// [START aiplatform_create_batch_prediction_job_text_classification]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.\
30+
* (Not necessary if passing values as arguments)
31+
*/
32+
33+
// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
36+
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
37+
// eg. "gs://<your-gcs-bucket>/destination_path"
38+
// const project = 'YOUR_PROJECT_ID';
39+
// const location = 'YOUR_PROJECT_LOCATION';
40+
41+
// Imports the Google Cloud Job Service Client library
42+
const {JobServiceClient} = require('@google-cloud/aiplatform').v1;
43+
44+
// Specifies the location of the api endpoint
45+
const clientOptions = {
46+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
47+
};
48+
49+
// Instantiates a client
50+
const jobServiceClient = new JobServiceClient(clientOptions);
51+
52+
async function createBatchPredictionJobTextClassification() {
53+
// Configure the parent resource
54+
const parent = `projects/${project}/locations/${location}`;
55+
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;
56+
57+
const inputConfig = {
58+
instancesFormat: 'jsonl',
59+
gcsSource: {uris: [gcsSourceUri]},
60+
};
61+
const outputConfig = {
62+
predictionsFormat: 'jsonl',
63+
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
64+
};
65+
const batchPredictionJob = {
66+
displayName: batchPredictionDisplayName,
67+
model: modelName,
68+
inputConfig,
69+
outputConfig,
70+
};
71+
const request = {
72+
parent,
73+
batchPredictionJob,
74+
};
75+
76+
// Create batch prediction job request
77+
const [response] = await jobServiceClient.createBatchPredictionJob(request);
78+
79+
console.log('Create batch prediction job text classification response');
80+
console.log(`Name : ${response.name}`);
81+
console.log('Raw response:');
82+
console.log(JSON.stringify(response, null, 2));
83+
}
84+
createBatchPredictionJobTextClassification();
85+
// [END aiplatform_create_batch_prediction_job_text_classification]
86+
}
87+
88+
process.on('unhandledRejection', err => {
89+
console.error(err.message);
90+
process.exitCode = 1;
91+
});
92+
93+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(
20+
batchPredictionDisplayName,
21+
modelId,
22+
gcsSourceUri,
23+
gcsDestinationOutputUriPrefix,
24+
project,
25+
location = 'us-central1'
26+
) {
27+
// [START aiplatform_create_batch_prediction_job_text_entity_extraction]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.\
30+
* (Not necessary if passing values as arguments)
31+
*/
32+
33+
// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
36+
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
37+
// eg. "gs://<your-gcs-bucket>/destination_path"
38+
// const project = 'YOUR_PROJECT_ID';
39+
// const location = 'YOUR_PROJECT_LOCATION';
40+
41+
// Imports the Google Cloud Job Service Client library
42+
const {JobServiceClient} = require('@google-cloud/aiplatform').v1;
43+
44+
// Specifies the location of the api endpoint
45+
const clientOptions = {
46+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
47+
};
48+
49+
// Instantiates a client
50+
const jobServiceClient = new JobServiceClient(clientOptions);
51+
52+
async function createBatchPredictionJobTextEntityExtraction() {
53+
// Configure the parent resource
54+
const parent = `projects/${project}/locations/${location}`;
55+
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;
56+
57+
const inputConfig = {
58+
instancesFormat: 'jsonl',
59+
gcsSource: {uris: [gcsSourceUri]},
60+
};
61+
const outputConfig = {
62+
predictionsFormat: 'jsonl',
63+
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
64+
};
65+
const batchPredictionJob = {
66+
displayName: batchPredictionDisplayName,
67+
model: modelName,
68+
inputConfig,
69+
outputConfig,
70+
};
71+
const request = {
72+
parent,
73+
batchPredictionJob,
74+
};
75+
76+
// Create batch prediction job request
77+
const [response] = await jobServiceClient.createBatchPredictionJob(request);
78+
79+
console.log('Create batch prediction job text entity extraction response');
80+
console.log(`Name : ${response.name}`);
81+
console.log('Raw response:');
82+
console.log(JSON.stringify(response, null, 2));
83+
}
84+
createBatchPredictionJobTextEntityExtraction();
85+
// [END aiplatform_create_batch_prediction_job_text_entity_extraction]
86+
}
87+
88+
process.on('unhandledRejection', err => {
89+
console.error(err.message);
90+
process.exitCode = 1;
91+
});
92+
93+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(
20+
batchPredictionDisplayName,
21+
modelId,
22+
gcsSourceUri,
23+
gcsDestinationOutputUriPrefix,
24+
project,
25+
location = 'us-central1'
26+
) {
27+
// [START aiplatform_create_batch_prediction_job_text_sentiment_analysis]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.\
30+
* (Not necessary if passing values as arguments)
31+
*/
32+
33+
// const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME';
34+
// const modelId = 'YOUR_MODEL_ID';
35+
// const gcsSourceUri = 'YOUR_GCS_SOURCE_URI';
36+
// const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX';
37+
// eg. "gs://<your-gcs-bucket>/destination_path"
38+
// const project = 'YOUR_PROJECT_ID';
39+
// const location = 'YOUR_PROJECT_LOCATION';
40+
41+
// Imports the Google Cloud Job Service Client library
42+
const {JobServiceClient} = require('@google-cloud/aiplatform').v1;
43+
44+
// Specifies the location of the api endpoint
45+
const clientOptions = {
46+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
47+
};
48+
49+
// Instantiates a client
50+
const jobServiceClient = new JobServiceClient(clientOptions);
51+
52+
async function createBatchPredictionJobTextSentimentAnalysis() {
53+
// Configure the parent resource
54+
const parent = `projects/${project}/locations/${location}`;
55+
const modelName = `projects/${project}/locations/${location}/models/${modelId}`;
56+
57+
const inputConfig = {
58+
instancesFormat: 'jsonl',
59+
gcsSource: {uris: [gcsSourceUri]},
60+
};
61+
const outputConfig = {
62+
predictionsFormat: 'jsonl',
63+
gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix},
64+
};
65+
const batchPredictionJob = {
66+
displayName: batchPredictionDisplayName,
67+
model: modelName,
68+
inputConfig,
69+
outputConfig,
70+
};
71+
const request = {
72+
parent,
73+
batchPredictionJob,
74+
};
75+
76+
// Create batch prediction job request
77+
const [response] = await jobServiceClient.createBatchPredictionJob(request);
78+
79+
console.log('Create batch prediction job text sentiment analysis response');
80+
console.log(`Name : ${response.name}`);
81+
console.log('Raw response:');
82+
console.log(JSON.stringify(response, null, 2));
83+
}
84+
createBatchPredictionJobTextSentimentAnalysis();
85+
// [END aiplatform_create_batch_prediction_job_text_sentiment_analysis]
86+
}
87+
88+
process.on('unhandledRejection', err => {
89+
console.error(err.message);
90+
process.exitCode = 1;
91+
});
92+
93+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const {assert} = require('chai');
20+
const {after, describe, it} = require('mocha');
21+
const uuid = require('uuid').v4;
22+
const cp = require('child_process');
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const aiplatform = require('@google-cloud/aiplatform');
26+
const clientOptions = {
27+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
28+
};
29+
30+
const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions);
31+
32+
const batchPredictionDisplayName = `temp_create_batch_prediction_text_classification_test${uuid()}`;
33+
const modelId = '7827432074230366208';
34+
const gcsSourceUri =
35+
'gs://ucaip-samples-test-output/inputs/batch_predict_TCN/tcn_inputs.jsonl';
36+
const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/';
37+
const location = 'us-central1';
38+
const project = process.env.CAIP_PROJECT_ID;
39+
40+
let batchPredictionJobId;
41+
42+
describe('AI platform create batch prediction job text classification', () => {
43+
it('should create a text classification batch prediction job', async () => {
44+
const stdout = execSync(
45+
`node ./create-batch-prediction-job-text-classification.js ${batchPredictionDisplayName} ${modelId} ${gcsSourceUri} ${gcsDestinationOutputUriPrefix} ${project} ${location}`
46+
);
47+
assert.match(
48+
stdout,
49+
/Create batch prediction job text classification response/
50+
);
51+
batchPredictionJobId = stdout
52+
.split('/locations/us-central1/batchPredictionJobs/')[1]
53+
.split('\n')[0];
54+
});
55+
after('should cancel delete the batch prediction job', async () => {
56+
const name = jobServiceClient.batchPredictionJobPath(
57+
project,
58+
location,
59+
batchPredictionJobId
60+
);
61+
62+
const cancelRequest = {
63+
name,
64+
};
65+
66+
jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => {
67+
const deleteRequest = {
68+
name,
69+
};
70+
71+
return jobServiceClient.deleteBatchPredictionJob(deleteRequest);
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)