gRPC 无法连接到所有地址 - Python

gRPC failed to connect to all addresses - Python

我正在尝试连接到 Clarifai 的 'general' 图像分类模型以使用以下 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:\Users\Eshaan\Desktop\moveForward\Python\Level 4\New try\Vision MFSA.py", line 45, in <module>
    post_model_outputs_response = stub.PostModelOutputs(
  File "C:\Users\Eshaan\AppData\Local\Programs\Python\Python39\lib\site-packages\grpc\_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Users\Eshaan\AppData\Local\Programs\Python\Python39\lib\site-packages\grpc\_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}]}"
>

任何 idea/suggestion 为什么会这样以及如何解决它?我参考了以下文档: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