-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (56 loc) · 2.13 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, render_template, url_for, flash, request, jsonify
from markupsafe import Markup
from forms import PredictionForm
from pandas import DataFrame
import joblib
app = Flask(__name__)
# Secret key protect against modifying cookies and cross-site request forgery attacks
app.config['SECRET_KEY'] = '686ae18682ce71b6e620dfea8995d735'
# Load trained model
model = joblib.load("models/loan_model_logreg.pkl")
# Home page
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html')
# Prediction page
@app.route('/prediction', methods=['GET', 'POST'])
def prediction():
form = PredictionForm()
field_list = ["SelectField", "DecimalField", "IntegerField"]
if form.validate_on_submit():
# convert input data as dataframe and run through pipeline
new_data = dict([(field.id, field.data)
for field in form
if field.type in field_list])
new_df = DataFrame([new_data])
new_df['Credit_History'] = new_df['Credit_History'].astype('float')
# predict
pred_prob = model.predict_proba(new_df)[0, 1]
if pred_prob > 0.5:
conclusion = 'APPROVED :)'
category = 'success'
else:
conclusion = 'REJECTED :('
category = 'danger'
message = Markup(
f'''
<b>Probability of loan approval</b>: {pred_prob*100:.2f}% <br>
<b>Conclusion</b>: Loan is {conclusion}
'''
)
flash(message, category=category)
# return redirect(url_for('home'))
return render_template('prediction.html', title='Predict', form=form, field_list=field_list)
# API
@app.route('/result', methods=['POST'])
def result():
new_data = request.get_json(force=True)
new_df = DataFrame([new_data])
new_df['Credit_History'] = new_df['Credit_History'].astype('float')
# predict
pred_prob = model.predict_proba(new_df)[0, 1]
pred_class = 'approved' if pred_prob > 0.5 else 'rejected'
return jsonify((pred_prob, pred_class))
if __name__ == '__main__':
app.run(debug=True, port=5000)