'How to return a float with 'text/csv' as "Content-Type" from SageMaker endpoint that uses custom inference code?
I am trying to return output(or predictions) from a SageMaker endpoint with 'text/csv' or 'text/csv; charset=utf-8' as "Content-Type". I have tried multiple ways, but the sagemaker always returns with 'text/html; charset=utf-8' as the "Content-Type", and I would like SageMaker to return 'text/csv' or 'text/csv; charset=utf-8'.
Here's the output_fn from my inference-code:
** my other code **
def output_fn(prediction, content_type='text/csv'):
** my other code **
return output_float
above function returns number with float data-type and I got error(in cloudwatch logs) that this function should only be returning string, tuple, dict or Respoonse instance.
So, here are all the different ways I have tried to have SageMaker return my number with 'text/csv' but only receives 'text/html; charset=utf-8'
return json.dumps(output_float). this sent 'text/html; charset=utf-8'.return f"{output_float}". this sent 'text/html; charset=utf-8'.return f"{output_float},". this sent 'text/html; charset=utf-8'.return f"{output_float}\n". this sent 'text/html; charset=utf-8'.return f"{output_float},\n". this sent 'text/html; charset=utf-8'.- by using
sagemaker.serializers.CSVSerializerlike this:
I gotfrom 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'error with this. - as a tuple:
return (output_float,). I haven't noted down what this did, but it sure didn't return the number with 'text/csv' as "Content-Type". - made changes in my model object to return a float on calling
.predict_probaon my model-object and deployed it from sagemaker studio without using any custom inference code and deployed this from SageMaker studio. but got this error after sending a request to the endpoint:'NoneType' object has no attribute 'startswith', but at my side, when I pass proper inputs to the unpicked model and call .predict_proba, i get float as expected. - by returning
flask.Responselike this:
but, I got some IndexError with this(I didn't notedown the traceback.)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'})
Some other info:
- Model I am using is completely outside of SageMaker, not from a training job or anything like that.
- All of the aforementioned endpoints have been deployed entirely from aws-cli with relevant .json files, except the one in point-8 above.
How do I return number with "text/csv" as content-type from sagemaker? I need my output in "text/csv" content-type specifically for Model-Quality-Monitor. How do I do this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
