如何从使用自定义推理代码的 SageMaker 端点 return 一个带有 'text/csv' 的浮点数作为 "Content-Type"?
How to return a float with 'text/csv' as "Content-Type" from SageMaker endpoint that uses custom inference code?
我正在尝试从 SageMaker 端点 return 输出(或预测)'text/csv' 或 'text/csv; charset=utf-8' 作为“内容类型”。我尝试了多种方法,但 sagemaker 总是 returns 以 'text/html; charset=utf-8' 作为“Content-Type”,我希望 SageMaker 为 return 'text/csv' 或 'text/csv; charset=utf-8'.
这是我的推理代码中的 output_fn
:
** my other code **
def output_fn(prediction, content_type='text/csv'):
** my other code **
return output_float
上面的函数 returns 数字与 float 数据类型,我得到错误(在 cloudwatch 日志中)这个函数应该只 returning 字符串,元组,字典或响应实例。
所以,这是我尝试使用 SageMaker return 我的号码 'text/csv' 但只收到 'text/html; charset=utf-8'
的所有不同方式
return json.dumps(output_float)
。这发送了 'text/html; charset=utf-8'.
return f"{output_float}"
。这发送了 'text/html; charset=utf-8'.
return f"{output_float},"
。这发送了 'text/html; charset=utf-8'.
return f"{output_float}\n"
。这发送了 'text/html; charset=utf-8'.
return f"{output_float},\n"
。这发送了 'text/html; charset=utf-8'.
- 通过像这样使用
sagemaker.serializers.CSVSerializer
:
from sagemaker.serializers import CSVSerializer
csv_serialiser = CSVSerializer(content_type='text/csv')
def output_fn(prediction, content_type='text/csv'):
** my other code **
return csv_serialiser.serialize(output_float)
我遇到了 'NoneType' object has no attribute 'startswith'
错误。
- 作为元组:
return (output_float,)
。我没有记下它做了什么,但它肯定没有 return 带有 'text/csv' 的数字作为“Content-Type”。
- 将我的模型对象更改为 return 在我的模型对象上调用
.predict_proba
时的浮动,并在不使用任何自定义推理代码的情况下从 sagemaker studio 部署它,并从 SageMaker studio 部署它。但是在向端点发送请求后出现此错误:'NoneType' object has no attribute 'startswith'
,但在我这边,当我将适当的输入传递给未选择的模型并调用时。predict_proba,我得到了预期的浮动。
- 通过 returning
flask.Response
像这样:
from flask import Response
def output_fn(prediction, content_type='text/csv'):
** my other code **
return Response(response=output_float, status=200, headers={'Content-Type':'text/csv; charset=utf-8'})
但是,我遇到了一些 IndexError(我没有记下回溯。)
一些其他信息:
- 我使用的模型完全在 SageMaker 之外,而不是来自培训工作或类似的东西。
- 上述所有端点都已完全从 aws-cli 与相关的 .json 文件部署,除了上面第 8 点中的那个。
如何从 sagemaker return 以“text/csv”作为内容类型的号码?我需要在“text/csv”内容类型中专门为模型质量监视器输出。我该怎么做?
从 scikit_bring_your_own 示例中,我建议通过如下设置 Response 进行测试:
return flask.Response(response= output_float, status=200, mimetype="text/csv")
我正在尝试从 SageMaker 端点 return 输出(或预测)'text/csv' 或 'text/csv; charset=utf-8' 作为“内容类型”。我尝试了多种方法,但 sagemaker 总是 returns 以 'text/html; charset=utf-8' 作为“Content-Type”,我希望 SageMaker 为 return 'text/csv' 或 'text/csv; charset=utf-8'.
这是我的推理代码中的 output_fn
:
** my other code **
def output_fn(prediction, content_type='text/csv'):
** my other code **
return output_float
上面的函数 returns 数字与 float 数据类型,我得到错误(在 cloudwatch 日志中)这个函数应该只 returning 字符串,元组,字典或响应实例。
所以,这是我尝试使用 SageMaker return 我的号码 'text/csv' 但只收到 'text/html; charset=utf-8'
的所有不同方式return json.dumps(output_float)
。这发送了 'text/html; charset=utf-8'.return f"{output_float}"
。这发送了 'text/html; charset=utf-8'.return f"{output_float},"
。这发送了 'text/html; charset=utf-8'.return f"{output_float}\n"
。这发送了 'text/html; charset=utf-8'.return f"{output_float},\n"
。这发送了 'text/html; charset=utf-8'.- 通过像这样使用
sagemaker.serializers.CSVSerializer
:
我遇到了from sagemaker.serializers import CSVSerializer csv_serialiser = CSVSerializer(content_type='text/csv') def output_fn(prediction, content_type='text/csv'): ** my other code ** return csv_serialiser.serialize(output_float)
'NoneType' object has no attribute 'startswith'
错误。 - 作为元组:
return (output_float,)
。我没有记下它做了什么,但它肯定没有 return 带有 'text/csv' 的数字作为“Content-Type”。 - 将我的模型对象更改为 return 在我的模型对象上调用
.predict_proba
时的浮动,并在不使用任何自定义推理代码的情况下从 sagemaker studio 部署它,并从 SageMaker studio 部署它。但是在向端点发送请求后出现此错误:'NoneType' object has no attribute 'startswith'
,但在我这边,当我将适当的输入传递给未选择的模型并调用时。predict_proba,我得到了预期的浮动。 - 通过 returning
flask.Response
像这样:
但是,我遇到了一些 IndexError(我没有记下回溯。)from flask import Response def output_fn(prediction, content_type='text/csv'): ** my other code ** return Response(response=output_float, status=200, headers={'Content-Type':'text/csv; charset=utf-8'})
一些其他信息:
- 我使用的模型完全在 SageMaker 之外,而不是来自培训工作或类似的东西。
- 上述所有端点都已完全从 aws-cli 与相关的 .json 文件部署,除了上面第 8 点中的那个。
如何从 sagemaker return 以“text/csv”作为内容类型的号码?我需要在“text/csv”内容类型中专门为模型质量监视器输出。我该怎么做?
从 scikit_bring_your_own 示例中,我建议通过如下设置 Response 进行测试:
return flask.Response(response= output_float, status=200, mimetype="text/csv")