def transform(self):  # type: () -> _worker.Response
        """Responsible to make predictions against the model.

        Returns:
            (worker.Response): a Flask response object with the following args:

                * Args:
                    response: the serialized data to return
                    accept: the content-type that the data was transformed to.
        """
        request = _worker.Request()

        try:
            data = self._input_fn(request.content, request.content_type)
        except _errors.UnsupportedFormatError as e:
            return self._error_response(e, http_client.UNSUPPORTED_MEDIA_TYPE)

        prediction = self._predict_fn(data, self._model)

        try:
            result = self._output_fn(prediction, request.accept)
        except _errors.UnsupportedFormatError as e:
            return self._error_response(e, http_client.NOT_ACCEPTABLE)

        if isinstance(result, tuple):
            # transforms tuple in Response for backwards compatibility
            return _worker.Response(response=result[0], accept=result[1])

        return result
Example #2
0
 def _error_response(self, error, status_code):
     body = json.dumps({
         'error': error.__class__.__name__,
         'error-message': str(error),
         'stack-trace': traceback.format_exc()
     })
     return _worker.Response(response=body, status=status_code)
Example #3
0
 def _error_response(self, error, status_code):  # pylint: disable=no-self-use
     """Placeholder docstring"""
     body = json.dumps({
         "error": error.__class__.__name__,
         "error-message": str(error),
         "stack-trace": traceback.format_exc(),
     })
     return _worker.Response(response=body,
                             status=status_code,
                             mimetype=_content_types.JSON)
Example #4
0
def default_output_fn(prediction, accept):
    """Function responsible to serialize the prediction for the response.

    Args:
        prediction (obj): prediction returned by predict_fn .
        accept (str): accept content-type expected by the client.

    Returns:
        (worker.Response): a Flask response object with the following args:

            * Args:
                response: the serialized data to return
                accept: the content-type that the data was transformed to.
    """
    return _worker.Response(_encoders.encode(prediction, accept), accept)
Example #5
0
    def transform(self):  # type: () -> _worker.Response
        """Take a request with input data, deserialize it, make a prediction, and return a
        serialized response.

        Returns:
            sagemaker_containers.beta.framework.worker.Response: a Flask response object with
                the following args:

                * response: the serialized data to return
                * accept: the content type that the data was serialized into
        """
        request = _worker.Request()
        result = self._transform_fn(self._model, request.content,
                                    request.content_type, request.accept)

        if isinstance(result, tuple):
            # transforms tuple in Response for backwards compatibility
            return _worker.Response(response=result[0], accept=result[1])

        return result
Example #6
0
    def transform(self):  # type: () -> _worker.Response
        """Responsible to make predictions against the model.

        Returns:
            (worker.Response): a Flask response object with the following args:

                * Args:
                    response: the serialized data to return
                    accept: the content-type that the data was transformed to.
        """
        request = _worker.Request()

        data = self._input_fn(request.content, request.content_type)
        prediction = self._predict_fn(data, self._model)
        result = self._output_fn(prediction, request.accept)

        if isinstance(result, tuple):
            # transforms tuple in Response for backwards compatibility
            return _worker.Response(response=result[0], accept=result[1])

        return result
 def transform(self):
     self.calls['transform'] += 1
     return _worker.Response(json.dumps(self.calls), _content_types.JSON)
 def transform_fn():
     return _worker.Response(response='fake data',
                             accept=_content_types.JSON)
 def transform_fn():
     return _worker.Response(response="fake data",
                             accept="deprecated accept arg")
 def transform_fn():
     return _worker.Response(response="fake data", mimetype=content_type)
Example #11
0
 def transform(self):
     self.calls["transform"] += 1
     return _worker.Response(response=json.dumps(self.calls), mimetype=_content_types.JSON)
 def transform_fn():
     return _worker.Response(response='fake data',
                             accept='deprecated accept arg')