1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2019 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # DO NOT EDIT! This is a generated sample ("LongRunningPromise", "translate_v3_batch_translate_text_with_glossary_and_model")
18
+
19
+ # To install the latest published package dependency, execute the following:
20
+ # pip install google-cloud-translate
21
+
22
+ # sample-metadata
23
+ # title: Batch Translate with Glossary and Model
24
+ # description: Batch translate text with Glossary using AutoML Translation model
25
+ # usage: python3 translate_v3_batch_translate_text_with_glossary_and_model.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--target_language en] [--source_language de] [--model_id "{your-model-id}"] [--glossary_id "{your-glossary-id}"]
26
+
27
+ # [START translate_v3_batch_translate_text_with_glossary_and_model]
28
+ from google .cloud import translate
29
+
30
+ def sample_batch_translate_text_with_glossary_and_model (
31
+ input_uri ,
32
+ output_uri ,
33
+ project_id ,
34
+ location ,
35
+ target_language ,
36
+ source_language ,
37
+ model_id ,
38
+ glossary_id
39
+ ):
40
+ """
41
+ Batch translate text with Glossary and Translation model
42
+ """
43
+
44
+ client = translate .TranslationServiceClient ()
45
+
46
+ # TODO(developer): Uncomment and set the following variables
47
+ # input_uri = 'gs://cloud-samples-data/text.txt'
48
+ # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'
49
+ # project = '[Google Cloud Project ID]'
50
+ # location = 'us-central1'
51
+ # target_language = 'en'
52
+ # source_language = 'de'
53
+ # model_id = '{your-model-id}'
54
+ # glossary_id = '[YOUR_GLOSSARY_ID]'
55
+ target_language_codes = [target_language ]
56
+ gcs_source = {"input_uri" : input_uri }
57
+
58
+ # Optional. Can be "text/plain" or "text/html".
59
+ mime_type = "text/plain"
60
+ input_configs_element = {"gcs_source" : gcs_source , "mime_type" : mime_type }
61
+ input_configs = [input_configs_element ]
62
+ gcs_destination = {"output_uri_prefix" : output_uri }
63
+ output_config = {"gcs_destination" : gcs_destination }
64
+ parent = client .location_path (project_id , location )
65
+ model_path = 'projects/{}/locations/{}/models/{}' .format (project_id , 'us-central1' , model_id )
66
+ models = {target_language : model_path }
67
+
68
+ glossary_path = client .glossary_path (
69
+ project_id ,
70
+ 'us-central1' , # The location of the glossary
71
+ glossary_id )
72
+
73
+ glossary_config = translate .types .TranslateTextGlossaryConfig (
74
+ glossary = glossary_path )
75
+ glossaries = {"ja" : glossary_config } #target lang as key
76
+
77
+ operation = client .batch_translate_text (
78
+ parent = parent ,
79
+ source_language_code = source_language ,
80
+ target_language_codes = target_language_codes ,
81
+ input_configs = input_configs ,
82
+ output_config = output_config ,
83
+ models = models ,
84
+ glossaries = glossaries
85
+ )
86
+
87
+ print (u"Waiting for operation to complete..." )
88
+ response = operation .result ()
89
+
90
+ # Display the translation for each input text provided
91
+ print (u"Total Characters: {}" .format (response .total_characters ))
92
+ print (u"Translated Characters: {}" .format (response .translated_characters ))
93
+
94
+
95
+ # [END translate_v3_batch_translate_text_with_glossary_and_model]
96
+
97
+
98
+ def main ():
99
+ import argparse
100
+
101
+ parser = argparse .ArgumentParser ()
102
+ parser .add_argument (
103
+ "--input_uri" , type = str , default = "gs://cloud-samples-data/text.txt"
104
+ )
105
+ parser .add_argument (
106
+ "--output_uri" , type = str , default = "gs://YOUR_BUCKET_ID/path_to_store_results/"
107
+ )
108
+ parser .add_argument ("--project_id" , type = str , default = "[Google Cloud Project ID]" )
109
+ parser .add_argument ("--location" , type = str , default = "us-central1" )
110
+ parser .add_argument ("--target_language" , type = str , default = "en" )
111
+ parser .add_argument ("--source_language" , type = str , default = "de" )
112
+ parser .add_argument (
113
+ "--model_id" ,
114
+ type = str ,
115
+ default = "{your-model-id}"
116
+ )
117
+ parser .add_argument (
118
+ "--glossary_id" ,
119
+ type = str ,
120
+ default = "[YOUR_GLOSSARY_ID]" ,
121
+ )
122
+ args = parser .parse_args ()
123
+
124
+ sample_batch_translate_text_with_glossary_and_model (
125
+ args .input_uri ,
126
+ args .output_uri ,
127
+ args .project_id ,
128
+ args .location ,
129
+ args .target_language ,
130
+ args .source_language ,
131
+ args .model_id ,
132
+ args .glossary_id
133
+ )
134
+
135
+
136
+ if __name__ == "__main__" :
137
+ main ()
0 commit comments