如何将Google App Engine与Cloud Natural Language结合 [英] How to combine Google App Engine with Cloud Natural Language

查看:49
本文介绍了如何将Google App Engine与Cloud Natural Language结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我想做的事情很简单,但事实并非如此.

I thought what I was trying to do would be simple, but that seems to not be the case.

我发现将自然语言API与Google Compute Engine结合使用非常简单,因为我可以简单地在Python中导入所需的库.

I've found that using the Natural Language API with Google Compute Engine is fairly straightforward, as I can simply import the needed libraries in Python.

App Engine似乎不是这种情况,因为导入错误困扰着我,一旦我解决了一个错误,又出现了另一个错误.

This does not seem to be the case with App Engine, as I am plagued by import errors, as soon as I fix one, another arises.

你们中有没有人曾经将这两项服务结合在一起?如果是,怎么做?

Have any of you ever worked to combine these two services, and if so, how?

谢谢

推荐答案

App Engine Standard尚不支持Google客户端库(我认为您正在尝试将其导入到您的应用程序中),因此它正在开发中,因此现在您可以尝试以下替代方法:

App Engine Standard does not yet support Google Client Libraries (which I assume you are trying to import into your application), it is work in the development, so by now you can try with the following alternatives:

  • App Engine Flexible::它确实支持客户端库,您只需将它们提供给应用程序即可,就像它们是第三方库一样.您可以遵循本指南以便适当地添加 google-api-python-client 库.
  • REST API :您可以使用REST API (已具有稳定版本 v1 ).它可能不如客户端库方便,但是您可以使用Python代码发出HTTP请求并处理它们的响应.
  • Compute Engine:,正如您在问题中指出的那样,您将能够在任何所需的计算机(本地或Compute Engine中的实例)上使用自定义Python运行时环境中的客户端库.
  • App Engine Flexible: it does support Client Libraries, you just have to vendor them into your application as if they were third-party libraries. You can follow this guide in order to add the google-api-python-client library appropriately.
  • REST API: you can use the REST API (which already has a stable version, v1). It might not be as convenient as Client Libraries, but you can make HTTP requests with your Python code and process their responses.
  • Compute Engine: as you pointed out in your question, you will be able to use Client Libraries from your custom Python runtime environment in any machine you want (either locally or an instance in Compute Engine).

更新:

UPDATE:

实际上,我已经对您的问题进行了更深入的研究,并且能够通过使用

Actually, I have looked deeper into your issue and have been able to solve it using App Engine Standard by using the Google API Client Library (not Google Client Libraries), which is an alternative version that is available for the Standard environment. Below I leave a small working piece of code which you can populate with your own data and try in App Engine environment or even with the Local Development server.

from apiclient.discovery import build

service = build('language', 'v1', developerKey='<YOUR_API_KEY>')                
collection = service.documents()

data = {}
data['document'] = {}
data['document']['language'] = 'en'
data['document']['content'] = 'I am really happy'
data['document']['type'] = 'PLAIN_TEXT'

request = collection.analyzeSentiment(body=data)
res = request.execute()

您将必须获取API密钥如文档中所述进行身份验证,并且您还需要按照

You will have to obtain an API key for authentication, as explained in the documentation, and you will also need to add the library as explained in the other link I shared.

最后,您在这里

Last, here you have the documentation on the available methods from the API. The example I provided is using analyzeSentiment(), but you can go with the one you need.

希望有帮助!

这篇关于如何将Google App Engine与Cloud Natural Language结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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