内部 lambda 函数 - Blazing 文本算法调用端点不支持输入内容类型
Inside lambda function - Blazing text algorithm invoke endpoint doesn't support the input content type
我正在使用内置的 blazing 文本算法进行句子分类,同时在 lambda 函数中调用端点时遇到内容类型不匹配错误。
-- 对于 blazing 文本,它仅支持 application/jsonlines 或 application/json 但在调用时,它会遇到错误,例如,它只接受字节或字节数组
input format . application/json
event={
"features": [
"sensor_subtype Thermostats Thermal Switches product_features Hermetically sealed n Tight tolerances n Tight differentials n Logic level contacts n applications Computers n Medical electronics n Power supplies n Industrial controls n Test equipment n Infotech n description Technical Specifications technical_specs CloseTolerance 2 8 C 5 F DielectricStrength MIL STD 202 Method 301 1250 Vac 60 Hz Terminal to Case ContactResistance MIL STD"
]
}
我也试过了application/jsonlines
我的代码是这样的>>>>>>>>>>>>>>>>>>>>>>>>>>
def transform_data(data):
try:
features = data.copy()
return features
except Exception as err:
print('Error when transforming: {0},{1}'.format(data,err))
raise Exception('Error when transforming: {0},{1}'.format(data,err))
def lambda_handler(event, context):
try:
print("Received event: " + json.dumps(event, indent=2))
request = json.loads(json.dumps(event))
transformed_data = str(transform_data(request['features'])) #for instance in request['features'])
print(ENDPOINT_NAME, "------->>>>")
payload=transformed_data
result = client.invoke_endpoint(EndpointName=ENDPOINT_NAME,
Body=(payload.encode('utf-8')),
ContentType='application/json')
return result
"statusCode": 400,
"isBase64Encoded": false,
"body": "Call Failed An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (406) from model with message \"Invalid payload format\".
_______________LOGS__________________________________
11:35:22
[08/18/2019 11:35:22 ERROR 140074862942016] Customer Error: Unable to decode payload: Incorrect data format. (caused by ValueError)
11:35:22
Caused by: No JSON object could be decoded
11:35:22
Traceback (most recent call last): File "/opt/amazon/lib/python2.7/site-packages/blazingtext/serve.py", line 317, in invocations data = json.loads(payload.decode("utf-8")) File "/opt/amazon/python2.7/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/opt/amazon/python2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.
11:35:22
ValueError: No JSON object could be decoded
我需要使用 invoke_endpoint 选项实时预测句子,但它显示有效载荷格式无效
我尝试了字节格式和 apllication/jsonlines 格式。
尝试在调用端点传递以下内容
Body=json.dumps(payload.encode('utf-8')
我在尝试使用 BlazingText 容器预测文本分类时遇到了同样的问题。对我有用的是简单地更改有效负载中的密钥,同时将 ContentType 保持为 application/json:
sentence = "I'm selling my PS4, practically brand new"
payload = {"instances": [sentence]}
response = client.invoke_endpoint(
EndpointName="text_classification",
Body=json.dumps(payload),
ContentType='application/json'
)
在对有效载荷进行了一些研究之后,似乎 blazing 文本模型只接受有效载荷作为一个字典,以“实例”作为它的键,一个包含你想要预测的数据的列表作为它的值。
简单地进行预测:
print("ResponseMetadata:", response["ResponseMetadata"])
print()
print("Body:", response['Body'].read())
还要确保以 Json 格式获得您需要更改的响应:
print("Body:", response['Body'].read())
到
print("Body:", response['Body'].read().decode())
我正在使用内置的 blazing 文本算法进行句子分类,同时在 lambda 函数中调用端点时遇到内容类型不匹配错误。
-- 对于 blazing 文本,它仅支持 application/jsonlines 或 application/json 但在调用时,它会遇到错误,例如,它只接受字节或字节数组
input format . application/json
event={
"features": [
"sensor_subtype Thermostats Thermal Switches product_features Hermetically sealed n Tight tolerances n Tight differentials n Logic level contacts n applications Computers n Medical electronics n Power supplies n Industrial controls n Test equipment n Infotech n description Technical Specifications technical_specs CloseTolerance 2 8 C 5 F DielectricStrength MIL STD 202 Method 301 1250 Vac 60 Hz Terminal to Case ContactResistance MIL STD"
]
}
我也试过了application/jsonlines
我的代码是这样的>>>>>>>>>>>>>>>>>>>>>>>>>>
def transform_data(data):
try:
features = data.copy()
return features
except Exception as err:
print('Error when transforming: {0},{1}'.format(data,err))
raise Exception('Error when transforming: {0},{1}'.format(data,err))
def lambda_handler(event, context):
try:
print("Received event: " + json.dumps(event, indent=2))
request = json.loads(json.dumps(event))
transformed_data = str(transform_data(request['features'])) #for instance in request['features'])
print(ENDPOINT_NAME, "------->>>>")
payload=transformed_data
result = client.invoke_endpoint(EndpointName=ENDPOINT_NAME,
Body=(payload.encode('utf-8')),
ContentType='application/json')
return result
"statusCode": 400,
"isBase64Encoded": false,
"body": "Call Failed An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (406) from model with message \"Invalid payload format\".
_______________LOGS__________________________________
11:35:22
[08/18/2019 11:35:22 ERROR 140074862942016] Customer Error: Unable to decode payload: Incorrect data format. (caused by ValueError)
11:35:22
Caused by: No JSON object could be decoded
11:35:22
Traceback (most recent call last): File "/opt/amazon/lib/python2.7/site-packages/blazingtext/serve.py", line 317, in invocations data = json.loads(payload.decode("utf-8")) File "/opt/amazon/python2.7/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/opt/amazon/python2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.
11:35:22
ValueError: No JSON object could be decoded
我需要使用 invoke_endpoint 选项实时预测句子,但它显示有效载荷格式无效
我尝试了字节格式和 apllication/jsonlines 格式。
尝试在调用端点传递以下内容
Body=json.dumps(payload.encode('utf-8')
我在尝试使用 BlazingText 容器预测文本分类时遇到了同样的问题。对我有用的是简单地更改有效负载中的密钥,同时将 ContentType 保持为 application/json:
sentence = "I'm selling my PS4, practically brand new"
payload = {"instances": [sentence]}
response = client.invoke_endpoint(
EndpointName="text_classification",
Body=json.dumps(payload),
ContentType='application/json'
)
在对有效载荷进行了一些研究之后,似乎 blazing 文本模型只接受有效载荷作为一个字典,以“实例”作为它的键,一个包含你想要预测的数据的列表作为它的值。
简单地进行预测:
print("ResponseMetadata:", response["ResponseMetadata"])
print()
print("Body:", response['Body'].read())
还要确保以 Json 格式获得您需要更改的响应:
print("Body:", response['Body'].read())
到
print("Body:", response['Body'].read().decode())