Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# libraries for data preprocessing
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import pickle
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
|
9 |
+
## lets load the model
|
10 |
+
with open('loan-model.bin', 'rb') as f_in:
|
11 |
+
dv, model = pickle.load(f_in)
|
12 |
+
|
13 |
+
|
14 |
+
# Example function to preprocess the input
|
15 |
+
def applicant_data(gender, married, dependents, education, self_employed, applicantincome, coapplicantincome, loanamount, loan_amount_term, credit_history, property_area):
|
16 |
+
# Create a dictionary of the input features
|
17 |
+
input_data = {
|
18 |
+
'Gender': gender,
|
19 |
+
'Married': married,
|
20 |
+
'Dependents': dependents,
|
21 |
+
'Education': education,
|
22 |
+
'Self_Employed': self_employed,
|
23 |
+
'ApplicantIncome': applicantincome,
|
24 |
+
'CoapplicantIncome': coapplicantincome,
|
25 |
+
'LoanAmount': loanamount,
|
26 |
+
'Loan_Amount_Term': loan_amount_term,
|
27 |
+
'Credit_History': credit_history,
|
28 |
+
'Property_Area': property_area
|
29 |
+
}
|
30 |
+
|
31 |
+
# Transform the input data using the pre-fitted transformer (e.g., DictVectorizer, OneHotEncoder)
|
32 |
+
X = dv.transform([input_data])
|
33 |
+
|
34 |
+
return X
|
35 |
+
|
36 |
+
# Function to make predictions
|
37 |
+
def predict_loan_status(gender, married, dependents, education, self_employed, applicantincome, coapplicantincome, loanamount, loan_amount_term, credit_history, property_area):
|
38 |
+
# Preprocess the input data
|
39 |
+
input_data = applicant_data(gender, married, dependents, education, self_employed, applicantincome, coapplicantincome, loanamount, loan_amount_term, credit_history, property_area)
|
40 |
+
|
41 |
+
# Predict using the model
|
42 |
+
prediction = model.predict_proba(input_data)[:,1]
|
43 |
+
|
44 |
+
|
45 |
+
# Post-process the prediction if needed
|
46 |
+
# Assuming the model output is a single prediction value
|
47 |
+
predicted_value = prediction[0]
|
48 |
+
|
49 |
+
print(predicted_value)
|
50 |
+
|
51 |
+
# Return verdict based on threshold
|
52 |
+
if predicted_value >= 0.3:
|
53 |
+
return f'Verdict: Good standing - Approved ({predicted_value:.2f})'
|
54 |
+
else:
|
55 |
+
return f'Verdict: Bad standing - Rejected ({predicted_value:.2f})'
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
# Gradio interface
|
60 |
+
# Gradio interface with improved input handling
|
61 |
+
inputs = [
|
62 |
+
gr.Dropdown(choices=["Male", "Female"], label="Gender"),
|
63 |
+
gr.Dropdown(choices=["Yes", "No"], label="Married"),
|
64 |
+
gr.Number(label="Dependents", value=0),
|
65 |
+
gr.Dropdown(choices=["Graduate", "Not Graduate"], label="Education"),
|
66 |
+
gr.Dropdown(choices=["Yes", "No"], label="Self Employed"),
|
67 |
+
gr.Number(label="Applicant Income", value=0),
|
68 |
+
gr.Number(label="Coapplicant Income", value=0),
|
69 |
+
gr.Number(label="Loan Amount", value=0),
|
70 |
+
gr.Number(label="Loan Amount Term", value=360),
|
71 |
+
gr.Dropdown(choices=[0, 1], label="Credit History"),
|
72 |
+
gr.Dropdown(choices=["Urban", "Semiurban", "Rural"], label="Property Area")
|
73 |
+
]
|
74 |
+
|
75 |
+
|
76 |
+
output_text = gr.Textbox(label="Loan Prediction")
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
# Create the Gradio interface
|
81 |
+
gr.Interface(fn=predict_loan_status, inputs=inputs, outputs=output_text, title="Loan Status Prediction").launch(share=True)
|