Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (97 sloc) 4.54 KB
import numpy as np
import pandas as pd
import pickle
import re
from flask import Flask, request, Response, render_template, send_file, session
import PyPDF2
import time
import os
import tempfile
#instantiating app
app = Flask('ModelPredictor')
app.secret_key = 'x42nig!.5207'
#change route to default
@app.route('/')
def home():
return render_template('/home.html')
@app.route('/submit', methods=("POST", "GET"))
def submit():
user_input = request.args
data = [{
'age': int(user_input['age']),
'num_depend': int(user_input['num_depend']),
'monthly_inc': int(user_input['monthly_inc']),
'debt_ratio': float(user_input['debt_ratio']),
'past_due_30': int(user_input['past_due_30']),
'past_due_60': int(user_input['past_due_60']),
'past_due_90': int(user_input['past_due_90']),
'credit_bal': int(user_input['credit_bal']),
'num_credit_lines': int(user_input['num_credit_lines']),
'num_real_estate_lines': int(user_input['num_real_estate_lines'])
}]
model = pickle.load(open('./pickle/model_v2.pickle', 'rb'))
scenario = pd.DataFrame(data,index=[0])
scenario = scenario[['age', 'num_depend', 'monthly_inc', 'debt_ratio', 'past_due_30',
'past_due_60', 'past_due_90', 'credit_bal',
'num_credit_lines', 'num_real_estate_lines']]
features = scenario[['age', 'num_depend', 'monthly_inc', 'debt_ratio', 'past_due_30',
'past_due_60', 'past_due_90', 'credit_bal',
'num_credit_lines', 'num_real_estate_lines']]
scenario['delinq?'] = model.predict(features)
scenario['probability'] = model.predict_proba(features)[:,1]
with tempfile.NamedTemporaryFile(delete=True) as temp:
scenario.to_csv(temp.name + '.csv', index=False)
filename = temp.name + '.csv'
session['data'] = filename
return render_template('results.html', tables=[scenario.to_html()], titles=[scenario.columns.values])
@app.route('/upload', methods=['POST', 'GET'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
data_xls = pd.read_excel(f)
features = data_xls[['age', 'num_depend', 'monthly_inc', 'debt_ratio', 'past_due_30',
'past_due_60', 'past_due_90', 'credit_bal',
'num_credit_lines', 'num_real_estate_lines']]
model = pickle.load(open('./pickle/model_v2.pickle', 'rb'))
data_xls['delinq?'] = model.predict(features)
data_xls['probability'] = model.predict_proba(features)[:,1]
with tempfile.NamedTemporaryFile(delete=True) as temp:
data_xls.to_csv(temp.name + '.csv', index=False)
filename = temp.name + '.csv'
session['data'] = filename
return render_template('results.html', tables=[data_xls.to_html()], titles=[data_xls.columns.values])
@app.route('/pdf', methods=['POST', 'GET'])
def upload_pdf():
if request.method == 'POST':
f = request.files['file']
pdfReader = PyPDF2.PdfFileReader(f)
page1 = pdfReader.getPage(0).extractText().replace('\n',',').rstrip(',').split(",")
list_odd = []
list_even = []
for x in range(len(page1)):
if x % 2 !=0:
list_odd.append(page1[x])
if x % 2 == 0:
list_even.append(page1[x])
res = {list_even[i]: list_odd[i] for i in range(len(list_even))}
df = pd.DataFrame(data=res, index=[0])
features_pdf = df[['age', 'num_depend', 'monthly_inc', 'debt_ratio', 'past_due_30',
'past_due_60', 'past_due_90', 'credit_bal',
'num_credit_lines', 'num_real_estate_lines']]
model = pickle.load(open('./pickle/model_v2.pickle', 'rb'))
df['delinq?'] = model.predict(features_pdf)
df['probability'] = model.predict_proba(features_pdf)[:,1]
with tempfile.NamedTemporaryFile(delete=True) as temp:
df.to_csv(temp.name + '.csv', index=False)
filename = temp.name + '.csv'
session['data'] = filename
return render_template('results.html', tables=[df.to_html()], titles=[df.columns.values])
@app.route('/return-files')
def return_files():
scenario_df = pd.read_csv(session.get('data')).to_csv()
# temp.close()
return Response(scenario_df, mimetype="text/csv",
headers={"Content-disposition":"attachment; filename=output.csv"})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
# app.run(debug=True)