airflow.providers.google.cloud.hooks.translate

此模組包含 Google Cloud Translate Hook。

模組內容

類別

CloudTranslateHook

用於 Google Cloud Translate API 的 Hook。

TranslateHook

用於 Google Cloud Translation (進階版) 的 Hook,使用用戶端版本 V3。

exception airflow.providers.google.cloud.hooks.translate.WaitOperationNotDoneYetError[source]

Bases: Exception

等待操作尚未完成錯誤。

class airflow.providers.google.cloud.hooks.translate.CloudTranslateHook(gcp_conn_id='google_cloud_default', impersonation_chain=None, **kwargs)[source]

Bases: airflow.providers.google.common.hooks.base_google.GoogleBaseHook

用於 Google Cloud Translate API 的 Hook。

Hook 中所有使用 project_id 的方法都必須使用關鍵字引數而非位置引數來呼叫。

get_conn()[source]

擷取與 Cloud Translate 的連線。

返回

Google Cloud Translate 用戶端物件。

返回類型

google.cloud.translate_v2.Client

translate(values, target_language, format_=None, source_language=None, model=None)[source]

翻譯字串或字串列表。

請參閱 https://cloud.google.com/translate/docs/translating-text

參數
  • values (str | list[str]) – 要翻譯的字串或字串列表。

  • target_language (str) – 要將結果翻譯成的語言。這是 API 的必要參數,預設為目前執行個體的目標語言。

  • format – (選填) texthtml 其中之一,用於指定輸入文字是純文字還是 HTML。

  • source_language (str | None) – (選填) 要翻譯的文字語言。

  • model (str | list[str] | None) – (選填) 用於翻譯文字的模型,例如 'base''NMT'

返回

每個查詢值的字典列表。每個字典通常包含三個鍵 (儘管並非所有情況下都會全部存在)

  • detectedSourceLanguage: 偵測到的文字語言 (以 ISO 639-1 語言代碼表示)。

  • translatedText: 文字翻譯成目標語言的結果。

  • input: 對應的輸入值。

  • model: 用於翻譯文字的模型。

如果僅傳遞單一值,則僅返回單一字典。

引發

ValueError 如果值和翻譯的數量不同。

返回類型

dict

class airflow.providers.google.cloud.hooks.translate.TranslateHook(gcp_conn_id='google_cloud_default', impersonation_chain=None, **kwargs)[source]

Bases: airflow.providers.google.common.hooks.base_google.GoogleBaseHook

用於 Google Cloud Translation (進階版) 的 Hook,使用用戶端版本 V3。

請參閱相關文檔 https://cloud.google.com/translate/docs/editions#advanced

get_client()[source]

擷取 TranslationService 用戶端。

返回

Google Cloud Translation Service 用戶端物件。

返回類型

google.cloud.translate_v3.TranslationServiceClient

static wait_for_operation_done(*, operation, timeout=None, initial=3, multiplier=2, maximum=3600)[source]

等待長時間運行的操作完成。

呼叫 operation.done() 直到成功或超時耗盡,並遵循退避重試策略。請參閱 google.api_core.retry.Retry。它旨在用於結果為空的操作 (google.protobuf.empty_pb2.Empty)。因此,對於此類操作呼叫 operation.result() 會觸發異常 GoogleAPICallError("Unexpected state: Long-running operation had neither response nor error set."),即使操作本身完全正常。

static wait_for_operation_result(operation, timeout=None)[source]

等待長時間運行的操作完成。

static extract_object_id(obj)[source]

返回物件的唯一 ID。

translate_text(*, project_id=PROVIDE_PROJECT_ID, contents, target_language_code, source_language_code=None, mime_type=None, location=None, model=None, transliteration_config=None, glossary_config=None, labels=None, timeout=DEFAULT, metadata=(), retry=DEFAULT)[source]

翻譯提供的文字內容。

參數
  • project_id (str) – 必要。服務所屬的 Google Cloud 專案 ID。

  • contents (collections.abc.Sequence[str]) – 必要。字串格式的輸入內容。建議最大長度為 1024 個項目,共 30,000 個碼位。

  • mime_type (str | None) – 選填。來源文字的格式。如果留空,MIME 類型預設為「text/html」。

  • source_language_code (str | None) – 選填。如果已知輸入文字的 ISO-639 語言代碼。如果未指定來源語言,API 會嘗試自動識別來源語言,並在回應中返回來源語言。

  • target_language_code (str) – 必要。用於翻譯輸入文字的 ISO-639 語言代碼

  • location (str | None) – 選填。用於呼叫的專案或位置。必須參照呼叫者的專案。如果未指定,則使用「global」。使用 AutoML 模型或自訂詞彙表的要求需要非全域位置。模型和詞彙表必須位於相同區域 (具有相同的 location-id)。

  • model (str | None) –

    選填。此翻譯請求所要求的 model 類型。如果未提供,將使用預設 Google 模型 (NMT)。格式取決於模型類型

    • AutoML Translation 模型: projects/{project-number-or-id}/locations/{location-id}/models/{model-id}

    • 一般 (內建) 模型: projects/{project-number-or-id}/locations/{location-id}/models/general/nmt

    • Translation LLM 模型: projects/{project-number-or-id}/locations/{location-id}/models/general/translation-llm

    對於全域 (無區域) 請求,請使用 location-id global。例如,projects/{project-number-or-id}/locations/global/models/general/nmt

  • glossary_config (google.cloud.translate_v3.types.TranslateTextGlossaryConfig | None) – 選填。要套用的詞彙表。詞彙表必須與模型位於相同區域 (具有相同的 location-id)。

  • transliteration_config (google.cloud.translate_v3.types.TransliterationConfig | None) – 選填。要套用的音譯設定。

  • labels (str | None) – 選填。帶有使用者定義中繼資料的要求標籤。有關更多資訊,請參閱 https://cloud.google.com/translate/docs/advanced/labels

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault | None) – 指定應重試哪些錯誤 (如果有的話)。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 此請求的超時時間。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 應與請求一起作為中繼資料傳送的字串。

返回

來自 API 回應的文字翻譯結果。

返回類型

dict

batch_translate_text(*, project_id=PROVIDE_PROJECT_ID, location, source_language_code, target_language_codes, input_configs, output_config, models=None, glossaries=None, labels=None, timeout=DEFAULT, metadata=(), retry=DEFAULT)[source]

翻譯大量文字資料。

參數
返回

具有批次文字翻譯結果的 Operation 物件,這些結果在準備就緒時會以批次形式返回。

返回類型

google.api_core.operation.Operation

create_dataset(*, project_id=PROVIDE_PROJECT_ID, location, dataset, timeout=DEFAULT, metadata=(), retry=DEFAULT)[source]

建立翻譯資料集。

參數
  • dataset (dict | google.cloud.translate_v3.types.automl_translation.Dataset) – 要建立的資料集。如果提供 dict,則必須對應於 automl_translation.Dataset 類型。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault | None) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

要建立之資料集的 Operation 物件。

返回類型

google.api_core.operation.Operation

get_dataset(dataset_id, project_id, location, retry=DEFAULT, timeout=DEFAULT, metadata=())[source]

擷取給定 dataset_id 的資料集。

參數
  • dataset_id (str) – 要擷取的翻譯資料集 ID。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

automl_translation.Dataset 執行個體。

返回類型

google.cloud.translate_v3.types.automl_translation.Dataset

import_dataset_data(dataset_id, location, input_config, project_id=PROVIDE_PROJECT_ID, retry=DEFAULT, timeout=None, metadata=())[source]

將資料匯入翻譯資料集。

參數
  • dataset_id (str) – 翻譯資料集的 ID。

  • input_config (dict | google.cloud.translate_v3.types.DatasetInputConfig) – 期望的輸入位置及其網域特定語意 (如果有的話)。如果提供 dict,則其格式必須與 protobuf 訊息 InputConfig 相同。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果為 None,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | None) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

匯入資料的 Operation 物件。

返回類型

google.api_core.operation.Operation

list_datasets(project_id, location, retry=DEFAULT, timeout=DEFAULT, metadata=())[source]

列出專案中的翻譯資料集。

參數
  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

pagers.ListDatasetsPager 執行個體,可迭代物件以檢索資料集清單。

返回類型

google.cloud.translate_v3.services.translation_service.pagers.ListDatasetsPager

delete_dataset(dataset_id, project_id, location, retry=DEFAULT, timeout=None, metadata=())[source]

刪除翻譯資料集及其所有內容。

參數
  • dataset_id (str) – 要刪除的資料集 ID。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | None) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

Operation 物件,包含資料集刪除結果 (完成時)。

返回類型

google.api_core.operation.Operation

create_model(dataset_id, display_name, project_id, location, retry=DEFAULT, timeout=None, metadata=())[source]

透過在提供的翻譯資料集上訓練來建立原生模型。

參數
  • dataset_id (str) – 用於模型訓練的資料集 ID。

  • display_name (str) – 訓練模型的顯示名稱。允許 A-Z 和 a-z、底線 (_) 以及 ASCII 數字 0-9。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | None) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

Operation 物件,包含模型建立結果 (完成時)。

返回類型

google.api_core.operation.Operation

get_model(model_id, project_id, location, retry=DEFAULT, timeout=DEFAULT, metadata=())[source]

檢索給定 model_id 的資料集。

參數
  • model_id (str) – 要檢索的翻譯模型 ID。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

automl_translation.Model 執行個體。

返回類型

google.cloud.translate_v3.types.automl_translation.Model

list_models(project_id, location, filter_str=None, page_size=None, retry=DEFAULT, timeout=DEFAULT, metadata=())[source]

列出專案中的翻譯模型。

參數
  • project_id (str) – Google Cloud 專案 ID,模型位於其中。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • filter_str (str | None) – 用於篩選將傳回模型的選用運算式。支援的篩選器: dataset_id=${dataset_id}

  • page_size (int | None) – 選用的自訂頁面大小值。伺服器傳回的結果可能少於請求的數量。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

pagers.ListDatasetsPager 執行個體,可迭代物件以檢索資料集清單。

返回類型

google.cloud.translate_v3.services.translation_service.pagers.ListModelsPager

delete_model(model_id, project_id, location, retry=DEFAULT, timeout=None, metadata=())[source]

刪除翻譯模型及其所有內容。

參數
  • model_id (str) – 要刪除的模型 ID。

  • project_id (str) – 資料集所在的 Google Cloud 專案 ID。如果未提供,則使用預設 project_id。

  • location (str) – 專案的位置。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault) – 用於重試請求的重試物件。如果指定 None,則不會重試請求。

  • timeout (float | None) – 等待請求完成的秒數。請注意,如果指定 retry,則超時時間適用於每次個別嘗試。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 提供給方法的其他中繼資料。

返回

Operation 物件,包含資料集刪除結果 (完成時)。

返回類型

google.api_core.operation.Operation

translate_document(*, project_id=PROVIDE_PROJECT_ID, source_language_code=None, target_language_code, location=None, document_input_config, document_output_config, customized_attribution=None, is_translate_native_pdf_only=False, enable_shadow_removal_native_pdf=False, enable_rotation_correction=False, model=None, glossary_config=None, labels=None, timeout=DEFAULT, metadata=(), retry=DEFAULT)[source]

翻譯提供的文件。

參數
  • project_id (str) – 必要。服務所屬的 Google Cloud 專案 ID。

  • source_language_code (str | None) – 選用項目。如果已知輸入文件文字的 ISO-639 語言代碼。如果未指定來源語言,API 會嘗試自動識別來源語言,並在回應中傳回來源語言。

  • target_language_code (str) – 必填。用於翻譯輸入文件文字的 ISO-639 語言代碼。

  • location (str | None) – 選填。用於呼叫的專案或位置。必須參照呼叫者的專案。如果未指定,則使用「global」。使用 AutoML 模型或自訂詞彙表的要求需要非全域位置。模型和詞彙表必須位於相同區域 (具有相同的 location-id)。

  • document_input_config (google.cloud.translate_v3.types.DocumentInputConfig | dict) – 文件翻譯請求輸入設定。

  • document_output_config (google.cloud.translate_v3.types.DocumentOutputConfig | dict | None) – 選用項目。文件翻譯請求輸出設定。如果未提供,翻譯後的文件將僅透過位元組串流傳回,且其輸出 MIME 類型將與輸入文件的 MIME 類型相同。

  • customized_attribution (str | None) – 選用項目。此標記用於支援使用者自訂歸屬。如果未提供,預設值為 Machine Translated by Google。自訂歸屬應遵循 https://cloud.google.com/translate/attribution#attribution_and_logos 中的規則

  • is_translate_native_pdf_only (bool) – 選用項目。外部客戶的參數。如果為 true,線上原生 PDF 翻譯的頁面限制為 300,且僅翻譯原生 PDF 頁面。

  • enable_shadow_removal_native_pdf (bool) – 選用項目。如果為 true,請使用文字移除伺服器來移除原生 PDF 翻譯背景圖片上的陰影文字。陰影移除功能僅在 is_translate_native_pdf_onlypdf_native_only 皆為 False 時啟用。

  • enable_rotation_correction (bool) – 選用項目。如果為 true,則在 DVS 中啟用自動旋轉校正。

  • model (str | None) –

    選填。此翻譯請求所要求的 model 類型。如果未提供,將使用預設 Google 模型 (NMT)。格式取決於模型類型

    • AutoML Translation 模型: projects/{project-number-or-id}/locations/{location-id}/models/{model-id}

    • 通用 (內建) 模型: projects/{project-number-or-id}/locations/{location-id}/models/general/nmt,

    如果未提供,預設的 Google 模型 (NMT) 將用於翻譯。

  • glossary_config (google.cloud.translate_v3.types.TranslateTextGlossaryConfig | None) – 選填。要套用的詞彙表。詞彙表必須與模型位於相同區域 (具有相同的 location-id)。

  • labels (str | None) – 選填。帶有使用者定義中繼資料的要求標籤。有關更多資訊,請參閱 https://cloud.google.com/translate/docs/advanced/labels

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault | None) – 指定應重試哪些錯誤 (如果有的話)。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 此請求的超時時間。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 應與請求一起作為中繼資料傳送的字串。

返回

來自 API 回應的翻譯文件結果。

返回類型

google.cloud.translate_v3.types.TranslateDocumentResponse

batch_translate_document(*, project_id=PROVIDE_PROJECT_ID, source_language_code, target_language_codes=None, location=None, input_configs, output_config, customized_attribution=None, format_conversions=None, enable_shadow_removal_native_pdf=False, enable_rotation_correction=False, models=None, glossaries=None, timeout=DEFAULT, metadata=(), retry=DEFAULT)[source]

依據提供的設定批量翻譯文件。

參數
  • project_id (str) – 必要。服務所屬的 Google Cloud 專案 ID。

  • source_language_code (str) – 選用項目。如果已知輸入文字的 ISO-639 語言代碼。如果未指定來源語言,API 會嘗試自動識別來源語言,並在回應中傳回來源語言。

  • target_language_codes (collections.abc.MutableSequence[str] | None) – 必填。用於翻譯輸入文件的 ISO-639 語言代碼。在此處最多指定 10 個語言代碼。

  • location (str | None) – 選填。用於呼叫的專案或位置。必須參照呼叫者的專案。如果未指定,則使用「global」。使用 AutoML 模型或自訂詞彙表的要求需要非全域位置。模型和詞彙表必須位於相同區域 (具有相同的 location-id)。

  • input_configs (collections.abc.MutableSequence[google.cloud.translate_v3.types.BatchDocumentInputConfig | dict]) – 輸入設定。符合條件的檔案總數應 <= 100。要翻譯的總內容大小應 <= 100M Unicode 字碼指標。檔案必須使用 UTF-8 編碼。

  • output_config (google.cloud.translate_v3.types.BatchDocumentOutputConfig | dict) – 輸出設定。如果 2 個輸入設定符合同一個檔案 (即相同的輸入路徑),則不會產生重複輸入的輸出。

  • format_conversions (collections.abc.MutableMapping[str, str] | None) –

    選用項目。套用至所有輸入檔案的檔案格式轉換對應。對應鍵是原始 mime_type。對應值是翻譯後文件的目標 mime_type。支援的檔案格式轉換包括

    • application/pdf 轉換為 application/vnd.openxmlformats-officedocument.wordprocessingml.document

    如果未指定任何項目,則輸出檔案將與原始檔案的格式相同。

  • customized_attribution (str | None) – 選用項目。此標記用於支援使用者自訂歸屬。如果未提供,預設值為 Machine Translated by Google。自訂歸屬應遵循 https://cloud.google.com/translate/attribution#attribution_and_logos 中的規則

  • enable_shadow_removal_native_pdf (bool) – 選用項目。如果為 true,請使用文字移除伺服器來移除原生 PDF 翻譯背景圖片上的陰影文字。陰影移除功能僅在 is_translate_native_pdf_onlypdf_native_only 皆為 False 時啟用。

  • enable_rotation_correction (bool) – 選用項目。如果為 true,則在 DVS 中啟用自動旋轉校正。

  • models (collections.abc.MutableMapping[str, str] | None) –

    選用項目。用於翻譯的模型。對應的鍵是目標語言代碼。對應的值是模型名稱。值可以是內建通用模型,或 AutoML Translation 模型。值的格式取決於模型類型

    • AutoML Translation 模型: projects/{project-number-or-id}/locations/{location-id}/models/{model-id}

    • 通用 (內建) 模型: projects/{project-number-or-id}/locations/{location-id}/models/general/nmt,

    如果對應為空,或者未針對語言組請求特定模型,則會使用預設的 Google 模型 (NMT)。

  • glossaries (collections.abc.MutableMapping[str, google.cloud.translate_v3.types.TranslateTextGlossaryConfig] | None) – 要套用的詞彙表。以目標語言代碼作為鍵。

  • retry (google.api_core.retry.Retry | google.api_core.gapic_v1.method._MethodDefault | None) – 指定應重試哪些錯誤 (如果有的話)。

  • timeout (float | google.api_core.gapic_v1.method._MethodDefault) – 此請求的超時時間。

  • metadata (collections.abc.Sequence[tuple[str, str]]) – 應與請求一起作為中繼資料傳送的字串。

返回

來自 API 回應的批量翻譯文件結果。

返回類型

google.api_core.operation.Operation

這個條目是否有幫助?