Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 1e030d4

Browse files
committed
docs: add w/ glossary and model
1 parent 6cf9742 commit 1e030d4

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2019 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+
import os
16+
import pytest
17+
import uuid
18+
import translate_v3_batch_translate_text_with_glossary_and_model
19+
import translate_v3_create_glossary
20+
import translate_v3_delete_glossary
21+
from google.cloud import storage
22+
23+
PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
24+
GLOSSARY_INPUT_URI = 'gs://cloud-samples-data/translation/glossary_ja.csv'
25+
MODEL_ID = 'TRL3128559826197068699'
26+
27+
@pytest.fixture(scope='session')
28+
def glossary():
29+
"""Get the ID of a glossary available to session (do not mutate/delete)."""
30+
glossary_id = 'must-start-with-letters-' + str(uuid.uuid1())
31+
translate_v3_create_glossary.sample_create_glossary(
32+
PROJECT_ID,
33+
GLOSSARY_INPUT_URI,
34+
glossary_id
35+
)
36+
37+
yield glossary_id
38+
39+
try:
40+
translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id)
41+
except Exception:
42+
pass
43+
44+
@pytest.fixture(scope='function')
45+
def bucket():
46+
"""Create a temporary bucket to store annotation output."""
47+
bucket_name = "mike-test-delete-" + str(uuid.uuid1())
48+
storage_client = storage.Client()
49+
bucket = storage_client.create_bucket(bucket_name)
50+
51+
yield bucket
52+
53+
bucket.delete(force=True)
54+
55+
def test_batch_translate_text_with_glossary_and_model(capsys, bucket, glossary):
56+
translate_v3_batch_translate_text_with_glossary_and_model.sample_batch_translate_text_with_glossary_and_model(
57+
'gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt',
58+
'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name),
59+
PROJECT_ID,
60+
'us-central1',
61+
'ja',
62+
'en',
63+
MODEL_ID,
64+
glossary
65+
)
66+
67+
out, _ = capsys.readouterr()
68+
assert 'Total Characters: 25' in out
69+
#TODO: find a way to make sure it translates correctly
70+
# SHOULD NOT BE - Google NMT model -> それはしません。欺ception"
71+
# literal: "それはそうだ" # custom model
72+
# literal: "欺く" # glossary

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy