pythongoogle-app-enginegoogle-cloud-automl-nl

Unable to make prediction on google automl


I trained a model and would like to predict the score of a new image.

Now I execute the following function but it returns an error:

google.api_core.exceptions.PermissionDenied: 403 Permission 'automl.models.predict' denied on resource 'projects/project_id/locations/us-central1/models/model_id' (or it may not exist).

I am not sure if it is due to incorrect location, i.e. us-central1? What is the gcloud command to check?

How to solve this problem?

Thank you very much.

def get_prediction(content, project_id, model_id):

    prediction_client = automl_v1beta1.PredictionServiceClient()
    name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
    payload = {'image': {'image_bytes': content }}
    params = {}
    request = prediction_client.predict(name, payload, params)
    return request  # waits till request is returned

Solution

  • The permission error messages are usually thrown when the application is not being authenticated correctly; Therefore, it is required to verify that the service account you are using has the required roles assigned, as well as provide the credentials to your application by using environment variables or explicitly point to your service account file in code. Keep in mind that when you set an environment variable value in a session, it is reset every time the session is dropped.

    Additionally, AutoML Vision currently requires the location us-central1, as mentioned in the API Tutorial. Based on this, you should be fine on this aspect; However, you can take a look on the projects.locations REST methods in case you want to get additional information about this config.

    You can use the following official documentation example to Pass the path to the service account key in code, as well as the QuickStart guide, to know more about the required configuration to begin using AutoML Vision service.

    namespace Google\Cloud\Samples\Auth;
    
    // Imports the Google Cloud Storage client library.
    use Google\Cloud\Storage\StorageClient;
    
    function auth_cloud_explicit($projectId, $serviceAccountPath)
    {
        # Explicitly use service account credentials by specifying the private key
        # file.
        $config = [
            'keyFilePath' => $serviceAccountPath,
            'projectId' => $projectId,
        ];
        $storage = new StorageClient($config);
    
        # Make an authenticated API request (listing storage buckets)
        foreach ($storage->buckets() as $bucket) {
            printf('Bucket: %s' . PHP_EOL, $bucket->name());
        }
    }