import json
import logging

import joblib
import pandas as pd
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

@app.route("/api/machinePrediction", methods=['GET'])
def home():
    incomingMachineId = request.args.get('machineId')
    modelPath = request.args.get('modelPath')
    column_names = request.args.get('columnNames')
    data_points = request.args.get('dataPoints')

    app.logger.info('Received machine id is %s', incomingMachineId)
    app.logger.info('Model path is %s', modelPath)

    json_object = json.loads(data_points)
    pairs = json_object.items()

    vitals_value = []
    for key, value in pairs:
      vitals_value.append(value)

    modelObj = joblib.load(modelPath)

    data = [vitals_value]
    df = pd.DataFrame(data=data, columns = column_names)

    modelPrediction = modelObj.predict(df)
    app.logger.info('Model prediction is: %s', modelPrediction)

    return jsonify(modelPrediction[0])

if __name__ == "__main__":
    app.run(debug=True)


# To start the server
# python3 app.py

[((153, 168), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (158, 168), False, 'from flask import Flask, jsonify, request\n'), ((169, 178), 'flask_cors.CORS', 'CORS', (['app'], {}), '(app)\n', (173, 178), False, 'from flask_cors import CORS, cross_origin\n'), ((270, 299), 'flask.request.args.get', 'request.args.get', (['"""machineId"""'], {}), "('machineId')\n", (286, 299), False, 'from flask import Flask, jsonify, request\n'), ((316, 345), 'flask.request.args.get', 'request.args.get', (['"""modelPath"""'], {}), "('modelPath')\n", (332, 345), False, 'from flask import Flask, jsonify, request\n'), ((365, 396), 'flask.request.args.get', 'request.args.get', (['"""columnNames"""'], {}), "('columnNames')\n", (381, 396), False, 'from flask import Flask, jsonify, request\n'), ((415, 445), 'flask.request.args.get', 'request.args.get', (['"""dataPoints"""'], {}), "('dataPoints')\n", (431, 445), False, 'from flask import Flask, jsonify, request\n'), ((585, 608), 'json.loads', 'json.loads', (['data_points'], {}), '(data_points)\n', (595, 608), False, 'import json\n'), ((742, 764), 'joblib.load', 'joblib.load', (['modelPath'], {}), '(modelPath)\n', (753, 764), False, 'import joblib\n'), ((801, 846), 'pandas.DataFrame', 'pd.DataFrame', ([], {'data': 'data', 'columns': 'column_names'}), '(data=data, columns=column_names)\n', (813, 846), True, 'import pandas as pd\n'), ((969, 996), 'flask.jsonify', 'jsonify', (['modelPrediction[0]'], {}), '(modelPrediction[0])\n', (976, 996), False, 'from flask import Flask, jsonify, request\n')]