Skip to content

Commit

Permalink
add: Translate Document & Batch Translate Document samples (#694)
Browse files Browse the repository at this point in the history
* translate document sample and test

Change-Id: Id0f943163f063e114aaec5703f3160a5b9c71485

* remove comment

Change-Id: I13ee4f8692c76416564d05485271a50929ff42cf

* lint

Change-Id: I3eb785d6a164748484726ad359a8ae7f8156051d

* batch translate document

Change-Id: Id36d46f363cb061c38ddf099e23836020757b9f2

* batch translate document

Change-Id: Ifbf6c3faec2352237f44d714c31360349b950efb

* update year

Change-Id: Icb80b2a660ee38de5cbbd09e497eeaf4d3532371

* refactor error handling

Change-Id: Ic7493f025e1c98b3c4e0dbc6a48919e8a2df309e

* refactor error handling

Change-Id: Ibe57fa98dbb060039d8b60684238294f3f4746f1
  • Loading branch information
aribray authored and Ace Nassri committed Nov 17, 2022
1 parent b161984 commit fcb9893
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
81 changes: 81 additions & 0 deletions translate/test/v3beta1/translate_batch_translate_document.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2021 Google LLC
//
// 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 {describe, it, before, after} = require('mocha');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const {Storage} = require('@google-cloud/storage');
const cp = require('child_process');
const uuid = require('uuid');

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

const REGION_TAG = 'translate_batch_translate_document';

describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const bucketUuid = uuid.v4();
const bucketName = `translation-${bucketUuid}/BATCH_OUTPUT/`;
const storage = new Storage();

before(async () => {
const projectId = await translationClient.getProjectId();

//Create bucket if needed
try {
await storage.createBucket(projectId, {
location: 'US',
storageClass: 'COLDLINE',
});
} catch (error) {
if (error.code !== 409) {
//if it's not a duplicate bucket error, let the user know
console.error(error);
throw error;
}
}
});

it('should batch translate the input documents', async function () {
this.retries(3);
const projectId = await translationClient.getProjectId();
const inputUri = 'gs://cloud-samples-data/translation/async_invoices/*';

const outputUri = `gs://${projectId}/${bucketName}`;
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
);
assert.match(output, /Total Pages: /);

this.timeout(500000);
});

// Delete the folder from GCS for cleanup
after(async () => {
const projectId = await translationClient.getProjectId();
const options = {
prefix: `translation-${bucketUuid}`,
};

const bucket = await storage.bucket(projectId);
const [files] = await bucket.getFiles(options);
const length = files.length;
if (length > 0) {
await Promise.all(files.map(file => file.delete()));
}
});
});
37 changes: 37 additions & 0 deletions translate/test/v3beta1/translate_translate_document.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 Google LLC
//
// 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 {describe, it} = require('mocha');
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_document';

describe(REGION_TAG, () => {
it('should translate the input document', async () => {
const translationClient = new TranslationServiceClient();
const projectId = await translationClient.getProjectId();
const location = 'global';
const inputUri = 'gs://cloud-samples-data/translation/fake_invoice.pdf';
const output = execSync(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${inputUri}`
);
assert.match(output, /Response: Mime Type/);
});
});
83 changes: 83 additions & 0 deletions translate/v3beta1/translate_batch_translate_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 Google LLC
//
// 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',
inputUri = 'gs://cloud-samples-data/translation/async_invoices/*',
outputUri = 'gs://YOUR_PROJECT_ID/translation/BATCH_DOCUMENT_TRANSLATION_OUTPUT/'
) {
// [START translate_batch_translate_document]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const inputUri = 'path_to_your_files';
// const outputUri = 'path_to_your_output_bucket';

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

// Instantiates a client
const translationClient = new TranslationServiceClient();

const documentInputConfig = {
gcsSource: {
inputUri: inputUri,
},
};

async function batchTranslateDocument() {
// Construct request
const request = {
parent: translationClient.locationPath(projectId, location),
documentInputConfig: documentInputConfig,
sourceLanguageCode: 'en-US',
targetLanguageCodes: ['sr-Latn'],
inputConfigs: [
{
gcsSource: {
inputUri: inputUri,
},
},
],
outputConfig: {
gcsDestination: {
outputUriPrefix: outputUri,
},
},
};

// Batch translate documents using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.batchTranslateDocument(request);

// Wait for operation to complete.
const [response] = await operation.promise();

console.log(`Total Pages: ${response.totalPages}`);
}

batchTranslateDocument();
// [END translate_batch_translate_document]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
67 changes: 67 additions & 0 deletions translate/v3beta1/translate_translate_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2021 Google LLC
//
// 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 = 'global',
inputUri = 'path_to_your_file'
) {
// [START translate_translate_document]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'global';
// const inputUri = 'path_to_your_file';

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

// Instantiates a client
const translationClient = new TranslationServiceClient();

const documentInputConfig = {
gcsSource: {
inputUri: inputUri,
},
};

async function translateDocument() {
// Construct request
const request = {
parent: translationClient.locationPath(projectId, location),
documentInputConfig: documentInputConfig,
sourceLanguageCode: 'en-US',
targetLanguageCode: 'sr-Latn',
};

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

console.log(
`Response: Mime Type - ${response.documentTranslation.mimeType}`
);
}

translateDocument();
// [END translate_translate_document]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));

0 comments on commit fcb9893

Please sign in to comment.