How to access Google Prediction API with Python?

312 views Asked by At

I have a practice project given to me by an advisor that asks me to write a python script to access a model I trained with the Prediction API using the API Explorer. I have two questions regarding this,

  1. The guidelines specify that I should not check in my credentials in the Python script and I am not exactly sure what that means, it also leads to question...

  2. When I follow the documentation to call the "predict" method of "trainedmodels" (to predict the language of a text using a trained model)

    from apiclient import discovery
    
    service = discovery.build('prediction','v1.6')
    x = service.trainedmodels().predict(project='My First Project', 
         id='my_project_id', 
         body={"input":{"csvInstance":['bonjour!']}})
    

This is the return value

    <googleapiclient.http.HttpRequest object at 0x1031996d0>

Because I am not too aware of what is meant by "not checking in my credentials", I am unclear as to how to proceed in resolving this problem.

Thank you in advance.

1

There are 1 answers

0
Alexander Graebe On

There are at least ways to achieve that:

  • Using the gcloud tool to store your credentials locally (https://cloud.google.com/sdk/gcloud/#gcloud.auth). Those will be easily accessible from within your Python app like below.
  • Creating a configuration file and retrieve the credentials from there

Here's a snippet showing how to access the credentials from within Python:

http = AppAssertionCredentials('https://www.googleapis.com/auth/prediction https://www.googleapis.com/auth/devstorage.read_only').authorize(httplib2.Http())
service = build('prediction', 'v1.6', http=http)