jkg012 commited on
Commit
33fe11c
·
verified ·
1 Parent(s): e05ede9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -61
app.py CHANGED
@@ -1,61 +1,51 @@
1
- import gradio as gr
2
- import pickle
3
- import numpy as np
4
- import pandas as pd
5
- from sklearn.preprocessing import StandardScaler
6
-
7
- # Load your trained model
8
- with open("log_reg.pkl", "rb") as f:
9
- model = pickle.load(f)
10
-
11
- # Load or define your scaler (assumed you saved it similarly)
12
- with open("scaler.pkl", "rb") as f:
13
- scaler = pickle.load(f)
14
-
15
- # Define the input features that your model uses, e.g.:
16
- input_features = [
17
- "Age", "Income", "Debt", "Debt_to_Income",
18
- "Payment_History_Num", "Is_Employed",
19
- # ... add other numeric features or encoded categorical features needed
20
- ]
21
-
22
- def predict_creditworthiness(age, income, debt, payment_history, employment_status):
23
- # Feature engineering like in training
24
- debt_to_income = debt / (income + 1)
25
- payment_map = {"Bad": 0, "Average": 1, "Good": 2}
26
- payment_num = payment_map.get(payment_history, 1)
27
- employed_num = 1 if employment_status == "Employed" else 0
28
-
29
- # Create feature array in correct order expected by model
30
- input_data = np.array([[age, income, debt, debt_to_income, payment_num, employed_num]])
31
-
32
- # Scale features
33
- input_scaled = scaler.transform(input_data)
34
-
35
- # Predict probability and class
36
- proba = model.predict_proba(input_scaled)[0, 1]
37
- pred_class = model.predict(input_scaled)[0]
38
-
39
- credit_status = "Good Credit" if pred_class == 1 else "Bad Credit"
40
- return f"Prediction: {credit_status} (Probability of good credit: {proba:.2f})"
41
-
42
- # Gradio input widgets reflecting your features
43
- iface = gr.Interface(
44
- fn=predict_creditworthiness,
45
- inputs=[
46
- gr.Number(label="Age", value=30, precision=0),
47
- gr.Number(label="Income", value=50000, precision=2),
48
- gr.Number(label="Debt", value=5000, precision=2),
49
- gr.Dropdown(label="Payment History", choices=["Bad", "Average", "Good"], value="Average"),
50
- gr.Radio(label="Employment Status", choices=["Employed", "Unemployed"], value="Employed"),
51
- ],
52
- outputs="text",
53
- title="Credit Scoring Model (Logistic Regression)",
54
- description="Enter your financial details to predict creditworthiness."
55
- )
56
-
57
- if __name__ == "__main__":
58
- iface.launch()
59
-
60
-
61
-
 
1
+ import gradio as gr
2
+ import pickle
3
+ import numpy as np
4
+
5
+ # Load model and scaler
6
+ with open("log_reg.pkl", "rb") as f:
7
+ model = pickle.load(f)
8
+
9
+ with open("scaler.pkl", "rb") as f:
10
+ scaler = pickle.load(f)
11
+
12
+ def predict_creditworthiness(age, income, debt, payment_history, employment_status):
13
+ try:
14
+ debt_to_income = debt / (income + 1)
15
+ payment_map = {"Bad": 0, "Average": 1, "Good": 2}
16
+ payment_num = payment_map.get(payment_history, 1)
17
+ employed_num = 1 if employment_status == "Employed" else 0
18
+
19
+ input_data = np.array([[age, income, debt, debt_to_income, payment_num, employed_num]])
20
+
21
+ # --- Debug prints ---
22
+ print("Input data", input_data)
23
+ print("Input shape", input_data.shape)
24
+ print("Scaler expects", scaler.mean_.shape) # Should be (6,)
25
+
26
+ input_scaled = scaler.transform(input_data)
27
+ proba = model.predict_proba(input_scaled)[0, 1]
28
+ pred_class = model.predict(input_scaled)[0]
29
+ credit_status = "Good Credit" if pred_class == 1 else "Bad Credit"
30
+ return f"Prediction: {credit_status} (Probability of good credit: {proba:.2f})"
31
+ except Exception as e:
32
+ # Print error for debug
33
+ print("Prediction error:", str(e))
34
+ return f"Error: {str(e)}"
35
+
36
+ iface = gr.Interface(
37
+ fn=predict_creditworthiness,
38
+ inputs=[
39
+ gr.Number(label="Age", value=30, precision=0),
40
+ gr.Number(label="Income", value=50000, precision=2),
41
+ gr.Number(label="Debt", value=5000, precision=2),
42
+ gr.Dropdown(label="Payment History", choices=["Bad", "Average", "Good"], value="Average"),
43
+ gr.Radio(label="Employment Status", choices=["Employed", "Unemployed"], value="Employed"),
44
+ ],
45
+ outputs="text",
46
+ title="Credit Scoring Model (Logistic Regression)",
47
+ description="Enter your financial details to predict creditworthiness."
48
+ )
49
+
50
+ if __name__ == "__main__":
51
+ iface.launch()