|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +# [START translate_v3_batch_translate_text_with_model] |
| 17 | +from google.cloud import translate |
| 18 | + |
| 19 | + |
| 20 | +def batch_translate_text_with_model( |
| 21 | + input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", |
| 22 | + output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", |
| 23 | + project_id="YOUR_PROJECT_ID", |
| 24 | + model_id="YOUR_MODEL_ID", |
| 25 | +): |
| 26 | + """Batch translate text using Translation model. |
| 27 | + Model can be AutoML or General[built-in] model. """ |
| 28 | + |
| 29 | + client = translate.TranslationServiceClient() |
| 30 | + |
| 31 | + # Supported file types: https://cloud.google.com/translate/docs/supported-formats |
| 32 | + gcs_source = {"input_uri": input_uri} |
| 33 | + location = "us-central1" |
| 34 | + |
| 35 | + input_configs_element = { |
| 36 | + "gcs_source": gcs_source, |
| 37 | + "mime_type": "text/plain" # Can be "text/plain" or "text/html". |
| 38 | + } |
| 39 | + gcs_destination = {"output_uri_prefix": output_uri} |
| 40 | + output_config = {"gcs_destination": gcs_destination} |
| 41 | + parent = client.location_path(project_id, location) |
| 42 | + |
| 43 | + model_path = "projects/{}/locations/{}/models/{}".format( |
| 44 | + project_id, location, model_id # The location of AutoML model. |
| 45 | + ) |
| 46 | + |
| 47 | + # Supported language codes: https://cloud.google.com/translate/docs/languages |
| 48 | + models = {"ja": model_path} # takes a target lang as key. |
| 49 | + |
| 50 | + operation = client.batch_translate_text( |
| 51 | + parent=parent, |
| 52 | + source_language_code="en", |
| 53 | + target_language_codes=["ja"], # Up to 10 language codes here. |
| 54 | + input_configs=[input_configs_element], |
| 55 | + output_config=output_config, |
| 56 | + models=models, |
| 57 | + ) |
| 58 | + |
| 59 | + print(u"Waiting for operation to complete...") |
| 60 | + response = operation.result() |
| 61 | + |
| 62 | + # Display the translation for each input text provided. |
| 63 | + print(u"Total Characters: {}".format(response.total_characters)) |
| 64 | + print(u"Translated Characters: {}".format(response.translated_characters)) |
| 65 | + |
| 66 | + |
| 67 | +# [END translate_v3_batch_translate_text_with_model] |
0 commit comments