Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import xgboost as xgb
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pickle
|
| 5 |
+
import json
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
# Load pre-trained model
|
| 9 |
+
model = pickle.load(open("lapse_model.pkl", "rb"))
|
| 10 |
+
|
| 11 |
+
# Salesforce (Optional)
|
| 12 |
+
SALESFORCE_ENDPOINT = "https://orgfarm-ac78ff910d-dev-ed.develop.lightning.force.com/lightning/setup/SetupOneHome/home"
|
| 13 |
+
SALESFORCE_AUTH_TOKEN = "AmmfRcd6IiYaRtSGntBnzNMQU"
|
| 14 |
+
|
| 15 |
+
def predict_lapse(policy_id, last_premium_paid_date, payment_mode, policy_term, policy_age):
|
| 16 |
+
# Map payment_mode to numeric
|
| 17 |
+
payment_map = {"Annual": 0, "Semi-Annual": 1, "Quarterly": 2, "Monthly": 3}
|
| 18 |
+
payment_encoded = payment_map.get(payment_mode, 0)
|
| 19 |
+
|
| 20 |
+
# Feature vector
|
| 21 |
+
features = np.array([[policy_term, policy_age, payment_encoded]])
|
| 22 |
+
|
| 23 |
+
# Predict risk
|
| 24 |
+
risk_score = model.predict_proba(features)[0][1]
|
| 25 |
+
|
| 26 |
+
# Save to Salesforce (Optional)
|
| 27 |
+
try:
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": SALESFORCE_AUTH_TOKEN,
|
| 30 |
+
"Content-Type": "application/json"
|
| 31 |
+
}
|
| 32 |
+
data = {
|
| 33 |
+
"Name": policy_id,
|
| 34 |
+
"Lapse_Risk_Score__c": risk_score,
|
| 35 |
+
"Last_Paid_Date__c": last_premium_paid_date,
|
| 36 |
+
"Premium_Payment_Mode__c": payment_mode,
|
| 37 |
+
"Policy_Term__c": policy_term,
|
| 38 |
+
"Policy_Age__c": policy_age
|
| 39 |
+
#"Communication_Score__c": communication_score
|
| 40 |
+
}
|
| 41 |
+
response = requests.post(SALESFORCE_ENDPOINT, json=data, headers=headers)
|
| 42 |
+
print("Salesforce Response:", response.status_code, response.text)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print("Salesforce Integration Error:", e)
|
| 45 |
+
|
| 46 |
+
return round(risk_score, 3)
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=predict_lapse,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Text(label="Policy ID"),
|
| 53 |
+
gr.Text(label="Last Premium Paid Date (YYYY-MM-DD)"),
|
| 54 |
+
gr.Dropdown(["Annual", "Semi-Annual", "Quarterly", "Monthly"], label="Payment Mode"),
|
| 55 |
+
gr.Number(label="Policy Term (Years)"),
|
| 56 |
+
gr.Number(label="Policy Age (Years)"),
|
| 57 |
+
gr.Slider(0, 1, label="Communication Score (0 to 1)") # a feature from comm. history analysis
|
| 58 |
+
],
|
| 59 |
+
outputs=gr.Number(label="Lapse Risk Score (0 - 1)"),
|
| 60 |
+
title="Lapse Risk Predictor",
|
| 61 |
+
description="Predict the likelihood of policy lapse using XGBoost model"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
demo.launch()
|