在没有gcloud的生产环境中使用CloudML预测API [英] Using CloudML prediction API in production without gcloud

查看:36
本文介绍了在没有gcloud的生产环境中使用CloudML预测API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生产服务中使用CloudML预测API的最佳方法是什么?

What is the best way to use CloudML prediction API in production service?

我看过: https://cloud.google.com/ml/docs/quickstarts/prediction 但是它依赖于gcloud工具

I've seen: https://cloud.google.com/ml/docs/quickstarts/prediction but it relies on gcloud tool

我正在研究不依赖于在发出请求的计算机上安装和初始化gcloud的解决方案.拥有在GCP,AWS以及可能在其他云上运行的解决方案将是很棒的.

I'm looking into solution that doesn't depend on having gcloud installed and initialized on machine making the request. It would be great to have solution that works on GCP, AWS and possibly other clouds.

谢谢

推荐答案

我将向您展示如何对生产环境进行身份验证以使用CloudML在线预测. CloudML快速入门使用gcloud通过用户名,密码等对最终用户进行身份验证.gcloud不能很好地扩展到具有100台计算机启动和停止的环境.下面,我将引导您完成创建Cloud 服务帐户并生成私有密钥的步骤,通过这些步骤,您的生产实例可以在Google服务器上标识自己.请参见此处.

I'll show you how to authenticate your production environment to use CloudML online prediction. The CloudML quickstarts use gcloud to authenticate the end-user via username, password etc. gcloud doesn’t scale well to environments with 100’s of machines starting and stopping. Below, I walk you through the steps of creating a Cloud service account and generating a private key to which will allow your production instances to identify themselves to the Google servers. See authentication documentation here.

这是您可以使用的食谱.

Here's a recipe you can use.

PROJECT=
MODEL_NAME=
SERVICE_ACCOUNT_PREFIX=cloud-ml-predict
SERVICE_ACCOUNT="${SERVICE_ACCOUNT_PREFIX}@${PROJECT}.iam.gserviceaccount.com"

这些步骤仅需执行一次,并将为您创建一个服务帐户和私钥.

These steps have to be done only once, and will create a service account and private key for you.

# Make a new service account
gcloud iam service-accounts create  ${SERVICE_ACCOUNT_PREFIX} \
  --display-name ${SERVICE_ACCOUNT_PREFIX}

# Provide correct role to service account permissions:
gcloud projects add-iam-policy-binding $PROJECT \
  --member "serviceAccount:$SERVICE_ACCOUNT" --role roles/viewer

# Create private key for the service account:
gcloud iam service-accounts keys create --iam-account \
  $SERVICE_ACCOUNT private_key.json

现在有了私钥(在private_key.json中),我们可以从具有googleapiclient Python库的任何计算机上调用预测API.现在,无论有没有gcloud的任何计算机,您只需要包括以下行即可通过HTTP访问CloudML预测服务

Now that we have a private key (in private_key.json) we can call the prediction API from any machine that has the googleapiclient Python library. Now from any machine with or without gcloud you only need to include the following lines to access the CloudML prediction service via HTTP

scopes = ['https://www.googleapis.com/auth/cloud-platform']
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_filename, scopes=scopes)
ml_service = discovery.build('ml', 'v1beta1', credentials=credentials)

最后,这是一个工作示例,假设您已从 quickstarts .

Finally, here's a worked example assuming you have a the MNIST model deployed from the quickstarts.

cat > key_pair_cloud_ml_serve.py <<EOD
from googleapiclient import discovery
import json
from oauth2client.service_account import ServiceAccountCredentials
import sys

def get_mnist_prediction(ml_service, project, model_name, instance):
  parent = 'projects/{}/models/{}'.format(project, model_name)
  request_dict = {'instances': [json.loads(instance)]}

  request = ml_service.projects().predict(name=parent, body=request_dict)
  print request.execute()  # waits till request is returned

if __name__ == '__main__':
  usage_str = 'usage: python prog private_key.json MODEL_NAME data/predict*json'
  assert len(sys.argv) == 4, usage_str

  key_file = sys.argv[1]
  model_name = sys.argv[2]
  data_file = sys.argv[3]

  scopes = ['https://www.googleapis.com/auth/cloud-platform']
  credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file,
scopes=scopes)
  ml_service = discovery.build('ml', 'v1beta1', credentials=credentials)
  with open(key_file) as ff:
    project = json.load(ff)['project_id']


  with open(data_file) as ff:
    for ii, instance in enumerate(ff):
      get_mnist_prediction(ml_service, project, model_name, instance)
EOD

Cloud ML示例mnist/deployable文件夹中,我们将代码称为代码". ..

And from within the mnist/deployable folder of the Cloud ML samples we call our code...

python key_pair_cloud_ml_serve.py private_key.json \
  $MODEL_NAME data/predict_sample.tensor.json


{u'predictions': [{u'prediction': 5, u'key': 0, u'scores': [0.04025577753782272, 0.00042669562390074134, 0.005919951014220715, 0.4221051335334778, 2.2986243493505754e-05, 0.5084351897239685, 0.0007824163185432553, 0.01125132292509079, 0.008616944774985313, 0.0021835025399923325]}]}

Voila!我们使用了私有密钥,而无需使用gcloud进行身份验证或查询我们的预测模型!

Voila! We used a private key and never needed to use gcloud for authentication or querying our prediction model!

这篇关于在没有gcloud的生产环境中使用CloudML预测API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆