Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import joblib
|
3 |
+
import pandas as pd
|
4 |
+
from streamlit_option_menu import option_menu
|
5 |
+
from reportlab.pdfgen import canvas
|
6 |
+
from reportlab.lib.pagesizes import letter
|
7 |
+
from reportlab.lib import colors
|
8 |
+
from io import BytesIO
|
9 |
+
import base64
|
10 |
+
# Load the trained model
|
11 |
+
model = joblib.load('loan_approval_model.pkl')
|
12 |
+
|
13 |
+
# Define the function to make predictions
|
14 |
+
def predict_loan_approval(features):
|
15 |
+
prediction = model.predict([features])
|
16 |
+
return prediction[0]
|
17 |
+
|
18 |
+
# Streamlit app
|
19 |
+
st.title("Loan Approval Prediction System")
|
20 |
+
|
21 |
+
# Input fields for user data
|
22 |
+
st.sidebar.header("Menu")
|
23 |
+
if __name__ == '__main__':
|
24 |
+
st.markdown("## Loan Approval Prediction System")
|
25 |
+
with st.sidebar:
|
26 |
+
selected = option_menu('Loan Approval Prediction System',
|
27 |
+
['Predict Loan Approval',
|
28 |
+
'Our Prediction Records',
|
29 |
+
'About Me'],
|
30 |
+
icons=['info','book','info'],
|
31 |
+
default_index=0)
|
32 |
+
|
33 |
+
if selected =="Predict Loan Approval":
|
34 |
+
# Example input fields
|
35 |
+
Name = st.text_input('Enter your name:')
|
36 |
+
gender = st.selectbox("Gender", options=[0, 1], format_func=lambda x: 'Male' if x == 1 else 'Female')
|
37 |
+
married = st.selectbox("Married", options=[0, 1], format_func=lambda x: 'Yes' if x == 1 else 'No')
|
38 |
+
education = st.selectbox("Education", options=[0, 1], format_func=lambda x: 'Graduate' if x == 1 else 'Not Graduate')
|
39 |
+
self_employed = st.selectbox("Self Employed", options=[0, 1], format_func=lambda x: 'Yes' if x == 1 else 'No')
|
40 |
+
applicant_income = st.number_input("Applicant Income", min_value=0, value=50000)
|
41 |
+
coapplicant_income = st.number_input("Coapplicant Income", min_value=0.0, value=0.0)
|
42 |
+
loan_amount = st.number_input("Loan Amount", min_value=0.0, value=10000.0)
|
43 |
+
loan_amount_term = st.number_input("Loan Amount Term", min_value=0.0, value=360.0)
|
44 |
+
credit_history = st.selectbox("Credit History", options=[0.0, 1.0], format_func=lambda x: 'No' if x == 0.0 else 'Yes')
|
45 |
+
property_area = st.selectbox("Property Area", options=[0, 1, 2], format_func=lambda x: ['Urban', 'Semiurban', 'Rural'][x])
|
46 |
+
dependents = st.number_input("Dependents", min_value=0, value=0)
|
47 |
+
|
48 |
+
# Collect inputs into a list
|
49 |
+
user_input = [gender, married, education, self_employed, applicant_income, coapplicant_income,
|
50 |
+
loan_amount, loan_amount_term, credit_history, property_area, dependents]
|
51 |
+
|
52 |
+
# Predict
|
53 |
+
if st.button("Predict"):
|
54 |
+
result = predict_loan_approval(user_input)
|
55 |
+
if result == 1:
|
56 |
+
st.success("Loan Approved")
|
57 |
+
else:
|
58 |
+
st.error("Loan Denied")
|
59 |
+
f = open("user_records.txt", "a")
|
60 |
+
f.write("\n")
|
61 |
+
new_data = str([Name, gender, married, education, self_employed, applicant_income, coapplicant_income,
|
62 |
+
loan_amount, loan_amount_term, credit_history, property_area, dependents, result])
|
63 |
+
leng = len(new_data)
|
64 |
+
f.write(new_data[1:leng-1])
|
65 |
+
f.close()
|
66 |
+
|
67 |
+
def generate_report(Name, result):
|
68 |
+
buffer = BytesIO()
|
69 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
70 |
+
width, height = letter
|
71 |
+
c.drawString(100, height-50, "Loan Prediction Report")
|
72 |
+
c.drawString(100, height-70, "--------------------------------------------")
|
73 |
+
c.drawString(100, height-90, f"Name: {Name}")
|
74 |
+
c.drawString(100, height-110, f"Gender: {gender}")
|
75 |
+
c.drawString(100, height-130, f"Married?: {married}")
|
76 |
+
c.drawString(100, height-150, f"education Status: {education}")
|
77 |
+
c.drawString(100, height-170, f"Self Employed?): {self_employed}")
|
78 |
+
c.drawString(100, height-190, f"Income: {applicant_income}")
|
79 |
+
c.drawString(100, height-210, f"Gurrantor Income: {coapplicant_income}")
|
80 |
+
c.drawString(100, height-230, f"Loan Amount: {loan_amount}")
|
81 |
+
c.drawString(100, height-250, f"Credit History: {credit_history}")
|
82 |
+
c.drawString(100, height-270, f"Property: {property_area}")
|
83 |
+
c.drawString(100, height-290, f"Dependents: {dependents}")
|
84 |
+
c.drawString(100, height-310, "--------------------------------------------")
|
85 |
+
c.drawString(100, height-330, "Prediction:")
|
86 |
+
if result == 1:
|
87 |
+
c.setFillColorRGB(0, 1, 0) # Red color
|
88 |
+
prediction_text = "Loan Approved"
|
89 |
+
else:
|
90 |
+
c.setFillColorRGB(1, 0, 0) # Green color
|
91 |
+
prediction_text = "Loan Not Approved"
|
92 |
+
c.drawString(100, height-345, f"{prediction_text}")
|
93 |
+
c.setFillColor(colors.black)
|
94 |
+
c.setFont("Helvetica", 10)
|
95 |
+
footnote = "Note: The prediction is based on probability. Actual results may vary. Please consult an expert for a detailed check."
|
96 |
+
c.drawString(100, 200, footnote)
|
97 |
+
c.showPage()
|
98 |
+
c.save()
|
99 |
+
pdf_bytes = buffer.getvalue()
|
100 |
+
buffer.close()
|
101 |
+
return pdf_bytes
|
102 |
+
|
103 |
+
pdf_bytes = generate_report(Name, result)
|
104 |
+
pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
|
105 |
+
pdf_display = f'<a href="data:application/pdf;base64,{pdf_base64}" download="Loan_Approval_report_{Name}.pdf">Download Report</a>'
|
106 |
+
st.markdown(pdf_display, unsafe_allow_html=True)
|
107 |
+
|
108 |
+
if selected == "Our Prediction Records":
|
109 |
+
st.markdown("<h3 style='text-align: center;'>PREDICTION RECORDS OF OUR PREVIOUS USERS</h1>", unsafe_allow_html=True)
|
110 |
+
f = pd.read_csv("user_records.txt")
|
111 |
+
#st.table(f)
|
112 |
+
st.table(f.style.set_table_attributes('style="width:100%;"'))
|
113 |
+
st.markdown("____")
|
114 |
+
st.write("All the records are stored only for academic and research purpose & will not be used for any other means.")
|
115 |
+
|
116 |
+
if selected == "About Me":
|
117 |
+
st.markdown("<h2 style='text-align: center;'>ABOUT</h2>", unsafe_allow_html=True)
|
118 |
+
st.markdown("____")
|
119 |
+
st.markdown("<p style='text-align: center;'>This is an academic project made by B.Tech Computer Science And Engineering 3rd year student.</p>", unsafe_allow_html=True)
|
120 |
+
st.markdown("____")
|
121 |
+
st.markdown("<h4 style='text-align: center;'>Developed and maintained by</h4>", unsafe_allow_html=True)
|
122 |
+
st.markdown("<p style='text-align: center;'>Subrata Bhuin</p>", unsafe_allow_html=True)
|
123 |
+
st.markdown("<p style='text-align: center;'>[email protected]</p>", unsafe_allow_html=True)
|
124 |
+
st.markdown("____")
|