【发布时间】:2021-03-28 12:46:15
【问题描述】:
在谷歌 colab 上运行。 我基本上已经从googles tutorial复制了代码,但是代码返回了多个错误(大部分已经通过重新安装包解决)。
我的环境身份验证变量设置正确。
我收到以下错误:
---------------------------------------------------------------------------
_InactiveRpcError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/google/api_core/grpc_helpers.py in error_remapped_callable(*args, **kwargs)
72 try:
---> 73 return callable_(*args, **kwargs)
74 except grpc.RpcError as exc:
7 frames
_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Invalid resource name location. Identifier must contain only lowercase letters, digits, or hyphens.; Resource type: location"
debug_error_string = "{"created":"@1616934732.016610183","description":"Error received from peer ipv4:142.250.65.74:443","file":"src/core/lib/surface/call.cc","file_line":1061,"grpc_message":"Invalid resource name location. Identifier must contain only lowercase letters, digits, or hyphens.; Resource type: location","grpc_status":3}"
>
The above exception was the direct cause of the following exception:
InvalidArgument Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/six.py in raise_from(value, from_value)
InvalidArgument: 400 Invalid resource name location. Identifier must contain only lowercase letters, digits, or hyphens.; Resource type: location
这是我的代码:
!pip install --upgrade google-cloud
from google.cloud import translate
def batch_translate_text(
input_uri="gs://HIDDEN/HIDDEN.txt",
output_uri="gs://HIDDEN/out",
project_id="HIDDEN",
timeout=180,
):
"""Translates a batch of texts on GCS and stores the result in a GCS location."""
client = translate.TranslationServiceClient()
location = "eu (multiple regions in European Union)"
# Supported file types: https://cloud.google.com/translate/docs/supported-formats
gcs_source = {"input_uri": input_uri}
input_configs_element = {
"gcs_source": gcs_source,
"mime_type": "text/plain", # Can be "text/plain" or "text/html".
}
gcs_destination = {"output_uri_prefix": output_uri}
output_config = {"gcs_destination": gcs_destination}
parent = f"projects/{project_id}/locations/{location}"
# Supported language codes: https://cloud.google.com/translate/docs/language
operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": "sv",
"target_language_codes": ["en"], # Up to 10 language codes here.
"input_configs": [input_configs_element],
"output_config": output_config
}
)
print("Waiting for operation to complete...")
response = operation.result(timeout)
print("Total Characters: {}".format(response.total_characters))
print("Translated Characters: {}".format(response.translated_characters))
batch_translate_text()
根据the API,这应该是正确的,并且标识符只包含小写字母、数字或连字符。
我可以做些什么来完成这项工作?
【问题讨论】:
标签: python google-api google-colaboratory google-translate invalid-argument