Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from sklearn.tree import DecisionTreeRegressor
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder
|
| 5 |
+
|
| 6 |
+
# Sample BFSI dataset
|
| 7 |
+
data = {
|
| 8 |
+
'Age': [25, 45, 30, 50, 28, 42],
|
| 9 |
+
'Income': [300000, 1200000, 600000, 1500000, 350000, 1000000],
|
| 10 |
+
'CreditScore': [720, 800, 760, 820, 710, 790],
|
| 11 |
+
'RiskAppetite': ['High', 'Low', 'Medium', 'Low', 'High', 'Medium'],
|
| 12 |
+
'LoanAmount': [200000, 600000, 400000, 750000, 220000, 580000]
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
# Prepare data
|
| 16 |
+
df = pd.DataFrame(data)
|
| 17 |
+
le = LabelEncoder()
|
| 18 |
+
df['RiskAppetiteEncoded'] = le.fit_transform(df['RiskAppetite'])
|
| 19 |
+
|
| 20 |
+
X = df[['Age', 'Income', 'CreditScore', 'RiskAppetiteEncoded']]
|
| 21 |
+
y = df['LoanAmount']
|
| 22 |
+
|
| 23 |
+
# Train model
|
| 24 |
+
model = DecisionTreeRegressor(random_state=42)
|
| 25 |
+
model.fit(X, y)
|
| 26 |
+
|
| 27 |
+
# Prediction function
|
| 28 |
+
def predict_loan(age, income, credit_score, risk_appetite):
|
| 29 |
+
encoded_risk = le.transform([risk_appetite])[0]
|
| 30 |
+
input_df = pd.DataFrame([[age, income, credit_score, encoded_risk]],
|
| 31 |
+
columns=['Age', 'Income', 'CreditScore', 'RiskAppetiteEncoded'])
|
| 32 |
+
prediction = model.predict(input_df)[0]
|
| 33 |
+
return f"Predicted Loan Amount: ₹{prediction:,.0f}"
|
| 34 |
+
|
| 35 |
+
# Build Gradio interface
|
| 36 |
+
app = gr.Interface(
|
| 37 |
+
fn=predict_loan,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Number(label="Age"),
|
| 40 |
+
gr.Number(label="Annual Income (₹)"),
|
| 41 |
+
gr.Number(label="Credit Score"),
|
| 42 |
+
gr.Dropdown(choices=['High', 'Medium', 'Low'], label="Risk Appetite")
|
| 43 |
+
],
|
| 44 |
+
outputs=gr.Textbox(label="Loan Prediction"),
|
| 45 |
+
title="Loan Amount Predictor (Tree-Based Regression)",
|
| 46 |
+
description="Enter customer details to predict the loan amount using a decision tree model."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Launch the app
|
| 50 |
+
app.launch()
|