Reputation: 37
I am currently working on integrating a glossary with the Google Cloud Translation API for document translation. I have successfully implemented document translation and created a glossary separately, but I am struggling to find a way to directly translate documents using the glossary ID without an AutoML model.I have gone through the documentation provided by Google, but it doesn't provide clear instructions on how to achieve this. Can someone please provide a code example, preferably in Python, on how to translate documents using a glossary ID with the Google Cloud Translation API?Thank you in advance for your help!
Upvotes: 1
Views: 357
Reputation: 535
Be great if you can provide code snippets of how you are creating the glossary and making document translation request.
Assuming that you already have glossary resource successfully created, and the document in GCS. Here is the code snippet of translating document using glossary (You can read more about this API here):
from google.cloud import translate_v3beta1 as translate
def translate_document(
project_id: str,
file_path: str,
glossary_id: str # Add your glossary ID parameter
) -> translate.TranslationServiceClient:
"""Translates a document.
Args:
project_id: The GCP project ID.
file_path: The path to the file to be translated.
glossary_id: glossary resource ID.
Returns:
The translated document.
"""
client = translate.TranslationServiceClient()
location = "us-central1"
parent = f"projects/{project_id}/locations/{location}"
document_input_config = {
"content": document_content,
"mime_type": "application/pdf",
}
# Create glossary configuration here
glossary_config = translate.types.TranslateTextGlossaryConfig(glossary_id=glossary_id)
response = client.translate_document(
request={
"parent": parent,
"target_language_code": "fr-FR",
"document_input_config": document_input_config,
"glossary_config": glossary_config, # Include glossary config
}
)
return response
You don't have to use AutoML together with glossary.
Upvotes: 0