0

我一直在尝试将 Tensorflow 模型用于 GCP 上的 AI 平台。但是当我作为 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']
4

0 回答 0