Skip to content

Commit aa0f37b

Browse files
leahecoleAce Nassri
authored andcommitted
docs(samples): Translate with automl model sample (#238)
1 parent 156727f commit aa0f37b

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

translate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"test": "mocha --recursive --timeout 90000"
1212
},
1313
"dependencies": {
14-
"@google-cloud/translate": "^3.0.1",
1514
"@google-cloud/automl": "^0.2.0",
15+
"@google-cloud/translate": "^3.0.1",
1616
"yargs": "^13.0.0"
1717
},
1818
"devDependencies": {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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+
* http://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+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
20+
const cp = require('child_process');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
const REGION_TAG = 'translate_translate_text_with_model_beta';
24+
25+
describe.skip(REGION_TAG, () => {
26+
const translationClient = new TranslationServiceClient();
27+
const location = 'us-central1';
28+
const modelId = 'TRL123456789'; //TODO: Create model that can be used for testing
29+
const input = 'Tell me how this ends';
30+
31+
it('should translate text with an automl model in project', async () => {
32+
const projectId = await translationClient.getProjectId();
33+
const output = await execSync(
34+
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${modelId} ${input}`
35+
);
36+
assert.match(output, /Translated Content: /);
37+
});
38+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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+
* http://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 = 'model-id',
21+
text = 'text to translate'
22+
) {
23+
// [START translate_text_with_model_beta]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const location = 'global';
29+
// const modelId = 'YOUR_MODEL_ID';
30+
31+
// Imports the Google Cloud Translation library
32+
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
33+
const automl = require('@google-cloud/automl');
34+
35+
// Instantiates a client
36+
const translationClient = new TranslationServiceClient();
37+
const autoMLClient = new automl.AutoMlClient();
38+
async function translateTextWithModel() {
39+
const model = autoMLClient.modelPath(projectId, location, modelId);
40+
// Construct request
41+
const request = {
42+
parent: translationClient.locationPath(projectId, location),
43+
contents: [text],
44+
mimeType: 'text/plain', // mime types: text/plain, text/html
45+
sourceLanguageCode: 'en-US',
46+
targetLanguageCode: 'ja',
47+
model: model,
48+
};
49+
50+
// Run request
51+
const [response] = await translationClient.translateText(request);
52+
53+
for (const translation of response.translations) {
54+
console.log(`Translated Content: ${translation.translatedText}`);
55+
}
56+
}
57+
58+
translateTextWithModel();
59+
// [END translate_text_with_model_beta]
60+
}
61+
62+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)