Search code examples
pythonpredictiongoogle-api-python-clientgoogle-cloud-ml

Python ml engine predict: How can I make a googleapiclient.discovery.build persistent?


I need to make online predictions from a model that is deployed in cloud ml engine. My code in python is similar to the one found in the docs (https://cloud.google.com/ml-engine/docs/tensorflow/online-predict):

service = googleapiclient.discovery.build('ml', 'v1')
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()

However, I receive the "instances" data from outside the script, I wonder if there is a way I could run this script without making the "service = googleapiclient.discovery.build('ml', 'v1')" each time before a request, since it takes time. pd: this is my very first project on gcp. Thank you.


Solution

  • Something like this will work. You'll want to initialize your service globally then use that service instance to make your call.

    import googleapiclient.discovery
    AI_SERVICE = None
    
    def ai_platform_init():
        global AI_SERVICE
        # Set GCP Authentication
        credentials = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
    
        # Path to your credentials
        credentials_path = os.path.join(os.path.dirname(__file__), 'ai-platform-credentials.json')
        if credentials is None and os.path.exists(credentials_path):
            os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
    
        # Create AI Platform Service
        if os.path.exists(credentials_path):
            AI_SERVICE = googleapiclient.discovery.build('ml', 'v1', cache=MemoryCache())
    
    # Initialize AI Platform on load.
    ai_platform_init()
    

    then later on, you can do something like this:

    def call_ai_platform():
       response = AI_SERVICE.projects().predict(name=name,
                                                body={'instances': instances}).execute()
    

    Bonus! in case you were curious about the MemoryCache class in the googleapiclient.discovery call, that was borrowed from another SO:

    class MemoryCache():
        """A workaround for cache warnings from Google.
        Check out: https://github.com/googleapis/google-api-python-client/issues/325#issuecomment-274349841
        """
        _CACHE = {}
    
        def get(self, url):
            return MemoryCache._CACHE.get(url)
    
        def set(self, url, content):
            MemoryCache._CACHE[url] = content