Skip to content

Commit

Permalink
docs(samples): Translate with automl model sample (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
leahecole authored and Ace Nassri committed Nov 17, 2022
1 parent 2ea4ece commit 71efeec
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"test": "mocha --recursive --timeout 90000"
},
"dependencies": {
"@google-cloud/translate": "^3.0.1",
"@google-cloud/automl": "^0.2.0",
"@google-cloud/translate": "^3.0.1",
"yargs": "^13.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const REGION_TAG = 'translate_translate_text_with_model_beta';

describe.skip(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const modelId = 'TRL123456789'; //TODO: Create model that can be used for testing
const input = 'Tell me how this ends';

it('should translate text with an automl model in project', async () => {
const projectId = await translationClient.getProjectId();
const output = await execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${modelId} ${input}`
);
assert.match(output, /Translated Content: これがどのように終わるか教えて/);
});
});
62 changes: 62 additions & 0 deletions translate/v3beta1/translate_translate_text_with_model_beta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'model-id',
text = 'text to translate'
) {
// [START translate_text_with_model_beta]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const modelId = 'YOUR_MODEL_ID';

// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const automl = require('@google-cloud/automl');

// Instantiates a client
const translationClient = new TranslationServiceClient();
const autoMLClient = new automl.AutoMlClient();
async function translateTextWithModel() {
const model = autoMLClient.modelPath(projectId, location, modelId);
// Construct request
const request = {
parent: translationClient.locationPath(projectId, location),
contents: [text],
mimeType: 'text/plain', // mime types: text/plain, text/html
sourceLanguageCode: 'en-US',
targetLanguageCode: 'ja',
model: model,
};

// Run request
const [response] = await translationClient.translateText(request);

for (const translation of response.translations) {
console.log(`Translated Content: ${translation.translatedText}`);
}
}

translateTextWithModel();
// [END translate_text_with_model_beta]
}

main(...process.argv.slice(2));

0 comments on commit 71efeec

Please sign in to comment.