GCP AI平台API,访问被拒绝

时间:2020-11-10 17:02:00

标签: python google-cloud-platform google-ai-platform

我一直试图将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']

0 个答案:

没有答案