ImranK/DeepLearningCreditApp
0
1 2import pandas as pd3import gradio as gr4import tensorflow5import tensorflow as tf6 7# define the function to make predictions8def predict_loan_approval(loan_amount, mortdue, prop_value, reason, job, yoj, derog, delinq, clage, ninq, clno, debtinc):9 # load the input data into a pandas dataframe10 input_df = pd.DataFrame({11 "LOAN": [loan_amount],12 "MORTDUE": [mortdue],13 "VALUE": [prop_value],14 "REASON_DebtCon": [int("DebtCon" in reason)],15 "REASON_HomeImp": [int("HomeImp" in reason)],16 "REASON_Other" :[int("Other" in reason)],17 "JOB_Mgr": [int("Mgr" in job)],18 "JOB_Office": [int("Office" in job)],19 "JOB_ProfExe": [int("ProfExe" in job)],20 "JOB_Sales": [int("Sales" in job)],21 "JOB_Self": [int("Self" in job)],22 "JOB_Other": [int("Other" in job)],23 "YOJ": [yoj],24 "DEROG": [derog],25 "DELINQ": [delinq],26 "CLAGE": [clage],27 "NINQ": [ninq],28 "CLNO": [clno],29 "DEBTINC": [debtinc]30 })31 # add dummy variables for reason and job32 if "DebtCon" in reason:33 input_df["REASON_DebtCon"] = 134 else:35 input_df["REASON_DebtCon"] = 036 if "HomeImp" in reason:37 input_df["REASON_HomeImp"] = 138 else:39 input_df["REASON_HomeImp"] = 040 41 if "Mgr" in job:42 input_df["JOB_Mgr"] = 143 else:44 input_df["JOB_Mgr"] = 045 if "Office" in job:46 input_df["JOB_Office"] = 147 else:48 input_df["JOB_Office"] = 049 if "Other" in job:50 input_df["JOB_Other"] = 151 else:52 input_df["JOB_Other"] = 053 if "ProfExe" in job:54 input_df["JOB_ProfExe"] = 155 else:56 input_df["JOB_ProfExe"] = 057 if "Sales" in job:58 input_df["JOB_Sales"] = 159 else:60 input_df["JOB_Sales"] = 061 if "Self" in job:62 input_df["JOB_Self"] = 163 else:64 input_df["JOB_Self"] = 065 66 # load the pretrained model67 model = tf.keras.models.load_model("credit_prediction_model.h5")68 69 # make a prediction70 prediction = model.predict(input_df)71 pred = prediction[0][0]72 # return the prediction as "Approved" or "Not Approved"73 if pred > 0.3:74 return "Risk score: "+str(round(pred))+" :Decision: "+"Not Approved"75 else:76 return str(round(pred,4))+":"+"Approved" 77 78 79# define the Gradio interface80inputs = [81 gr.inputs.Number(label="Loan Amount (000)"),82 gr.inputs.Number(label="Amount due on Existing Mortgage"),83 gr.inputs.Number(label="Value of Current Property"),84 gr.inputs.CheckboxGroup(85 label="Reason",86 choices=["DebtCon", "HomeImp"]87 ),88 gr.inputs.CheckboxGroup(89 label="Job",90 choices=['Office', 'Sales', 'Mgr', 'ProfExe', 'Self','Other']91 ),92 gr.inputs.Number(label="Years at Present Job"),93 gr.inputs.Number(label="Number of Major Derogatory Reports"),94 gr.inputs.Number(label="Number of Delinquent Credit Lines"),95 gr.inputs.Number(label="Age of Oldest Credit Line in Months"),96 gr.inputs.Number(label="Number of Recent Credit Inquiries"),97 gr.inputs.Number(label="Number of Credit Lines"),98 gr.inputs.Number(label="Debt-to-Income Ratio")99]100 101outputs = gr.outputs.Textbox(label="Loan Approval")102 103iface = gr.Interface(104 fn=predict_loan_approval,105 inputs=inputs,106 outputs=outputs,107 title="Loan Approval Prediction",108 description="Enter the details of the loan application to check for eligibility",109 theme="dark" # default theme110)111 112#iface.launch(share=True)113#iface.theme_toggle()