Search code examples
pythongoogle-cloud-automl

google.api_core.exceptions: 403 The caller does not have permission


i am using google cloud automl tables api , and when i execute the url request to the method it gives me this error :

google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission

the parameters is correct so why it shows me that error !

   @app.route('/model_train',methods=['GET','POST'])
def model_train():

    project_id = 'myproject'
    compute_region = 'us-central1'
    model_id = 'TBL7912987273010'
    file_path = 'E:/downloads/Dataset-entrainement-demain_ai_demonstrateur_attrition_Oct19_Copie.csv'
    score_threshold = '0.5'

    automl_client = automl.AutoMlClient()

    model_full_id = automl_client.model_path(
        project_id, compute_region, model_id
    )

    prediction_client = automl.PredictionServiceClient()

    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    with open(file_path, "rt") as csv_file:
        content = csv.reader(csv_file)
        for row in content:
            values = []
            for column in row:
                values.append({'number_value': float(column)})
            payload = {
                'row': {'values': values}
            }

            response = prediction_client.predict(model_full_id, payload)
            print("Prediction results:")
            for result in response.payload:
                print("Predicted class name: {}".format(result.display_name))
                print("Predicted class score: {}".format(result.classification.score))

can any one help me please !


Solution

  • Adding a formal answer, basically the issue is with the permissions set for the users account on GCP IAM section. Firstly, the automl API needs to be enabled for the project here

    You then need to generate credentials and pass them as environment variables. You can add the environment variables at the top of the file using the os library:

    import os
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json"