Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve daily precomputed predictions in aws sagemaker?

I'm trying to use Sagemaker to serve precomputed predictions. The predictions are in the following format in a python dictionary.

customer_group prediction
1              50
2              60
3              25
4              30
...

Currently the docker serve API code goes to s3 and downloads the data daily.

The problem is that downloading the data blocks the api from responding to the Sagemaker health endpoint calls.

This a case study of how zappos did it using Amazon DynamoDB. However, is there a way to do it in Sagemaker?

Where and how can I add the s3 download function to avoid interrupting the health check?

Could this work? -> https://github.com/seomoz/s3po https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-email-support

app = flask.Flask(__name__)

@app.route('/ping', methods=['GET'])
def ping():
    """Determine if the container is working and healthy. In this sample container, we declare
    it healthy if we can load the model successfully."""
    health = ScoringService.get_model() is not None  # You can insert a health check here

    status = 200 if health else 404
    return flask.Response(response='\n', status=status, mimetype='application/json')

@app.route('/invocations', methods=['POST'])
def transformation():
    """Do an inference on a single batch of data. In this sample server, we take data as CSV, convert
    it to a pandas data frame for internal use and then convert the predictions back to CSV (which really
    just means one prediction per line, since there's a single column.
    """
    data = None

    # Convert from CSV to pandas
    if flask.request.content_type == 'text/csv':
        data = flask.request.data.decode('utf-8')
        s = StringIO.StringIO(data)
        data = pd.read_csv(s, header=None)
    else:
        return flask.Response(response='This predictor only supports CSV data', status=415, mimetype='text/plain')

    print('Invoked with {} records'.format(data.shape[0]))

    # Do the prediction
    predictions = ScoringService.predict(data)

    # Convert from numpy back to CSV
    out = StringIO.StringIO()
    pd.DataFrame({'results':predictions}).to_csv(out, header=False, index=False)
    result = out.getvalue()

    return flask.Response(response=result, status=200, mimetype='text/csv')
like image 335
italo Avatar asked Oct 30 '25 11:10

italo


1 Answers

Why not call batch transform instead and let AWS do the heavy lifting.

You can either schedule to be completed every day, or instead trigger it manually.

After this use either API Gateway with a Lambda function or CloudFront to display the results from S3.

like image 100
Chris Williams Avatar answered Nov 02 '25 02:11

Chris Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!