Python 函数中的 Base64 解码失败错误
Python Base64 decode failed error in a function
我使用以下函数时出现 "Base64 decode failed"
错误。以下函数用于调用保存在Google AI Platform 中的模型。但是,数据输入必须进行 base64 序列化,因此,我在 tfx_test(request) 中包含了 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 编码每三个字符将它们编码为四个。
我使用以下函数时出现 "Base64 decode failed"
错误。以下函数用于调用保存在Google AI Platform 中的模型。但是,数据输入必须进行 base64 序列化,因此,我在 tfx_test(request) 中包含了 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 编码每三个字符将它们编码为四个。