moro23's picture
Create app.py
867642d verified
# libraries for data preprocessing
import numpy as np
import pandas as pd
import pickle
import gradio as gr
## lets load the model
with open('loan-model.bin', 'rb') as f_in:
dv, model = pickle.load(f_in)
# Example function to preprocess the input
def applicant_data(gender, married, dependents, education, self_employed, applicantincome, coapplicantincome, loanamount, loan_amount_term, credit_history, property_area):
# Create a dictionary of the input features
input_data = {
'Gender': gender,
'Married': married,
'Dependents': dependents,
'Education': education,
'Self_Employed': self_employed,
'ApplicantIncome': applicantincome,
'CoapplicantIncome': coapplicantincome,
'LoanAmount': loanamount,
'Loan_Amount_Term': loan_amount_term,
'Credit_History': credit_history,
'Property_Area': property_area
}
# Transform the input data using the pre-fitted transformer (e.g., DictVectorizer, OneHotEncoder)
X = dv.transform([input_data])
return X
# Function to make predictions
def predict_loan_status(gender, married, dependents, education, self_employed, applicantincome, coapplicantincome, loanamount, loan_amount_term, credit_history, property_area):
# Preprocess the input data
input_data = applicant_data(gender, married, dependents, education, self_employed, applicantincome, coapplicantincome, loanamount, loan_amount_term, credit_history, property_area)
# Predict using the model
prediction = model.predict_proba(input_data)[:,1]
# Post-process the prediction if needed
# Assuming the model output is a single prediction value
predicted_value = prediction[0]
print(predicted_value)
# Return verdict based on threshold
if predicted_value >= 0.3:
return f'Verdict: Good standing - Approved ({predicted_value:.2f})'
else:
return f'Verdict: Bad standing - Rejected ({predicted_value:.2f})'
# Gradio interface
# Gradio interface with improved input handling
inputs = [
gr.Dropdown(choices=["Male", "Female"], label="Gender"),
gr.Dropdown(choices=["Yes", "No"], label="Married"),
gr.Number(label="Dependents", value=0),
gr.Dropdown(choices=["Graduate", "Not Graduate"], label="Education"),
gr.Dropdown(choices=["Yes", "No"], label="Self Employed"),
gr.Number(label="Applicant Income", value=0),
gr.Number(label="Coapplicant Income", value=0),
gr.Number(label="Loan Amount", value=0),
gr.Number(label="Loan Amount Term", value=360),
gr.Dropdown(choices=[0, 1], label="Credit History"),
gr.Dropdown(choices=["Urban", "Semiurban", "Rural"], label="Property Area")
]
output_text = gr.Textbox(label="Loan Prediction")
# Create the Gradio interface
gr.Interface(fn=predict_loan_status, inputs=inputs, outputs=output_text, title="Loan Status Prediction").launch(share=True)