2

I am trying to build an application using Google Cloud Platform AutoML using Python. My overall code flow looks like this:

User Interacts--> data sent to PubSub--> callback invokes my AutoML--> Result

The snippet that calls pubsub looks like this:

blob=blob+bytes(doc_type,'utf-8')
        publisher.publish(topic,blob)
        future=subscriber.subscribe(subscription,callback=callback)
        #flash("The object is "+future,'info')
        try:
            future.result()
        except Exception as ex:
            subscriber.close()

In PubSub callback:

def callback(message):
     new_message=message.data
     display_name,score=predict_value(new_message,"modelID","projectid",'us-central1')
     message.ack()

And my predict_value gets the model_id, project id and compute region and performs the prediction.

When I directly call predict_value without using PubSub it is working fine. If I do like this, I am getting the below error:

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

Please help me to resolve the issue

3
  • Where is this callback being executed? Is it a Cloud Function or where exactly? My point is that the service account executing this code must have "automl.models.predict" permission.
    – Temu
    Sep 4, 2018 at 10:19
  • Hi @Temu...thank you for your response. There is only one credential right for the whole google cloud platform which is the service account key generated in our account. I have added that in the top ...global level Sep 4, 2018 at 15:20
  • I mean, I understand you are trying to hit the same project where you run the code, right?
    – Temu
    Sep 5, 2018 at 15:18

2 Answers 2

2

Thank you so much for all your responses. I have just fixed the issue using the below snippet example

def receive_messages_synchronously(project, subscription_name):
"""Pulling messages synchronously."""
# [START pubsub_subscriber_sync_pull]
# project           = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
    project, subscription_name)

# Builds a pull request with a specific number of messages to return.
# `return_immediately` is set to False so that the system waits (for a
# bounded amount of time) until at lease one message is available.
response = subscriber.pull(
    subscription_path,
    max_messages=3,
    return_immediately=False)

ack_ids = []
for received_message in response.received_messages:
    print("Received: {}".format(received_message.message.data))
    ack_ids.append(received_message.ack_id)

# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(subscription_path, ack_ids)
# [END pubsub_subscriber_sync_pull]

The reason being the subscription that is created uses the pull request. I guess the callback method concept used is mainly for "push" which may be the reason because I didnt give the endpoint and token to publish the message. Hope what I am guessing is correct. Let me know your views as well.

1
  • ShriHari, could you check if you have a pubsub service account here in your console? Look, when you call a service (with an account with all permissions) and then this services calls a different service, the service in the middle will need permissions to use the final service since the permissions used are not the first ones but the service in the middle ones. A->B->C Thats why A can call C but B can't. Because A has all the permissions but B has the minimum ones associated to that specific service.
    – Temu
    Sep 6, 2018 at 15:37
0

This is likely due to one of two factors:

  • invalid credentials being used when sending the request to the AutoML API - it is very likely that pubsub executes in other context and can't get the default credentials

  • invalid model resource name (make sure it is correct) - it should be something like: "projects/12423534/locations/us-central1/models/23432423"

1
  • Hi Michal, thanks for your response. The thing is if I dont use PubSub and directly call the model, it is executing fine. When it goes to pubsub there is an issue Sep 5, 2018 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.