调用Google ML-engine可以预测Firebase云功能

问题描述 投票:0回答:2

我试图使用postman的HTTP API method在我部署的一个模型上调用预测请求,我得到了这个作为响应:

{“error”:{“code”:401,“message”:“请求缺少必需的身份验证凭据。预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project。”,“status”:“UNAUTHENTICATED” }}

我意识到我们需要身份验证,所以我尝试使用Firebase云功能进行相同的HTTP调用,我仍然得到与上面相同的响应。我做了一些挖掘,遇到了所有可用于云功能的services,我看到了ML Engine。

我在我的模型的权限选项卡中添加了云功能服务帐户作为ML引擎所有者,期望它添加API调用所需的身份验证,但仍无效。

我不想同时使用cli或python-client-library,目的是使这个工作服务器更少。

任何人可以帮助我解决为什么会发生这种情况或者我如何对预测请求进行HTTP调用?

谢谢。

firebase google-cloud-platform google-cloud-functions google-cloud-ml google-iam
2个回答
2
投票

您是否为http请求设置了授权标头?授权:持票人

这里有一些关于cloud ml引擎的文档:https://cloud.google.com/ml-engine/docs/access-control

另一个Google Cloud功能的文档(概念是相同的):https://cloud.google.com/vision/docs/auth#using_a_service_account

顺便说一句,以防万一,函数不是必须的,我相信你可以从你的本机应用程序调用标题中的ApiKey。


0
投票

对我来说,它的工作原理如下。在同一个谷歌云项目中我部署了ML模型(ML平台 - >模型)和云功能,我创建了角色为“Cloud ML Developer”的服务帐户。必须在云功能配置中提供创建的服务帐户名称:

enter image description here云功能代码: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
© www.soinside.com 2019 - 2024. All rights reserved.