【问题标题】:Calling Google ML-engine's predict from Firebase Cloud Functions从 Firebase Cloud Functions 调用 Google ML 引擎的预测
【发布时间】:2018-07-21 18:50:56
【问题描述】:
我尝试使用邮递员的HTTP API method 对我已部署的模型之一调用预测请求,并得到以下响应:
{
“错误”: {
“代码”:401,
"message": "请求缺少所需的身份验证凭据。需要 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。请参阅 https://developers.google.com/identity/sign-in/web/devconsole-project。",
“状态”:“未经身份验证”
}
}
我意识到我们需要身份验证,因此我尝试使用 Firebase Cloud Functions 进行相同的 HTTP 调用,但仍然得到与上述相同的响应。我进行了一些挖掘,发现了所有可以与云功能一起使用的services,我在其中看到了 ML Engine。
我在模型的权限选项卡中将 Cloud Functions 服务帐户添加为 ML Engine Owner,希望它添加 API 调用所需的身份验证,但仍然无法正常工作。
我不想同时使用 cli 或 python-client-library,目的是使这项工作无需服务器。
谁能帮助我了解为什么会发生这种情况,或者我还能如何对预测请求进行 HTTP 调用?
谢谢。
【问题讨论】:
标签:
firebase
google-cloud-platform
google-cloud-functions
google-cloud-ml
google-iam
【解决方案2】:
对我来说,它的工作原理如下。在同一个谷歌云项目中,我部署了 ML 模型(ML 平台 -> 模型)和 Cloud Function,我创建了角色为“Cloud ML Developer”的服务帐户。必须在 Cloud Function 配置中提供创建的服务帐户名称:
云功能代码:
main.py
googleapiclient import discovery
import json
def run(request):
request_json = request.get_json()
if request.args and 'message' in request.args:
return request.args.get('message')
elif request_json and 'message' in request_json:
return request_json['message']
elif request_json and hasattr(request_json, "__len__"):
res = ml_call(prepare_frame(request_json))
return json.dumps(res)
else:
return f'Request error'
def ml_call(req):
PROJECT = 'test_proj'
MODEL_NAME = 'test_name'
MODEL_VERSION = 'test_ver'
parent = 'projects/{}/models/{}/versions/{}'.format(PROJECT, MODEL_NAME, MODEL_VERSION)
# Build a representation of the Cloud ML API.
ml = discovery.build('ml', 'v1')
# Create a dictionary with the fields from the request body.
data = {'instances': [{'input_image': req}]}
# Create a request
request = ml.projects().predict(name = parent, body = data)
response = request.execute()
return response
def prepare_frame(xxx):
...
return x
requirements.txt:
google-api-python-client