neuraxle.rest.flask

Neuraxle’s Flask Wrapper classes

The flask wrapper classes are used to easily serve pipeline predictions using a flask rest api.

Classes

FlaskRestApiWrapper(json_decoder, wrapped, …)

Wrap a pipeline to easily deploy it to a REST API.

JSONDataBodyDecoder(hyperparams, …)

Class to be used within a FlaskRESTApiWrapper to convert input json to actual data (e.g.: arrays)

JSONDataResponseEncoder(hyperparams, …)

Base class to be used within a FlaskRESTApiWrapper to convert prediction output to json response.

class neuraxle.rest.flask.FlaskRestApiWrapper(json_decoder: neuraxle.rest.flask.JSONDataBodyDecoder, wrapped: neuraxle.base.BaseStep, json_encoder: neuraxle.rest.flask.JSONDataResponseEncoder, route='/')[source]

Wrap a pipeline to easily deploy it to a REST API. Just provide a json encoder and a json decoder.

Usage example:

``` class CustomJSONDecoderFor2DArray(JSONDataBodyDecoder):

‘’’This is a custom JSON decoder class that precedes the pipeline’s transformation.’’’

def decode(self, data_inputs: dict):

values_in_json_2d_arr: List[List[int]] = data_inputs[“values”] return np.array(values_in_json_2d_arr)

class CustomJSONEncoderOfOutputs(JSONDataResponseEncoder):

‘’’This is a custom JSON response encoder class for converting the pipeline’s transformation outputs.’’’

def encode(self, data_inputs) -> dict:
return {

‘predictions’: list(data_inputs)

}

app = FlaskRestApiWrapper(

json_decoder=CustomJSONDecoderFor2DArray(), wrapped=Pipeline(…), json_encoder=CustomJSONEncoderOfOutputs(),

).get_app()

app.run(debug=False, port=5000) ```

get_app()[source]

This methods returns a REST API wrapping the pipeline.

Returns

a Flask app (as given by app = Flask(__name__) and then configured).

class neuraxle.rest.flask.JSONDataBodyDecoder(hyperparams: neuraxle.hyperparams.space.HyperparameterSamples = None, hyperparams_space: neuraxle.hyperparams.space.HyperparameterSpace = None, name: str = None, savers: List[neuraxle.base.BaseSaver] = None, hashers: List[neuraxle.base.BaseHasher] = None)[source]

Class to be used within a FlaskRESTApiWrapper to convert input json to actual data (e.g.: arrays)

decode(data_inputs: dict)[source]

Will convert data_inputs to a dict or a compatible data structure for jsonification

Parameters

data_inputs (dict parsed from json) (encoded) –

Returns

data_inputs (as a data structure compatible with pipeline’s data inputs)

transform(data_inputs)[source]

Transform given data inputs.

Parameters

data_inputs – data inputs

Returns

transformed data inputs

class neuraxle.rest.flask.JSONDataResponseEncoder(hyperparams: neuraxle.hyperparams.space.HyperparameterSamples = None, hyperparams_space: neuraxle.hyperparams.space.HyperparameterSpace = None, name: str = None, savers: List[neuraxle.base.BaseSaver] = None, hashers: List[neuraxle.base.BaseHasher] = None)[source]

Base class to be used within a FlaskRESTApiWrapper to convert prediction output to json response.

encode(data_inputs) → dict[source]

Convert data_inputs to a dict or a compatible data structure for jsonification.

Parameters

(a data structure outputted by the pipeline after a transform) (data_inputs) –

Returns

encoded data_inputs (jsonifiable dict)

transform(data_inputs) → flask.wrappers.Response[source]

Transform processed data inputs into a flask response object.

Parameters

data_inputs

Returns

flask response object