Skip to content

Commit 639e033

Browse files
authored
docs: add translate ga samples (#296)
* docs: add translate ga samples * update package.json * add missing file * Add license headers
1 parent 047ac64 commit 639e033

7 files changed

+336
-0
lines changed

automl/resources/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tell me how this ends
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2019 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 {AutoMlClient} = require('@google-cloud/automl').v1;
19+
20+
const cp = require('child_process');
21+
const uuid = require('uuid');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const CREATE_DATASET_REGION_TAG = 'translate_create_dataset';
26+
const LOCATION = 'us-central1';
27+
28+
describe('Automl Translate Create Dataset Test', () => {
29+
const client = new AutoMlClient();
30+
let datasetId;
31+
32+
it('should create a dataset', async () => {
33+
const projectId = await client.getProjectId();
34+
const displayName = `test_${uuid
35+
.v4()
36+
.replace(/-/g, '_')
37+
.substring(0, 26)}`;
38+
39+
// create
40+
const create_output = execSync(
41+
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}`
42+
);
43+
assert.match(create_output, /Dataset id:/);
44+
45+
datasetId = create_output.split('Dataset id: ')[1].split('\n')[0];
46+
});
47+
48+
after('delete created dataset', async () => {
49+
const projectId = await client.getProjectId();
50+
const request = {
51+
name: client.datasetPath(projectId, LOCATION, datasetId),
52+
};
53+
const [operation] = await client.deleteDataset(request);
54+
await operation.promise();
55+
});
56+
});
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2019 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 {AutoMlClient} = require('@google-cloud/automl').v1;
19+
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const CREATE_MODEL_REGION_TAG = 'translate_create_model';
25+
const LOCATION = 'us-central1';
26+
const DATASET_ID = 'TRL8522556519449886720';
27+
28+
describe('Automl Translate Create Model Tests', () => {
29+
const client = new AutoMlClient();
30+
let operationId;
31+
32+
it('should create a model', async () => {
33+
const projectId = await client.getProjectId();
34+
const create_output = execSync(
35+
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} translation_test_create_model`
36+
);
37+
38+
assert.match(create_output, /Training started/);
39+
40+
operationId = create_output
41+
.split('Training operation name: ')[1]
42+
.split('\n')[0];
43+
});
44+
45+
after('cancel model training', async () => {
46+
await client.operationsClient.cancelOperation({name: operationId});
47+
});
48+
});

automl/test/translate_predict.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019 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 {AutoMlClient} = require('@google-cloud/automl').v1;
19+
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const MODEL_ID = 'TRL1218052175389786112';
25+
const PREDICT_REGION_TAG = 'translate_predict';
26+
const LOCATION = 'us-central1';
27+
28+
describe('Automl Translate Predict Test', () => {
29+
const client = new AutoMlClient();
30+
31+
it('should predict', async () => {
32+
const projectId = await client.getProjectId();
33+
34+
const list_output = execSync(
35+
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} resources/input.txt`
36+
);
37+
assert.match(list_output, /Translated content:/);
38+
});
39+
});

automl/translate_create_dataset.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2019 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+
displayName = 'YOUR_DISPLAY_NAME'
21+
) {
22+
// [START automl_translate_create_dataset]
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 displayName = 'YOUR_DISPLAY_NAME';
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 createDataset() {
37+
// Construct request
38+
const request = {
39+
parent: client.locationPath(projectId, location),
40+
dataset: {
41+
displayName: displayName,
42+
translationDatasetMetadata: {
43+
sourceLanguageCode: 'en',
44+
targetLanguageCode: 'ja',
45+
},
46+
},
47+
};
48+
49+
// Create dataset
50+
const [operation] = await client.createDataset(request);
51+
52+
// Wait for operation to complete.
53+
const [response] = await operation.promise();
54+
55+
console.log(`Dataset name: ${response.name}`);
56+
console.log(`
57+
Dataset id: ${
58+
response.name
59+
.split('/')
60+
[response.name.split('/').length - 1].split('\n')[0]
61+
}`);
62+
}
63+
64+
createDataset();
65+
// [END automl_translate_create_dataset]
66+
}
67+
68+
main(...process.argv.slice(2));

automl/translate_create_model.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2019 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+
datasetId = 'YOUR_DATASET_ID',
21+
displayName = 'YOUR_DISPLAY_NAME'
22+
) {
23+
// [START automl_translate_create_model]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const location = 'us-central1';
29+
// const dataset_id = 'YOUR_DATASET_ID';
30+
// const displayName = 'YOUR_DISPLAY_NAME';
31+
32+
// Imports the Google Cloud AutoML library
33+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
34+
35+
// Instantiates a client
36+
const client = new AutoMlClient();
37+
38+
async function createModel() {
39+
// Construct request
40+
const request = {
41+
parent: client.locationPath(projectId, location),
42+
model: {
43+
displayName: displayName,
44+
datasetId: datasetId,
45+
translationModelMetadata: {}, // Leave unset, to use the default base model
46+
},
47+
};
48+
49+
// Don't wait for the LRO
50+
const [operation] = await client.createModel(request);
51+
console.log(`Training started...`);
52+
console.log(`Training operation name: ${operation.name}`);
53+
}
54+
55+
createModel();
56+
// [END automl_translate_create_model]
57+
}
58+
59+
main(...process.argv.slice(2));

automl/translate_predict.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2019 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+
filePath = 'path_to_local_file.txt'
22+
) {
23+
// [START automl_translate_predict]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const location = 'us-central1';
29+
// const modelId = 'YOUR_MODEL_ID';
30+
// const filePath = 'path_to_local_file.txt';
31+
32+
// Imports the Google Cloud AutoML library
33+
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;
34+
const fs = require(`fs`);
35+
36+
// Instantiates a client
37+
const client = new PredictionServiceClient();
38+
39+
// Read the file content for translation.
40+
const content = fs.readFileSync(filePath, `utf8`);
41+
42+
async function predict() {
43+
// Construct request
44+
const request = {
45+
name: client.modelPath(projectId, location, modelId),
46+
payload: {
47+
textSnippet: {
48+
content: content,
49+
},
50+
},
51+
};
52+
53+
const [response] = await client.predict(request);
54+
55+
console.log(
56+
`Translated content: `,
57+
response.payload[0].translation.translatedContent.content
58+
);
59+
}
60+
61+
predict();
62+
// [END automl_translate_predict]
63+
}
64+
65+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)