Skip to content

Commit e2f39d9

Browse files
nnegreyanguillanneuf
authored andcommitted
samples: translate: add v3 samples for translate text with model (#1939)
* translate: add v3 samples for translate text with model * Add clarifying comments that explain chunks of the code
1 parent b413551 commit e2f39d9

File tree

7 files changed

+384
-0
lines changed

7 files changed

+384
-0
lines changed

translate/snippets/src/main/java/com/example/translate/BatchTranslateTextWithGlossaryAndModel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,28 @@ public static void batchTranslateTextWithGlossaryAndModel(
7171
String location = "us-central1";
7272
LocationName parent = LocationName.of(projectId, location);
7373

74+
// Configure the source of the file from a GCS bucket
7475
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
7576
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
7677
InputConfig inputConfig =
7778
InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
7879

80+
// Configure where to store the output in a GCS bucket
7981
GcsDestination gcsDestination =
8082
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
8183
OutputConfig outputConfig =
8284
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
8385

86+
// Configure the glossary used in the request
8487
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
8588
TranslateTextGlossaryConfig glossaryConfig =
8689
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
8790

91+
// Configure the model used in the request
8892
String modelPath =
8993
String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
9094

95+
// Build the request that will be sent to the API
9196
BatchTranslateTextRequest request =
9297
BatchTranslateTextRequest.newBuilder()
9398
.setParent(parent.toString())
@@ -99,6 +104,7 @@ public static void batchTranslateTextWithGlossaryAndModel(
99104
.putModels(targetLanguage, modelPath)
100105
.build();
101106

107+
// Start an asynchronous request
102108
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future =
103109
client.batchTranslateTextAsync(request);
104110

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
package com.example.translate;
18+
19+
// [START translate_v3_batch_translate_text_with_model]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.translate.v3.BatchTranslateMetadata;
22+
import com.google.cloud.translate.v3.BatchTranslateResponse;
23+
import com.google.cloud.translate.v3.BatchTranslateTextRequest;
24+
import com.google.cloud.translate.v3.GcsDestination;
25+
import com.google.cloud.translate.v3.GcsSource;
26+
import com.google.cloud.translate.v3.InputConfig;
27+
import com.google.cloud.translate.v3.LocationName;
28+
import com.google.cloud.translate.v3.OutputConfig;
29+
import com.google.cloud.translate.v3.TranslationServiceClient;
30+
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
33+
34+
public class BatchTranslateTextWithModel {
35+
36+
public static void batchTranslateTextWithModel()
37+
throws InterruptedException, ExecutionException, IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String projectId = "YOUR-PROJECT-ID";
40+
// Supported Languages: https://cloud.google.com/translate/docs/languages
41+
String sourceLanguage = "your-source-language";
42+
String targetLanguage = "your-target-language";
43+
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt";
44+
String outputUri = "gs://your-gcs-bucket/path/to/results/";
45+
String modelId = "YOUR-MODEL-ID";
46+
batchTranslateTextWithModel(
47+
projectId, sourceLanguage, targetLanguage, inputUri, outputUri, modelId);
48+
}
49+
50+
// Batch translate text using AutoML Translation model
51+
public static void batchTranslateTextWithModel(
52+
String projectId,
53+
String sourceLanguage,
54+
String targetLanguage,
55+
String inputUri,
56+
String outputUri,
57+
String modelId)
58+
throws IOException, ExecutionException, InterruptedException {
59+
60+
// Initialize client that will be used to send requests. This client only needs to be created
61+
// once, and can be reused for multiple requests. After completing all of your requests, call
62+
// the "close" method on the client to safely clean up any remaining background resources.
63+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
64+
// Supported Locations: `global`, [glossary location], or [model location]
65+
// Glossaries must be hosted in `us-central1`
66+
// Custom Models must use the same location as your model. (us-central1)
67+
String location = "us-central1";
68+
LocationName parent = LocationName.of(projectId, location);
69+
70+
// Configure the source of the file from a GCS bucket
71+
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
72+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
73+
InputConfig inputConfig =
74+
InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
75+
76+
// Configure where to store the output in a GCS bucket
77+
GcsDestination gcsDestination =
78+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
79+
OutputConfig outputConfig =
80+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
81+
82+
// Configure the model used in the request
83+
String modelPath =
84+
String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
85+
86+
// Build the request that will be sent to the API
87+
BatchTranslateTextRequest request =
88+
BatchTranslateTextRequest.newBuilder()
89+
.setParent(parent.toString())
90+
.setSourceLanguageCode(sourceLanguage)
91+
.addTargetLanguageCodes(targetLanguage)
92+
.addInputConfigs(inputConfig)
93+
.setOutputConfig(outputConfig)
94+
.putModels(targetLanguage, modelPath)
95+
.build();
96+
97+
// Start an asynchronous request
98+
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future =
99+
client.batchTranslateTextAsync(request);
100+
101+
System.out.println("Waiting for operation to complete...");
102+
BatchTranslateResponse response = future.get();
103+
// Display the translation for each input text provided
104+
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
105+
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
106+
}
107+
}
108+
}
109+
// [END translate_v3_batch_translate_text_with_model]

translate/snippets/src/main/java/com/example/translate/CreateGlossary.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static void createGlossary(
6565
Glossary.LanguageCodesSet languageCodesSet =
6666
Glossary.LanguageCodesSet.newBuilder().addAllLanguageCodes(languageCodes).build();
6767

68+
// Configure the source of the file from a GCS bucket
6869
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
6970
GlossaryInputConfig inputConfig =
7071
GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
@@ -82,6 +83,7 @@ public static void createGlossary(
8283
.setGlossary(glossary)
8384
.build();
8485

86+
// Start an asynchronous request
8587
OperationFuture<Glossary, CreateGlossaryMetadata> future =
8688
client.createGlossaryAsync(request);
8789

translate/snippets/src/main/java/com/example/translate/DeleteGlossary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static void deleteGlossary(String projectId, String glossaryId)
5151
DeleteGlossaryRequest request =
5252
DeleteGlossaryRequest.newBuilder().setName(glossaryName.toString()).build();
5353

54+
// Start an asynchronous request
5455
OperationFuture<DeleteGlossaryResponse, DeleteGlossaryMetadata> future =
5556
client.deleteGlossaryAsync(request);
5657

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
package com.example.translate;
18+
19+
// [START translate_v3_translate_text_with_model]
20+
import com.google.cloud.translate.v3.LocationName;
21+
import com.google.cloud.translate.v3.TranslateTextRequest;
22+
import com.google.cloud.translate.v3.TranslateTextResponse;
23+
import com.google.cloud.translate.v3.Translation;
24+
import com.google.cloud.translate.v3.TranslationServiceClient;
25+
26+
import java.io.IOException;
27+
28+
public class TranslateTextWithModel {
29+
30+
public static void translateTextWithModel() throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "YOUR-PROJECT-ID";
33+
// Supported Languages: https://cloud.google.com/translate/docs/languages
34+
String sourceLanguage = "your-source-language";
35+
String targetLanguage = "your-target-language";
36+
String text = "your-text";
37+
String modelId = "YOUR-MODEL-ID";
38+
translateTextWithModel(projectId, sourceLanguage, targetLanguage, text, modelId);
39+
}
40+
41+
// Translating Text with Model
42+
public static void translateTextWithModel(
43+
String projectId, String sourceLanguage, String targetLanguage, String text, String modelId)
44+
throws IOException {
45+
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
50+
// Supported Locations: `global`, [glossary location], or [model location]
51+
// Glossaries must be hosted in `us-central1`
52+
// Custom Models must use the same location as your model. (us-central1)
53+
String location = "us-central1";
54+
LocationName parent = LocationName.of(projectId, location);
55+
String modelPath =
56+
String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
57+
58+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
59+
TranslateTextRequest request =
60+
TranslateTextRequest.newBuilder()
61+
.setParent(parent.toString())
62+
.setMimeType("text/plain")
63+
.setSourceLanguageCode(sourceLanguage)
64+
.setTargetLanguageCode(targetLanguage)
65+
.addContents(text)
66+
.setModel(modelPath)
67+
.build();
68+
69+
TranslateTextResponse response = client.translateText(request);
70+
71+
// Display the translation for each input text provided
72+
for (Translation translation : response.getTranslationsList()) {
73+
System.out.printf("Translated text: %s\n", translation.getTranslatedText());
74+
}
75+
}
76+
}
77+
}
78+
// [END translate_v3_translate_text_with_model]
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2020 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+
* http://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+
package com.example.translate;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.api.gax.paging.Page;
23+
24+
import com.google.cloud.storage.Blob;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageOptions;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import java.util.concurrent.ExecutionException;
31+
32+
import org.junit.After;
33+
import org.junit.Before;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.JUnit4;
38+
39+
/** Tests for Batch Translate Text With Model sample. */
40+
@RunWith(JUnit4.class)
41+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
42+
public class BatchTranslateTextWithModelTests {
43+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
44+
private static final String INPUT_URI =
45+
"gs://cloud-samples-data/translation/custom_model_text.txt";
46+
private static final String MODEL_ID = "TRL2188848820815848149";
47+
48+
private ByteArrayOutputStream bout;
49+
private PrintStream out;
50+
51+
private static final void cleanUpBucket() {
52+
Storage storage = StorageOptions.getDefaultInstance().getService();
53+
Page<Blob> blobs =
54+
storage.list(
55+
PROJECT_ID,
56+
Storage.BlobListOption.currentDirectory(),
57+
Storage.BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/"));
58+
59+
deleteDirectory(storage, blobs);
60+
}
61+
62+
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
63+
for (Blob blob : blobs.iterateAll()) {
64+
System.out.println(blob.getBlobId());
65+
if (!blob.delete()) {
66+
Page<Blob> subBlobs =
67+
storage.list(
68+
PROJECT_ID,
69+
Storage.BlobListOption.currentDirectory(),
70+
Storage.BlobListOption.prefix(blob.getName()));
71+
72+
deleteDirectory(storage, subBlobs);
73+
}
74+
}
75+
}
76+
77+
private static void requireEnvVar(String varName) {
78+
assertNotNull(
79+
"Environment variable '%s' is required to perform these tests.".format(varName),
80+
System.getenv(varName));
81+
}
82+
83+
@BeforeClass
84+
public static void checkRequirements() {
85+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
86+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
87+
}
88+
89+
@Before
90+
public void setUp() {
91+
bout = new ByteArrayOutputStream();
92+
out = new PrintStream(bout);
93+
System.setOut(out);
94+
}
95+
96+
@After
97+
public void tearDown() {
98+
cleanUpBucket();
99+
System.setOut(null);
100+
}
101+
102+
@Test
103+
public void testBatchTranslateTextWithModel()
104+
throws InterruptedException, ExecutionException, IOException {
105+
BatchTranslateTextWithModel.batchTranslateTextWithModel(
106+
PROJECT_ID,
107+
"en",
108+
"ja",
109+
INPUT_URI,
110+
"gs://" + PROJECT_ID + "/BATCH_TRANSLATION_OUTPUT/",
111+
MODEL_ID);
112+
String got = bout.toString();
113+
assertThat(got).contains("Total Characters: 15");
114+
}
115+
}

0 commit comments

Comments
 (0)