Python ml 引擎预测:如何使 googleapiclient.discovery.build 持久化?

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

我需要根据部署在云 ML 引擎中的模型进行在线预测。我在 python 中的代码类似于在文档 (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()

但是,我从脚本外部收到了 "instances" 数据,我想知道是否有一种方法可以 运行 这个脚本,而无需在每次请求之前都进行 "service = googleapiclient.discovery.build('ml', 'v1')" ,因为这需要时间。 pd:这是我在 gcp 上的第一个项目。谢谢。

像这样的东西会起作用。您需要全局初始化您的服务,然后使用该服务实例进行调用。

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()

之后,您可以这样做:

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

奖金!如果您对 googleapiclient.discovery 调用中的 MemoryCache class 感到好奇,那是从另一个 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