def predict():
    if request.method == 'POST':
        # step:1 Extract Post data from request body as JSON
        json_data = request.get_json()
        print(f'User input from UI: {json_data}')
        _logger.info(f'Inputs: {json_data}')

        # # step:2 Validate the input using marshmallow schema
        #input_data,errors = validate_inputs(input_data=json_data)

        # step 3: model prediction
        result = make_prediction(input_data=json_data)
        _logger.info(f'Outputs: {result}')

        # step 4: Convert numpy ndarray to list
        predictions = round(result.get('prediction')[0],2)
        #print(f'prediction from model ==== {predictions}')
        #version = result.get('version')

        return jsonify({'prediction': predictions}), 200
Esempio n. 2
0
def predict():
    if request.method = 'POST':
        # Step 1: Extract POST data from request body as jason
        json_data = request.get_json()
        _logger.debug(f'Inputs: {json_data}')

        # Step 2: Validate the input using marshmallow schema
        input_data, errors = validate_inputs(input_data=json_data)

        # Step 3: Model prediction_app
        result = make_prediction(input_data=input_data)
        _logger.debug(f'Outputs: {result}')

        # Step 4: Convert numpy array to list
        predictions = result.get('prediction').tolist()
        version = result.get('version')

        # Step 5: Return the response as JSON
        return jsonify({'predictions': predictions,
                        'version': version,
                        'errors': errors})
Esempio n. 3
0
def predict():
    if request.method == "POST":
        # Step 1: Extract POST data from request body as JSON
        json_data = request.get_json()
        _logger.debug(f"Inputs: {json_data}")

        # Step 2: Validate the input using marshmallow schema
        input_data, errors = validate_inputs(input_data=json_data)

        # Step 3: Model prediction
        result = make_prediction(input_data=input_data)
        _logger.debug(f"Outputs: {result}")

        # Step 4: Convert numpy ndarray to list
        predictions = result.get("predictions").tolist()
        version = result.get("version")

        # Step 5: Return the response as JSON
        return jsonify(
            {"predictions": predictions, "version": version, "errors": errors}
        )
Esempio n. 4
0
def test_model_prediction_differential(
        *,
        save_file: str = 'test_data_predictions.csv'):
    """
    This test compares the prediction result similarity of
    the current model with the previous model's results.
    """

    # Given
    # Load the saved previous model predictions
    previous_model_df = pd.read_csv(f'{config.PACKAGE_ROOT}/{save_file}')
    previous_model_predictions = previous_model_df.predictions.values

    test_data = load_dataset(file_name=model_config.TESTING_DATA_FILE)
    multiple_test_input = test_data[99:600]

    # When
    current_result = make_prediction(input_data=multiple_test_input)
    current_model_predictions = current_result.get('predictions')

    # Then
    # diff the current model vs. the old model
    assert len(previous_model_predictions) == len(
        current_model_predictions)

    # Perform the differential test
    for previous_value, current_value in zip(
            previous_model_predictions, current_model_predictions):

        # convert numpy float64 to Python float.
        previous_value = previous_value.item()
        current_value = current_value.item()

        # rel_tol is the relative tolerance – it is the maximum allowed
        # difference between a and b, relative to the larger absolute
        # value of a or b. For example, to set a tolerance of 5%, pass
        # rel_tol=0.05.
        assert math.isclose(previous_value,
                            current_value,
                            rel_tol=model_config.ACCEPTABLE_MODEL_DIFFERENCE)
def test_make_predictions():
    data = load_dataset(file_name=config.DATASET_FILE)

    X_train, X_test, y_train, y_test = train_test_split(
        data.drop(config.TARGET, axis=1),
        data[config.TARGET],
        test_size=0.2,
        random_state=0)  # must be 0

    test_data = X_test
    original_data_length = len(test_data)
    pred = make_prediction(input_data=test_data)

    # Check predictions have been made
    assert pred is not None

    # Check accuracy
    assert accuracy_score(y_test, pred['predictions'].round()) < 1

    # Check number of predictions is the same as length of test data
    # Change to != when more validation methods are added as some rows
    # may get removed.
    assert len(pred.get('predictions')) == original_data_length
Esempio n. 6
0
def predict():
    if request.method == 'POST':
        # Step 1: リクエスト本文からJSONとしてPOSTデータを抽出
        json_data = request.get_json()
        _logger.debug(f'Inputs: {json_data}')

        # Step 2: marshmallowスキーマを使用して入力を検証
        input_data, errors = validate_inputs(input_data=json_data)

        # Step 3: モデル予測
        result = make_prediction(input_data=input_data)
        _logger.debug(f'Outputs: {result}')

        # Step 4: numpy ndarrayをリストに変換
        predictions = result.get('predictions').tolist()
        version = result.get('version')

        # Step 5: 応答をJSONとして返す
        return jsonify({
            'predictions': predictions,
            'version': version,
            'errors': errors
        })
Esempio n. 7
0
def test_get_multiple_predictions_with_forecast_data(forecast_input_data):
    subject = make_prediction(input_data=forecast_input_data)

    assert subject is not None
    assert len(subject.get('predictions')) == len(forecast_input_data)
Esempio n. 8
0
def test_get_single_prediction_with_forecast_data(forecast_input_data):
    single_test_json = forecast_input_data[0:1]
    subject = make_prediction(input_data=single_test_json)

    assert subject is not None
    assert isinstance(subject.get('predictions')[0], float)
@prediction_app.route('/v1/predict/regression', methods=['POST'])
def predict():
    if request.method == 'POST':
        # Step 1: Extract POST data from request body as JSON
        json_data = request.get_json()
        _logger.debug(f'Inputs: {json_data}')

        # Step 2: Validate the input using marshmallow schema
<<<<<<< HEAD
        input_data, errors = validate_inputs(input_data=json_data)
=======
        input_data, errors = validate_inputs(input_json=json_data)
>>>>>>> 6162c318b58b225e0061fccd6c64cd67fe205c1b

        # Step 3: Model prediction
        result = make_prediction(input_data=input_data)
        _logger.debug(f'Outputs: {result}')

        # Step 4: Convert numpy ndarray to list
        predictions = result.get('predictions').tolist()
        version = result.get('version')

        # Step 5: Return the response as JSON
        return jsonify({'predictions': predictions,
                        'version': version,
                        'errors': errors})


@prediction_app.route('/predict/classifier', methods=['POST'])
def predict_image():
    if request.method == 'POST':