1- # -*- coding: utf-8 -*-
2- #
31# Copyright 2019 Google LLC
42#
53# Licensed under the Apache License, Version 2.0 (the "License");
64# you may not use this file except in compliance with the License.
75# You may obtain a copy of the License at
86#
9- # https ://www.apache.org/licenses/LICENSE-2.0
7+ # http ://www.apache.org/licenses/LICENSE-2.0
108#
119# Unless required by applicable law or agreed to in writing, software
1210# distributed under the License is distributed on an "AS IS" BASIS,
1311# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1412# See the License for the specific language governing permissions and
1513# limitations under the License.
1614
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-
2715# [START translate_v3_batch_translate_text_with_glossary_and_model]
2816from google .cloud import translate
2917
3018
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" ,
4025):
4126 """
4227 Batch translate text with Glossary and Translation model
4328 """
4429
4530 client = translate .TranslationServiceClient ()
4631
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" ]
5736 gcs_source = {"input_uri" : input_uri }
5837
5938 # Optional. Can be "text/plain" or "text/html".
@@ -66,7 +45,7 @@ def sample_batch_translate_text_with_glossary_and_model(
6645 model_path = "projects/{}/locations/{}/models/{}" .format (
6746 project_id , "us-central1" , model_id
6847 )
69- models = {target_language : model_path }
48+ models = {"ja" : model_path }
7049
7150 glossary_path = client .glossary_path (
7251 project_id , "us-central1" , glossary_id # The location of the glossary
@@ -96,39 +75,3 @@ def sample_batch_translate_text_with_glossary_and_model(
9675
9776
9877# [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