Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数中的Python Base64解码失败错误_Python_Python 3.x_Base64 - Fatal编程技术网

函数中的Python Base64解码失败错误

函数中的Python Base64解码失败错误,python,python-3.x,base64,Python,Python 3.x,Base64,使用以下函数时,我发现错误为“Base64解码失败”。以下函数用于调用保存在Google AI平台中的模型。但是,数据输入必须是base64序列化的,因此,我在tfx_测试(请求)中包含get_serialized_example(raw)函数。任何帮助和建议都将不胜感激。提前谢谢 def tfx_test(request): #User Inputs project = request.project model = request.model sig

使用以下函数时,我发现错误为
“Base64解码失败”
。以下函数用于调用保存在Google AI平台中的模型。但是,数据输入必须是base64序列化的,因此,我在tfx_测试(请求)中包含get_serialized_example(raw)函数。任何帮助和建议都将不胜感激。提前谢谢

def tfx_test(request):
    
    #User Inputs
    project = request.project
    model = request.model
    signature = request.signature
    version = request.version
    
    #Data inputs Base64 encoder 
    
    def get_serialized_example(raw):
        return tf.train.Example(
                features=tf.train.Features(
                  feature={"value":
                              tf.train.Feature(bytes_list=tf.train.BytesList(value=[raw]))
                          }
                    )
                ).SerializeToString()


    b64_country_code = base64.b64encode(get_serialized_example(request.country_code)).decode('utf-8')
    
    b64_project_type = base64.b64encode(get_serialized_example(request.project_type)).decode('utf-8')
    
    # ml.googleapis.com
    service = googleapiclient.discovery.build('ml', 'v1')
    name = 'projects/{}/models/{}'.format(project, model)

    if version is not None:
        name += '/versions/{}'.format(version)

    response = service.projects().predict(
        name=name,
        body={
            'signature_name': signature,
            'instances': [
                    {
                       "examples":{"b64": b64_country_code[0],
                                   "b64": b64_project_type[0]}
                    }]
        }
    ).execute()

    if 'error' in response:
        raise RuntimeError(response['error'])

    return response['predictions']

您似乎没有输入有效的base64字符串。相反,您只发送第一个字符:

"examples":{"b64": b64_country_code[0],
            "b64": b64_project_type[0]}

base-64字符串的第一个字符不是有效的base-64字符串,因为base64编码每三个字符将其编码为四个。

您的代码中是否有错误,还是来自Google?您好,谢谢您的评论。如果我去掉[0],我有以下错误:TypeError:列表索引必须是整数或切片,而不是str。您是从Google还是从客户端代码?从客户端。数据输入如下:class request():country_code=b“AU”project_type=b“Delivery”。我在函数上运行了一个单元测试,这也是我得到的错误。对不起,您必须提供更多信息-可能在一个新问题中提供所有详细信息。