File size: 1,678 Bytes
07f0306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import gradio as gr
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import LabelEncoder

# Sample BFSI dataset
data = {
    'Age': [25, 45, 30, 50, 28, 42],
    'Income': [300000, 1200000, 600000, 1500000, 350000, 1000000],
    'CreditScore': [720, 800, 760, 820, 710, 790],
    'RiskAppetite': ['High', 'Low', 'Medium', 'Low', 'High', 'Medium'],
    'LoanAmount': [200000, 600000, 400000, 750000, 220000, 580000]
}

# Prepare data
df = pd.DataFrame(data)
le = LabelEncoder()
df['RiskAppetiteEncoded'] = le.fit_transform(df['RiskAppetite'])

X = df[['Age', 'Income', 'CreditScore', 'RiskAppetiteEncoded']]
y = df['LoanAmount']

# Train model
model = DecisionTreeRegressor(random_state=42)
model.fit(X, y)

# Prediction function
def predict_loan(age, income, credit_score, risk_appetite):
    encoded_risk = le.transform([risk_appetite])[0]
    input_df = pd.DataFrame([[age, income, credit_score, encoded_risk]],
                            columns=['Age', 'Income', 'CreditScore', 'RiskAppetiteEncoded'])
    prediction = model.predict(input_df)[0]
    return f"Predicted Loan Amount: ₹{prediction:,.0f}"

# Build Gradio interface
app = gr.Interface(
    fn=predict_loan,
    inputs=[
        gr.Number(label="Age"),
        gr.Number(label="Annual Income (₹)"),
        gr.Number(label="Credit Score"),
        gr.Dropdown(choices=['High', 'Medium', 'Low'], label="Risk Appetite")
    ],
    outputs=gr.Textbox(label="Loan Prediction"),
    title="Loan Amount Predictor (Tree-Based Regression)",
    description="Enter customer details to predict the loan amount using a decision tree model."
)

# Launch the app
app.launch()