import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle

app = Flask(__name__)
model = pickle.load(open('model.pkl', 'rb'))

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/EDA Report1/')
def EDA1():
    return render_template('ReportEDA.html')

@app.route('/EDA Report 2/')
def EDA2():
    return render_template('Report EDA MSME data clean_kNN_Impute_data.html')

@app.route('/BarPlot/')
def EDA3():
    return render_template('barplotEDA2.html')

@app.route('/BoxPlot/')
def EDA4():
    return render_template('boxplotEDA2.html')


@app.route('/predict',methods=['POST'])
def predict():
    '''
    For rendering results on HTML GUI
    '''
    int_features = [int(x) for x in request.form.values()]
    final_features = [np.array(int_features)]
    prediction = model.predict(final_features)
    
    if prediction == 0:
        output = 'Not  defaulting'
    else:
        output = 'Defaulting'

    #output = round(prediction[0], 2)

    return render_template('index.html', prediction_text='Loan has a chance of {}'.format(output))

@app.route('/predict_api',methods=['POST'])
def predict_api():
    '''
    For direct API calls trought request
    '''
    data = request.get_json(force=True)
    prediction = model.predict([np.array(list(data.values()))])

    output = prediction[0]
    return jsonify(output)

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