首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Google访问Google自然语言

用Google访问Google自然语言
EN

Stack Overflow用户
提问于 2022-04-01 19:31:28
回答 1查看 387关注 0票数 1

我试图使用Google的和Google。

我首先遵循Google的简单示例:https://cloud.google.com/natural-language/docs/samples/language-entity-sentiment-text#language_entity_sentiment_text-python

因此,我的Colab笔记本实际上只是一个代码单元:

代码语言:javascript
运行
复制
from google.cloud import language_v1

client = language_v1.LanguageServiceClient()

text_content = 'Grapes are good. Bananas are bad.'

# Available types: PLAIN_TEXT, HTML
type_ = language_v1.types.Document.Type.PLAIN_TEXT

# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
document = {"content": text_content, "type_": type_, "language": language}

# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = language_v1.EncodingType.UTF8

response = client.analyze_entity_sentiment(request = {'document': document, 'encoding_type': encoding_type})

这导致了几条错误消息,我似乎主要是在this SO post的帮助下,通过稍微更新代码来解决这些错误消息,如下所示:

代码语言:javascript
运行
复制
from google.cloud import language_v1

client = language_v1.LanguageServiceClient()

text_content = 'Grapes are good. Bananas are bad.'

# Available types: PLAIN_TEXT, HTML
type_ = language_v1.types.Document.Type.PLAIN_TEXT

# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
#document = {"content": text_content, "type_": type_, "language": language} ## "type_" is not valid???
document = {"content": text_content, "type": type_, "language": language}

# Available values: NONE, UTF8, UTF16, UTF32
#encoding_type = language_v1.EncodingType.UTF8 ## Does not seem to work
encoding_type = "UTF8"

#response = client.analyze_entity_sentiment(request = {'document': document, 'encoding_type': encoding_type}) ## remove request
response = client.analyze_entity_sentiment( document = document, encoding_type = encoding_type )

在10分钟后,这会导致以下错误:

代码语言:javascript
运行
复制
_InactiveRpcError                         Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/google/api_core/grpc_helpers.py in error_remapped_callable(*args, **kwargs)
     72         try:
---> 73             return callable_(*args, **kwargs)
     74         except grpc.RpcError as exc:

11 frames
_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "Getting metadata from plugin failed with error: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Status: 404 Response:\nb''", <google.auth.transport.requests._Response object at 0x7f68cee39a90>)"
    debug_error_string = "{"created":"@1648840699.964791285","description":"Getting metadata from plugin failed with error: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Status: 404 Response:\nb''", <google.auth.transport.requests._Response object at 0x7f68cee39a90>)","file":"src/core/lib/security/credentials/plugin/plugin_credentials.cc","file_line":91,"grpc_status":14}"
>

The above exception was the direct cause of the following exception:

ServiceUnavailable                        Traceback (most recent call last)
ServiceUnavailable: 503 Getting metadata from plugin failed with error: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Status: 404 Response:\nb''", <google.auth.transport.requests._Response object at 0x7f68cee39a90>)

The above exception was the direct cause of the following exception:

RetryError                                Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/six.py in raise_from(value, from_value)

RetryError: Deadline of 600.0s exceeded while calling functools.partial(<function _wrap_unary_errors.<locals>.error_remapped_callable at 0x7f68cedb69e0>, document {
  type: PLAIN_TEXT
  content: "Grapes are good. Bananas are bad."
  language: "en"
}
encoding_type: UTF8
, metadata=[('x-goog-api-client', 'gl-python/3.7.13 grpc/1.44.0 gax/1.26.3 gapic/1.2.0')]), last exception: 503 Getting metadata from plugin failed with error: ("Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Status: 404 Response:\nb''", <google.auth.transport.requests._Response object at 0x7f68cee39a90>)

你能帮我把这个简单的“你好世界!”对于云自然语言与谷歌Colab?

我的直觉是,我需要创建一个服务帐户,并以某种方式向Colab提供密钥文件,比如this SO answer。如果是这样的话,你能不能再握住我的手,告诉我如何在Colab (相对于本地运行)中实现这一点?我是科拉布的新手。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-01 20:26:38

这似乎奏效了:

首先创建一个服务帐户,生成一个密钥文件,并在本地保存JSON文件。https://console.cloud.google.com/iam-admin/serviceaccounts (我仍然想知道,如果在服务帐户生成中选择了哪些角色:“授予此服务帐户对项目的访问权限”)

单元格1:上传带有服务帐户密钥的json文件

代码语言:javascript
运行
复制
from google.colab import files
uploaded = files.upload()

第2室:

代码语言:javascript
运行
复制
from google.oauth2 import service_account
from google.cloud import language_v1

client = language_v1.LanguageServiceClient.from_service_account_json("my-super-important-gcp-key-file.json")

第3室:

代码语言:javascript
运行
复制
text_content = 'Grapes are good. Bananas are bad.'
type_ = language_v1.types.Document.Type.PLAIN_TEXT
language = "en"
document = {"content": text_content, "type": type_, "language": language}
encoding_type = "UTF8"
response = client.analyze_entity_sentiment( document = document, encoding_type = encoding_type )
response

这是输出:

代码语言:javascript
运行
复制
entities {
  name: "Grapes"
  type: OTHER
  salience: 0.8335162997245789
  mentions {
    text {
      content: "Grapes"
    }
    type: COMMON
    sentiment {
      magnitude: 0.800000011920929
      score: 0.800000011920929
    }
  }
  sentiment {
    magnitude: 0.800000011920929
    score: 0.800000011920929
  }
}
entities {
  name: "Bananas"
  type: OTHER
  salience: 0.16648370027542114
  mentions {
    text {
      content: "Bananas"
      begin_offset: 17
    }
    type: COMMON
    sentiment {
      magnitude: 0.699999988079071
      score: -0.699999988079071
    }
  }
  sentiment {
    magnitude: 0.699999988079071
    score: -0.699999988079071
  }
}
language: "en"

我确信我刚刚违反了各种安全协议。因此,请允许我就如何改进这一进程提出任何建议。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71711943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档