Example #1
0
    def TransformOutput():
        request = extract_message()
        logger.debug("Request: %s", request)

        sanity_check_request(request)

        if hasattr(user_model, "transform_output_rest"):
            return jsonify(user_model.transform_output_rest(request))
        else:
            features = get_data_from_json(request)
            names = request.get("data", {}).get("names")
            meta = get_meta_from_json(request)

            transformed = transform_output(user_model,
                                           features,
                                           names,
                                           meta=meta)
            logger.debug("Transformed: %s", transformed)

            if isinstance(transformed, np.ndarray) or "data" in request:
                new_class_names = get_class_names(user_model, names)
                data = array_to_rest_datadef(transformed, new_class_names,
                                             request.get("data", {}))
                response = {"data": data, "meta": {}}
            else:
                response = {"binData": transformed, "meta": {}}

            tags = get_custom_tags(user_model)
            if tags:
                response["meta"]["tags"] = tags
            metrics = get_custom_metrics(user_model)
            if metrics:
                response["meta"]["metrics"] = metrics
            return jsonify(response)
Example #2
0
    def TransformInput():
        request = extract_message()
        logger.debug("Request: %s", request)

        sanity_check_request(request)

        if hasattr(user_model, "transform_input_rest"):
            return jsonify(user_model.transform_input_rest(request))
        else:
            features = get_data_from_json(request)
            names = request.get("data", {}).get("names")

            transformed = transform_input(user_model, features, names)
            logger.debug("Transformed: %s", transformed)

            # If predictions is an numpy array or we used the default data then return as numpy array
            if isinstance(transformed, np.ndarray) or "data" in request:
                new_feature_names = get_feature_names(user_model, names)
                transformed = np.array(transformed)
                data = array_to_rest_datadef(transformed, new_feature_names,
                                             request.get("data", {}))
                response = {"data": data, "meta": {}}
            else:
                response = {"binData": transformed, "meta": {}}

            tags = get_custom_tags(user_model)
            if tags:
                response["meta"]["tags"] = tags
            metrics = get_custom_metrics(user_model)
            if metrics:
                response["meta"]["metrics"] = metrics
            return jsonify(response)
Example #3
0
    def Route():

        request = extract_message()
        logger.debug("Request: %s", request)

        sanity_check_request(request)

        if hasattr(user_router, "route_rest"):
            return jsonify(user_router.route_rest(request))
        else:
            datadef = request.get("data")
            features = rest_datadef_to_array(datadef)

            routing = np.array(
                [[route(user_router, features, datadef.get("names"))]])
            # TODO: check that predictions is 2 dimensional
            class_names = []

            data = array_to_rest_datadef(routing, class_names, datadef)

            response = {"data": data, "meta": {}}
            tags = get_custom_tags(user_router)
            if tags:
                response["meta"]["tags"] = tags
            metrics = get_custom_metrics(user_router)
            if metrics:
                response["meta"]["metrics"] = metrics
            return jsonify(response)
Example #4
0
    def TransformInput():
        request = extract_message()
        sanity_check_request(request)

        datadef = request.get("data")
        features = rest_datadef_to_array(datadef)

        outlier_scores = score(user_model, features, datadef.get("names"))
        # TODO: check that predictions is 2 dimensional

        request["meta"].setdefault("tags", {})
        request["meta"]["tags"]["outlierScore"] = list(outlier_scores)

        return jsonify(request)
Example #5
0
def sanity_check_seldon_message_list(request):
    if not "seldonMessages" in request:
        raise SeldonMicroserviceException(
            "Request must contain seldonMessages field")
    msgs = request["seldonMessages"]
    if not type(msgs) in [list, tuple]:
        raise SeldonMicroserviceException("seldonMessages field is not a list")
    if len(msgs) == 0:
        raise SeldonMicroserviceException("seldonMessages field is empty")
    for idx, msg in enumerate(msgs):
        try:
            sanity_check_request(msg)
        except SeldonMicroserviceException as err:
            raise SeldonMicroserviceException(
                "Invalid SeldonMessage at index {0} : {1}".format(
                    idx, err.message))
    def Predict():
        request = extract_message()
        logger.debug("Request: %s", request)

        sanity_check_request(request)

        if hasattr(user_model, "predict_rest"):
            return jsonify(user_model.predict_rest(request))
        else:
            features = get_data_from_json(request)
            names = request.get("data", {}).get("names")
            meta = get_meta_from_json(request)

            predictions = predict(user_model, features, names, meta=meta)
            logger.debug("Predictions: %s", predictions)

            # If predictions is an numpy array or we used the default data then return as numpy array
            if isinstance(predictions, np.ndarray) or "data" in request:
                predictions = np.array(predictions)
                if len(predictions.shape) > 1:
                    class_names = get_class_names(user_model,
                                                  predictions.shape[1])
                else:
                    class_names = []
                data = array_to_rest_datadef(predictions, class_names,
                                             request.get("data", {}))
                response = {"data": data, "meta": {}}
            else:
                response = {"binData": predictions, "meta": {}}

            tags = get_custom_tags(user_model)
            if tags:
                response["meta"]["tags"] = tags
            metrics = get_custom_metrics(user_model)
            if metrics:
                response["meta"]["metrics"] = metrics
            return jsonify(response)