我有一个服务帐户,我已授予查看者角色,并已下载凭据 json 文件并为其设置正确的环境变量。我正在尝试在这里运行示例:
def predict_json(project, model, instances, version=None):
"""Send json data to a deployed model for prediction.
Args:
project (str): project where the Cloud ML Engine Model is deployed.
model (str): model name.
instances ([Mapping[str: Any]]): Keys should be the names of Tensors
your deployed model expects as inputs. Values should be datatypes
convertible to Tensors, or (potentially nested) lists of datatypes
convertible to tensors.
version: str, version of the model to target.
Returns:
Mapping[str: any]: dictionary of prediction results defined by the
model.
"""
# Create the ML Engine service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = googleapiclient.discovery.build('ml', 'v1beta1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response['predictions']
但是,这给了我一个 403 和错误The user doesn't have the required permission ml.versions.predict on the resource projects/project/models/model/versions/version
。我不确定我做错了什么 - 我正在为凭据设置正确的环境变量,根据他们的文档,服务帐户只需要查看者角色即可访问此端点。我做错了什么?