1
- # -*- coding: utf-8 -*-
2
- #
3
1
# Copyright 2019 Google LLC
4
2
#
5
3
# Licensed under the Apache License, Version 2.0 (the "License");
6
4
# you may not use this file except in compliance with the License.
7
5
# You may obtain a copy of the License at
8
6
#
9
- # https ://www.apache.org/licenses/LICENSE-2.0
7
+ # http ://www.apache.org/licenses/LICENSE-2.0
10
8
#
11
9
# Unless required by applicable law or agreed to in writing, software
12
10
# distributed under the License is distributed on an "AS IS" BASIS,
13
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
12
# See the License for the specific language governing permissions and
15
13
# limitations under the License.
16
14
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
15
# [START translate_v3_batch_translate_text_with_glossary_and_model]
28
16
from google .cloud import translate
29
17
30
18
31
- def sample_batch_translate_text_with_glossary_and_model (
32
- input_uri ,
33
- output_uri ,
34
- project_id ,
35
- location ,
36
- target_language ,
37
- source_language ,
38
- model_id ,
39
- glossary_id ,
19
+ def batch_translate_text_with_glossary_and_model (
20
+ input_uri = "gs://YOUR_BUCKET_ID/path/to/your/file.txt" ,
21
+ output_uri = "gs://YOUR_BUCKET_ID/path/to/save/results/" ,
22
+ project_id = "YOUR_PROJECT_ID" ,
23
+ model_id = "YOUR_MODEL_ID" ,
24
+ glossary_id = "YOUR_GLOSSARY_ID" ,
40
25
):
41
26
"""
42
27
Batch translate text with Glossary and Translation model
43
28
"""
44
29
45
30
client = translate .TranslationServiceClient ()
46
31
47
- # TODO(developer): Uncomment and set the following variables
48
- # input_uri = 'gs://cloud-samples-data/text.txt'
49
- # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'
50
- # project = '[Google Cloud Project ID]'
51
- # location = 'us-central1'
52
- # target_language = 'en'
53
- # source_language = 'de'
54
- # model_id = '{your-model-id}'
55
- # glossary_id = '[YOUR_GLOSSARY_ID]'
56
- target_language_codes = [target_language ]
32
+ # Supported language codes: https://cloud.google.com/translate/docs/languages
33
+ location = "us-central1"
34
+
35
+ target_language_codes = ["ja" ]
57
36
gcs_source = {"input_uri" : input_uri }
58
37
59
38
# Optional. Can be "text/plain" or "text/html".
@@ -66,7 +45,7 @@ def sample_batch_translate_text_with_glossary_and_model(
66
45
model_path = "projects/{}/locations/{}/models/{}" .format (
67
46
project_id , "us-central1" , model_id
68
47
)
69
- models = {target_language : model_path }
48
+ models = {"ja" : model_path }
70
49
71
50
glossary_path = client .glossary_path (
72
51
project_id , "us-central1" , glossary_id # The location of the glossary
@@ -96,39 +75,3 @@ def sample_batch_translate_text_with_glossary_and_model(
96
75
97
76
98
77
# [END translate_v3_batch_translate_text_with_glossary_and_model]
99
-
100
-
101
- def main ():
102
- import argparse
103
-
104
- parser = argparse .ArgumentParser ()
105
- parser .add_argument (
106
- "--input_uri" , type = str , default = "gs://cloud-samples-data/text.txt"
107
- )
108
- parser .add_argument (
109
- "--output_uri" , type = str , default = "gs://YOUR_BUCKET_ID/path_to_store_results/"
110
- )
111
- parser .add_argument ("--project_id" , type = str , default = "[Google Cloud Project ID]" )
112
- parser .add_argument ("--location" , type = str , default = "us-central1" )
113
- parser .add_argument ("--target_language" , type = str , default = "en" )
114
- parser .add_argument ("--source_language" , type = str , default = "de" )
115
- parser .add_argument ("--model_id" , type = str , default = "{your-model-id}" )
116
- parser .add_argument (
117
- "--glossary_id" , type = str , default = "[YOUR_GLOSSARY_ID]" ,
118
- )
119
- args = parser .parse_args ()
120
-
121
- sample_batch_translate_text_with_glossary_and_model (
122
- args .input_uri ,
123
- args .output_uri ,
124
- args .project_id ,
125
- args .location ,
126
- args .target_language ,
127
- args .source_language ,
128
- args .model_id ,
129
- args .glossary_id ,
130
- )
131
-
132
-
133
- if __name__ == "__main__" :
134
- main ()
0 commit comments