GCP AI平台API,访问被拒绝

问题描述

我一直试图将Tensorflow模型用于GCP的AI平台上。但是当我用outside作为API调用时,它返回一个

googleapiclient.errors.HttpError: <HttpError 403 when requesting ***** returned "Access to model denied.">

我使用了AI平台管理员的凭据,并且它以前也适用于sklearn模型,而我使用的是与之完美兼容的相同代码。 你知道是什么原因引起的吗?

from google.api_core.client_options import ClientOptions
import googleapiclient.discovery
from google.oauth2.service_account import Credentials
def predict_json(project,region,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.
        region (str): regional endpoint to use; set to None for ml.googleapis.com
        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>
    credentials = Credentials.from_service_account_file(r"./credentials2.json")
    prefix = "{}-ml".format(region) if region else "ml"
    api_endpoint = "https://{}.googleapis.com".format(prefix)
    client_options = ClientOptions(api_endpoint=api_endpoint)
    service = googleapiclient.discovery.build(
        'ml','v1',client_options=client_options,credentials=credentials)
    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']

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)