Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,22 +8,25 @@ import requests
|
|
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/
|
13 |
-
SALESFORCE_AUTH_TOKEN = "
|
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 |
-
#
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
try:
|
28 |
headers = {
|
29 |
"Authorization": SALESFORCE_AUTH_TOKEN,
|
@@ -35,8 +38,8 @@ def predict_lapse(policy_id, last_premium_paid_date, payment_mode, policy_term,
|
|
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 |
-
|
40 |
}
|
41 |
response = requests.post(SALESFORCE_ENDPOINT, json=data, headers=headers)
|
42 |
print("Salesforce Response:", response.status_code, response.text)
|
@@ -45,7 +48,7 @@ def predict_lapse(policy_id, last_premium_paid_date, payment_mode, policy_term,
|
|
45 |
|
46 |
return round(risk_score, 3)
|
47 |
|
48 |
-
# Gradio
|
49 |
demo = gr.Interface(
|
50 |
fn=predict_lapse,
|
51 |
inputs=[
|
@@ -53,8 +56,8 @@ demo = gr.Interface(
|
|
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 |
-
|
58 |
],
|
59 |
outputs=gr.Number(label="Lapse Risk Score (0 - 1)"),
|
60 |
title="Lapse Risk Predictor",
|
|
|
8 |
# Load pre-trained model
|
9 |
model = pickle.load(open("lapse_model.pkl", "rb"))
|
10 |
|
11 |
+
# Salesforce (Optional - replace with your actual endpoint and secure token handling!)
|
12 |
+
SALESFORCE_ENDPOINT = "https://orgfarm-ac78ff910d-dev-ed.develop.lightning.force.com/services/data/vXX.0/sobjects/Lapse_Risk__c/"
|
13 |
+
SALESFORCE_AUTH_TOKEN = "Bearer YOUR_SALESFORCE_TOKEN" # Use environment variable in production!
|
14 |
|
15 |
+
def predict_lapse(policy_id, last_premium_paid_date, payment_mode, policy_term, policy_age, communication_score):
|
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 |
+
# Create feature array with 4 features
|
21 |
+
features = np.array([[policy_term, policy_age, payment_encoded, communication_score]])
|
|
|
|
|
|
|
22 |
|
23 |
+
# Predict lapse risk
|
24 |
+
try:
|
25 |
+
risk_score = model.predict_proba(features)[0][1]
|
26 |
+
except Exception as e:
|
27 |
+
return f"Prediction failed: {e}"
|
28 |
+
|
29 |
+
# OPTIONAL: Send to Salesforce
|
30 |
try:
|
31 |
headers = {
|
32 |
"Authorization": SALESFORCE_AUTH_TOKEN,
|
|
|
38 |
"Last_Paid_Date__c": last_premium_paid_date,
|
39 |
"Premium_Payment_Mode__c": payment_mode,
|
40 |
"Policy_Term__c": policy_term,
|
41 |
+
"Policy_Age__c": policy_age,
|
42 |
+
"Communication_Score__c": communication_score
|
43 |
}
|
44 |
response = requests.post(SALESFORCE_ENDPOINT, json=data, headers=headers)
|
45 |
print("Salesforce Response:", response.status_code, response.text)
|
|
|
48 |
|
49 |
return round(risk_score, 3)
|
50 |
|
51 |
+
# Gradio UI
|
52 |
demo = gr.Interface(
|
53 |
fn=predict_lapse,
|
54 |
inputs=[
|
|
|
56 |
gr.Text(label="Last Premium Paid Date (YYYY-MM-DD)"),
|
57 |
gr.Dropdown(["Annual", "Semi-Annual", "Quarterly", "Monthly"], label="Payment Mode"),
|
58 |
gr.Number(label="Policy Term (Years)"),
|
59 |
+
gr.Number(label="Policy Age (Years)"),
|
60 |
+
gr.Slider(0, 1, step=0.01, label="Communication Score (0 to 1)")
|
61 |
],
|
62 |
outputs=gr.Number(label="Lapse Risk Score (0 - 1)"),
|
63 |
title="Lapse Risk Predictor",
|