Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +30 -0
- predictor.py +11 -0
- preprocessing.py +28 -0
- requirements.txt +5 -0
- train_model.py +18 -0
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from predictor import predict_risk
|
| 4 |
+
|
| 5 |
+
def predict(policy_id, last_premium_paid_date, payment_mode, policy_term, policy_age):
|
| 6 |
+
input_data = {
|
| 7 |
+
"policy_id": policy_id,
|
| 8 |
+
"last_premium_paid_date": last_premium_paid_date,
|
| 9 |
+
"payment_mode": payment_mode,
|
| 10 |
+
"policy_term": int(policy_term),
|
| 11 |
+
"policy_age": int(policy_age)
|
| 12 |
+
}
|
| 13 |
+
risk = predict_risk(input_data)
|
| 14 |
+
return {"Lapse Risk Score (0–1)": risk}
|
| 15 |
+
|
| 16 |
+
iface = gr.Interface(
|
| 17 |
+
fn=predict,
|
| 18 |
+
inputs=[
|
| 19 |
+
gr.Textbox(label="Policy ID"),
|
| 20 |
+
gr.Textbox(label="Last Premium Paid Date (YYYY-MM-DD)"),
|
| 21 |
+
gr.Dropdown(["Annual", "Semi-Annual", "Quarterly", "Monthly"], label="Payment Mode"),
|
| 22 |
+
gr.Number(label="Policy Term (Years)"),
|
| 23 |
+
gr.Number(label="Policy Age (Years)")
|
| 24 |
+
],
|
| 25 |
+
outputs="json",
|
| 26 |
+
title="Lapse Risk Predictor",
|
| 27 |
+
description="Enter policy details to predict the risk of lapse using an XGBoost model"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
iface.launch()
|
predictor.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import xgboost as xgb
|
| 3 |
+
from preprocessing import preprocess_input
|
| 4 |
+
|
| 5 |
+
model = xgb.XGBClassifier()
|
| 6 |
+
model.load_model("model/xgb_model.json")
|
| 7 |
+
|
| 8 |
+
def predict_risk(input_dict):
|
| 9 |
+
df = preprocess_input(input_dict)
|
| 10 |
+
score = model.predict_proba(df)[0][1]
|
| 11 |
+
return round(score, 2)
|
preprocessing.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
def encode_payment_mode(mode):
|
| 6 |
+
return {"Annual": 0, "Semi-Annual": 1, "Quarterly": 2, "Monthly": 3}.get(mode, -1)
|
| 7 |
+
|
| 8 |
+
def calculate_months_since(date_str):
|
| 9 |
+
try:
|
| 10 |
+
delta = datetime.now() - datetime.strptime(date_str, "%Y-%m-%d")
|
| 11 |
+
return delta.days // 30
|
| 12 |
+
except:
|
| 13 |
+
return 0
|
| 14 |
+
|
| 15 |
+
def preprocess_input(data):
|
| 16 |
+
return pd.DataFrame([{
|
| 17 |
+
"months_since_last_payment": calculate_months_since(data["last_premium_paid_date"]),
|
| 18 |
+
"payment_mode_encoded": encode_payment_mode(data["payment_mode"]),
|
| 19 |
+
"policy_term": data["policy_term"],
|
| 20 |
+
"policy_age": data["policy_age"]
|
| 21 |
+
}])
|
| 22 |
+
|
| 23 |
+
def preprocess_dataframe(df):
|
| 24 |
+
df["months_since_last_payment"] = df["last_premium_paid_date"].apply(calculate_months_since)
|
| 25 |
+
df["payment_mode_encoded"] = df["payment_mode"].apply(encode_payment_mode)
|
| 26 |
+
X = df[["months_since_last_payment", "payment_mode_encoded", "policy_term", "policy_age"]]
|
| 27 |
+
y = df["risk"]
|
| 28 |
+
return X, y
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio==4.24.0
|
| 3 |
+
xgboost==2.0.3
|
| 4 |
+
pandas==2.2.2
|
| 5 |
+
scikit-learn==1.5.0
|
train_model.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import xgboost as xgb
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
from preprocessing import preprocess_dataframe
|
| 6 |
+
|
| 7 |
+
# Sample training data
|
| 8 |
+
data = pd.DataFrame([
|
| 9 |
+
{"last_premium_paid_date": "2023-06-15", "payment_mode": "Annual", "policy_term": 15, "policy_age": 3, "risk": 1},
|
| 10 |
+
{"last_premium_paid_date": "2024-03-10", "payment_mode": "Monthly", "policy_term": 20, "policy_age": 2, "risk": 0},
|
| 11 |
+
])
|
| 12 |
+
|
| 13 |
+
X, y = preprocess_dataframe(data)
|
| 14 |
+
model = xgb.XGBClassifier()
|
| 15 |
+
model.fit(X, y)
|
| 16 |
+
|
| 17 |
+
os.makedirs("model", exist_ok=True)
|
| 18 |
+
model.save_model("model/xgb_model.json")
|