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

Commit

Permalink
feat: add translate_document and batch_translate_document (#234)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 393656336

Source-Link: googleapis/googleapis@17d3fb2

Source-Link: googleapis/googleapis-gen@00ca50b

feat(v3beta1): add format_conversions
  • Loading branch information
gcf-owl-bot[bot] committed Aug 29, 2021
1 parent a1297ba commit b5962d6
Show file tree
Hide file tree
Showing 14 changed files with 1,431 additions and 47 deletions.
30 changes: 30 additions & 0 deletions google/cloud/translate/__init__.py
Expand Up @@ -21,6 +21,19 @@
TranslationServiceAsyncClient,
)

from google.cloud.translate_v3.types.translation_service import BatchDocumentInputConfig
from google.cloud.translate_v3.types.translation_service import (
BatchDocumentOutputConfig,
)
from google.cloud.translate_v3.types.translation_service import (
BatchTranslateDocumentMetadata,
)
from google.cloud.translate_v3.types.translation_service import (
BatchTranslateDocumentRequest,
)
from google.cloud.translate_v3.types.translation_service import (
BatchTranslateDocumentResponse,
)
from google.cloud.translate_v3.types.translation_service import BatchTranslateMetadata
from google.cloud.translate_v3.types.translation_service import BatchTranslateResponse
from google.cloud.translate_v3.types.translation_service import (
Expand All @@ -34,6 +47,9 @@
from google.cloud.translate_v3.types.translation_service import DetectedLanguage
from google.cloud.translate_v3.types.translation_service import DetectLanguageRequest
from google.cloud.translate_v3.types.translation_service import DetectLanguageResponse
from google.cloud.translate_v3.types.translation_service import DocumentInputConfig
from google.cloud.translate_v3.types.translation_service import DocumentOutputConfig
from google.cloud.translate_v3.types.translation_service import DocumentTranslation
from google.cloud.translate_v3.types.translation_service import GcsDestination
from google.cloud.translate_v3.types.translation_service import GcsSource
from google.cloud.translate_v3.types.translation_service import GetGlossaryRequest
Expand All @@ -48,6 +64,10 @@
from google.cloud.translate_v3.types.translation_service import OutputConfig
from google.cloud.translate_v3.types.translation_service import SupportedLanguage
from google.cloud.translate_v3.types.translation_service import SupportedLanguages
from google.cloud.translate_v3.types.translation_service import TranslateDocumentRequest
from google.cloud.translate_v3.types.translation_service import (
TranslateDocumentResponse,
)
from google.cloud.translate_v3.types.translation_service import (
TranslateTextGlossaryConfig,
)
Expand All @@ -58,6 +78,11 @@
__all__ = (
"TranslationServiceClient",
"TranslationServiceAsyncClient",
"BatchDocumentInputConfig",
"BatchDocumentOutputConfig",
"BatchTranslateDocumentMetadata",
"BatchTranslateDocumentRequest",
"BatchTranslateDocumentResponse",
"BatchTranslateMetadata",
"BatchTranslateResponse",
"BatchTranslateTextRequest",
Expand All @@ -69,6 +94,9 @@
"DetectedLanguage",
"DetectLanguageRequest",
"DetectLanguageResponse",
"DocumentInputConfig",
"DocumentOutputConfig",
"DocumentTranslation",
"GcsDestination",
"GcsSource",
"GetGlossaryRequest",
Expand All @@ -81,6 +109,8 @@
"OutputConfig",
"SupportedLanguage",
"SupportedLanguages",
"TranslateDocumentRequest",
"TranslateDocumentResponse",
"TranslateTextGlossaryConfig",
"TranslateTextRequest",
"TranslateTextResponse",
Expand Down
20 changes: 20 additions & 0 deletions google/cloud/translate_v3/__init__.py
Expand Up @@ -17,6 +17,11 @@
from .services.translation_service import TranslationServiceClient
from .services.translation_service import TranslationServiceAsyncClient

from .types.translation_service import BatchDocumentInputConfig
from .types.translation_service import BatchDocumentOutputConfig
from .types.translation_service import BatchTranslateDocumentMetadata
from .types.translation_service import BatchTranslateDocumentRequest
from .types.translation_service import BatchTranslateDocumentResponse
from .types.translation_service import BatchTranslateMetadata
from .types.translation_service import BatchTranslateResponse
from .types.translation_service import BatchTranslateTextRequest
Expand All @@ -28,6 +33,9 @@
from .types.translation_service import DetectedLanguage
from .types.translation_service import DetectLanguageRequest
from .types.translation_service import DetectLanguageResponse
from .types.translation_service import DocumentInputConfig
from .types.translation_service import DocumentOutputConfig
from .types.translation_service import DocumentTranslation
from .types.translation_service import GcsDestination
from .types.translation_service import GcsSource
from .types.translation_service import GetGlossaryRequest
Expand All @@ -40,13 +48,20 @@
from .types.translation_service import OutputConfig
from .types.translation_service import SupportedLanguage
from .types.translation_service import SupportedLanguages
from .types.translation_service import TranslateDocumentRequest
from .types.translation_service import TranslateDocumentResponse
from .types.translation_service import TranslateTextGlossaryConfig
from .types.translation_service import TranslateTextRequest
from .types.translation_service import TranslateTextResponse
from .types.translation_service import Translation

__all__ = (
"TranslationServiceAsyncClient",
"BatchDocumentInputConfig",
"BatchDocumentOutputConfig",
"BatchTranslateDocumentMetadata",
"BatchTranslateDocumentRequest",
"BatchTranslateDocumentResponse",
"BatchTranslateMetadata",
"BatchTranslateResponse",
"BatchTranslateTextRequest",
Expand All @@ -58,6 +73,9 @@
"DetectLanguageRequest",
"DetectLanguageResponse",
"DetectedLanguage",
"DocumentInputConfig",
"DocumentOutputConfig",
"DocumentTranslation",
"GcsDestination",
"GcsSource",
"GetGlossaryRequest",
Expand All @@ -70,6 +88,8 @@
"OutputConfig",
"SupportedLanguage",
"SupportedLanguages",
"TranslateDocumentRequest",
"TranslateDocumentResponse",
"TranslateTextGlossaryConfig",
"TranslateTextRequest",
"TranslateTextResponse",
Expand Down
20 changes: 20 additions & 0 deletions google/cloud/translate_v3/gapic_metadata.json
Expand Up @@ -10,6 +10,11 @@
"grpc": {
"libraryClient": "TranslationServiceClient",
"rpcs": {
"BatchTranslateDocument": {
"methods": [
"batch_translate_document"
]
},
"BatchTranslateText": {
"methods": [
"batch_translate_text"
Expand Down Expand Up @@ -45,6 +50,11 @@
"list_glossaries"
]
},
"TranslateDocument": {
"methods": [
"translate_document"
]
},
"TranslateText": {
"methods": [
"translate_text"
Expand All @@ -55,6 +65,11 @@
"grpc-async": {
"libraryClient": "TranslationServiceAsyncClient",
"rpcs": {
"BatchTranslateDocument": {
"methods": [
"batch_translate_document"
]
},
"BatchTranslateText": {
"methods": [
"batch_translate_text"
Expand Down Expand Up @@ -90,6 +105,11 @@
"list_glossaries"
]
},
"TranslateDocument": {
"methods": [
"translate_document"
]
},
"TranslateText": {
"methods": [
"translate_text"
Expand Down
129 changes: 122 additions & 7 deletions google/cloud/translate_v3/services/translation_service/async_client.py
Expand Up @@ -218,8 +218,9 @@ async def translate_text(
contents (:class:`Sequence[str]`):
Required. The content of the input in
string format. We recommend the total
content be less than 30k codepoints. Use
BatchTranslateText for larger text.
content be less than 30k codepoints. The
max length of this field is 1024.
Use BatchTranslateText for larger text.
This corresponds to the ``contents`` field
on the ``request`` instance; if ``request`` is provided, this
Expand All @@ -235,14 +236,13 @@ async def translate_text(
- General (built-in) models:
``projects/{project-number-or-id}/locations/{location-id}/models/general/nmt``,
``projects/{project-number-or-id}/locations/{location-id}/models/general/base``
For global (non-regionalized) requests, use
``location-id`` ``global``. For example,
``projects/{project-number-or-id}/locations/global/models/general/nmt``.
If missing, the system decides which google base model
to use.
If not provided, the default Google model (NMT) will be
used.
This corresponds to the ``model`` field
on the ``request`` instance; if ``request`` is provided, this
Expand Down Expand Up @@ -506,11 +506,10 @@ async def get_supported_languages(
- General (built-in) models:
``projects/{project-number-or-id}/locations/{location-id}/models/general/nmt``,
``projects/{project-number-or-id}/locations/{location-id}/models/general/base``
Returns languages supported by the specified model. If
missing, we get supported languages of Google general
base (PBMT) model.
NMT model.
This corresponds to the ``model`` field
on the ``request`` instance; if ``request`` is provided, this
Expand Down Expand Up @@ -588,6 +587,54 @@ async def get_supported_languages(
# Done; return the response.
return response

async def translate_document(
self,
request: translation_service.TranslateDocumentRequest = None,
*,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> translation_service.TranslateDocumentResponse:
r"""Translates documents in synchronous mode.
Args:
request (:class:`google.cloud.translate_v3.types.TranslateDocumentRequest`):
The request object. A document translation request.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
google.cloud.translate_v3.types.TranslateDocumentResponse:
A translated document response
message.
"""
# Create or coerce a protobuf request object.
request = translation_service.TranslateDocumentRequest(request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.translate_document,
default_timeout=600.0,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)),
)

# Send the request.
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response

async def batch_translate_text(
self,
request: translation_service.BatchTranslateTextRequest = None,
Expand Down Expand Up @@ -656,6 +703,74 @@ async def batch_translate_text(
# Done; return the response.
return response

async def batch_translate_document(
self,
request: translation_service.BatchTranslateDocumentRequest = None,
*,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> operation_async.AsyncOperation:
r"""Translates a large volume of document in asynchronous
batch mode. This function provides real-time output as
the inputs are being processed. If caller cancels a
request, the partial results (for an input file, it's
all or nothing) may still be available on the specified
output location.
This call returns immediately and you can use
google.longrunning.Operation.name to poll the status of
the call.
Args:
request (:class:`google.cloud.translate_v3.types.BatchTranslateDocumentRequest`):
The request object. The BatchTranslateDocument request.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
google.api_core.operation_async.AsyncOperation:
An object representing a long-running operation.
The result type for the operation will be :class:`google.cloud.translate_v3.types.BatchTranslateDocumentResponse` Stored in the
[google.longrunning.Operation.response][google.longrunning.Operation.response]
field returned by BatchTranslateDocument if at least
one document is translated successfully.
"""
# Create or coerce a protobuf request object.
request = translation_service.BatchTranslateDocumentRequest(request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.batch_translate_document,
default_timeout=600.0,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)),
)

# Send the request.
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Wrap the response in an operation future.
response = operation_async.from_gapic(
response,
self._client._transport.operations_client,
translation_service.BatchTranslateDocumentResponse,
metadata_type=translation_service.BatchTranslateDocumentMetadata,
)

# Done; return the response.
return response

async def create_glossary(
self,
request: translation_service.CreateGlossaryRequest = None,
Expand Down

0 comments on commit b5962d6

Please sign in to comment.