nunclud commited on
Commit
711541e
·
verified ·
1 Parent(s): a6e16cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -28
app.py CHANGED
@@ -1,44 +1,76 @@
1
  import gradio as gr
2
  import joblib
3
- import pandas as pd
4
 
 
 
5
 
6
- model = joblib.load('loan_RFmodel.joblib')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
 
 
8
 
9
- def predict_loan(gender, married, dependents, education, self_employed,
10
- income, co_income, amount, term, history, area):
11
-
12
-
13
- features = pd.DataFrame([[
14
- gender, married, dependents, education, self_employed,
15
- income, co_income, amount, term, history, area
16
- ]], columns=['Gender', 'Married', 'Dependents', 'Education', 'Self_Employed',
17
- 'ApplicantIncome', 'CoapplicantIncome', 'LoanAmount',
18
- 'Loan_Amount_Term', 'Credit_History', 'Property_Area'])
19
-
 
 
 
 
 
 
 
 
 
 
20
  prediction = model.predict(features)[0]
 
21
  return "Loan Approved" if prediction == 1 else "Loan Rejected"
22
 
23
- runapp = gr.Interface(
24
- fn=predict_loan,
 
 
25
  inputs=[
26
- gr.Dropdown(choices=[(1, "Male"), (0, "Female")], label="Gender"),
27
- gr.Dropdown(choices=[(1, "Yes"), (0, "No")], label="Married"),
28
- gr.Number(label="Dependents (0, 1, 2, or 3)"),
29
- gr.Dropdown(choices=[(0, "Graduate"), (1, "Not Graduate")], label="Education"),
30
- gr.Dropdown(choices=[(1, "Yes"), (0, "No")], label="Self Employed"),
31
  gr.Number(label="Applicant Income"),
32
- gr.Number(label="Co-applicant Income"),
33
  gr.Number(label="Loan Amount"),
34
- gr.Number(label="Term (e.g. 360)"),
35
- gr.Dropdown(choices=[(1, "Good"), (0, "Bad")], label="Credit History"),
36
- gr.Dropdown(choices=[(0, "Rural"), (1, "Semiurban"), (2, "Urban")], label="Property Area")
37
  ],
38
  outputs="text",
39
- title="Loan Approval Predictor",
40
- description="Enter applicant details to predict loan eligibility."
41
  )
42
 
43
- # 4. Launch with the correct settings for Hugging Face
44
- runapp.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  import joblib
3
+ import numpy as np
4
 
5
+ # Load the trained loan model
6
+ model = joblib.load("loan_model.pkl")
7
 
8
+ def predict_loan_status(
9
+ married,
10
+ dependents,
11
+ education,
12
+ applicant_income,
13
+ coapplicant_income,
14
+ loan_amount,
15
+ loan_amount_term,
16
+ credit_history,
17
+ property_area
18
+ ):
19
+ """
20
+ This function:
21
+ - Receives user inputs
22
+ - Converts categorical inputs to numeric values
23
+ - Uses the trained model to predict loan status
24
+ """
25
 
26
+ # Encoding categorical variables (must match training logic)
27
+ married = 1 if married == "Yes" else 0
28
+ education = 1 if education == "Graduate" else 0
29
 
30
+ property_area_map = {
31
+ "Urban": 2,
32
+ "Semiurban": 1,
33
+ "Rural": 0
34
+ }
35
+ property_area = property_area_map[property_area]
36
+
37
+ # Combine inputs into model-ready format
38
+ features = np.array([[
39
+ married,
40
+ dependents,
41
+ education,
42
+ applicant_income,
43
+ coapplicant_income,
44
+ loan_amount,
45
+ loan_amount_term,
46
+ credit_history,
47
+ property_area
48
+ ]])
49
+
50
+ # Make prediction
51
  prediction = model.predict(features)[0]
52
+
53
  return "Loan Approved" if prediction == 1 else "Loan Rejected"
54
 
55
+
56
+ # Gradio Interface
57
+ interface = gr.Interface(
58
+ fn=predict_loan_status,
59
  inputs=[
60
+ gr.Radio(["Yes", "No"], label="Married"),
61
+ gr.Number(label="Number of Dependents"),
62
+ gr.Radio(["Graduate", "Not Graduate"], label="Education"),
 
 
63
  gr.Number(label="Applicant Income"),
64
+ gr.Number(label="Coapplicant Income"),
65
  gr.Number(label="Loan Amount"),
66
+ gr.Number(label="Loan Amount Term"),
67
+ gr.Radio([1, 0], label="Credit History (1 = Good, 0 = Bad)"),
68
+ gr.Radio(["Urban", "Semiurban", "Rural"], label="Property Area"),
69
  ],
70
  outputs="text",
71
+ title="Loan Status Prediction System",
72
+ description="Predict whether a loan application will be approved or rejected using a trained machine learning model."
73
  )
74
 
75
+ if __name__ == "__main__":
76
+ interface.launch()