File size: 2,930 Bytes
867642d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
# 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)