gRPC连接到所有地址失败- Python



我正在尝试连接到Clarifai的'通用'图像分类模型,使用以下Python脚本标记图像:

#python program to analyze an image and label it
##############################################################################
# Installation
##############################################################################
#pip install clarifai-grpc
##############################################################################
## Initialize client
##     - This initializes the gRPC based client to communicate with the 
##       Clarifai platform. 
##############################################################################
## Import in the Clarifai gRPC based objects needed
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_pb2, status_code_pb2
## Construct the communications channel and the object stub to call requests on.
# Note: You can also use a secure (encrypted) ClarifaiChannel.get_grpc_channel() however
# it is currently not possible to use it with the latest gRPC version
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

################################################################################
## Set up Personal Access Token and Access information
##     - This will be used by every Clarifai API call 
################################################################################
## Specify the Authorization key.  This should be changed to your Personal Access Token.
## Example: metadata = (('authorization', 'Key 123457612345678'),) 
metadata = (('authorization', 'Key <PERSONAL ACCESS TOKEN>'),)
##
## A UserAppIDSet object is needed for most rpc calls.  This object contains
## two pieces of information: the user id and the app id.  Both of these are
## specified as string values.
##
##     'user_id' : This is your user id
##     'app_id'  : This is the app id which contains the model of interest
userDataObject = resources_pb2.UserAppIDSet(user_id='<USERNAME>', app_id='<APP ID>')
# Insert here the initialization code as outlined on this page:
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
post_model_outputs_response = stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject,  # The userDataObject is created in the overview and is required when using a PAT
model_id="aaa03c23b3724a16a56b629203edc62c",
version_id="aa7f35c01e0642fda5cf400f543e7c40",  # This is optional. Defaults to the latest model version.
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
url="https://samples.clarifai.com/metro-north.jpg"
)
)
)
]
),
metadata=metadata
)
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
print("There was an error with your request!")
print("tCode: {}".format(post_model_outputs_response.outputs[0].status.code))
print("tDescription: {}".format(post_model_outputs_response.outputs[0].status.description))
print("tDetails: {}".format(post_model_outputs_response.outputs[0].status.details))
raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)
# Since we have one input, one output will exist here.
output = post_model_outputs_response.outputs[0]
print("Predicted concepts:")
for concept in output.data.concepts:
print("%s %.2f" % (concept.name, concept.value))

但是,我得到以下错误:

Traceback (most recent call last):
File "C:UsersEshaanDesktopmoveForwardPythonLevel 4New tryVision MFSA.py", line 45, in <module>
post_model_outputs_response = stub.PostModelOutputs(
File "C:UsersEshaanAppDataLocalProgramsPythonPython39libsite-packagesgrpc_channel.py", line 946, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "C:UsersEshaanAppDataLocalProgramsPythonPython39libsite-packagesgrpc_channel.py", line 849, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "failed to connect to all addresses"
debug_error_string = "{"created":"@1634655221.541000000","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3009,"referenced_errors":[{"created":"@1634655221.541000000","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":398,"grpc_status":14}]}"
>

有什么想法/建议,为什么这是和如何解决它?我参考了以下文档:https://docs.clarifai.com/api-guide/predict/images

我没有从我这边添加一行代码,它都来自文档加上所需的键。我已经看了网站上所有的相关答案,但还是没能弄明白。

目前gRPC在如何支持某些证书方面存在问题。您可以更新到最新版本以修复此问题,或者您可以使用稍微不同的方法并更改:

channel = ClarifaiChannel.get_grpc_channel()

channel = ClarifaiChannel.get_json_channel()

这将通过在幕后使用REST基本上回避问题。

(注意:程序的其余部分,包括解析,即使有了这个更改,也将继续按预期工作,它只是改变了通信方法。)

gRPC Core的新版本1.41.1修复了它https://github.com/grpc/grpc/pull/27539

相关内容

  • 没有找到相关文章

最新更新