arun-gharami commited on
Commit
8c9b0fa
Β·
verified Β·
1 Parent(s): e1dc50e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +168 -0
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Lead.AI Customer Predictor β€” Live Demo
3
+ Predicts customer churn and purchase likelihood for small businesses.
4
+ Visit https://www.lead-ai.us for a custom deployment.
5
+ """
6
+
7
+ import gradio as gr
8
+ import numpy as np
9
+ from sklearn.ensemble import GradientBoostingClassifier
10
+ import warnings
11
+ warnings.filterwarnings("ignore")
12
+
13
+ # ── Train demo models ─────────────────────────────────────────────────────────
14
+ np.random.seed(99)
15
+ n = 3000
16
+
17
+ tenure = np.random.randint(1, 60, n)
18
+ purchases = np.random.randint(0, 50, n)
19
+ avg_spend = np.random.uniform(10, 800, n)
20
+ support_tix = np.random.randint(0, 15, n)
21
+ days_since = np.random.randint(1, 180, n)
22
+ email_opens = np.random.uniform(0, 1, n)
23
+
24
+ X = np.column_stack([tenure, purchases, avg_spend, support_tix, days_since, email_opens])
25
+
26
+ # Churn: high support + long since last purchase + low email engagement
27
+ churn_score = (support_tix * 0.3 + days_since * 0.01 - purchases * 0.05
28
+ - email_opens * 0.5 - tenure * 0.005)
29
+ y_churn = (churn_score > np.percentile(churn_score, 65)).astype(int)
30
+
31
+ # Purchase: recent + engaged + history
32
+ buy_score = (purchases * 0.4 + email_opens * 0.3 - days_since * 0.008 + avg_spend * 0.001)
33
+ y_buy = (buy_score > np.percentile(buy_score, 50)).astype(int)
34
+
35
+ churn_model = GradientBoostingClassifier(n_estimators=100, random_state=42)
36
+ churn_model.fit(X, y_churn)
37
+
38
+ buy_model = GradientBoostingClassifier(n_estimators=100, random_state=42)
39
+ buy_model.fit(X, y_buy)
40
+
41
+ SEGMENTS = {
42
+ (False, True): ("🌟 High-Value Active", "This customer is engaged and ready to buy. Prioritize for upsell offers."),
43
+ (False, False): ("βœ… Stable Retained", "Low churn risk but not currently primed to buy. Nurture with content."),
44
+ (True, True): ("⚑ At-Risk, Still Buying", "Buying but showing churn signals. Act now with a retention offer."),
45
+ (True, False): ("🚨 High Churn Risk", "This customer is disengaging. Send a personal win-back message today."),
46
+ }
47
+
48
+ def predict_customer(tenure, purchases, avg_spend, support_tickets,
49
+ days_since_purchase, email_open_rate):
50
+ feats = np.array([[tenure, purchases, avg_spend, support_tickets,
51
+ days_since_purchase, email_open_rate]])
52
+
53
+ churn_prob = churn_model.predict_proba(feats)[0][1]
54
+ buy_prob = buy_model.predict_proba(feats)[0][1]
55
+
56
+ churn_pct = round(churn_prob * 100, 1)
57
+ buy_pct = round(buy_prob * 100, 1)
58
+
59
+ is_churn = churn_pct >= 50
60
+ is_buy = buy_pct >= 50
61
+
62
+ segment, action = SEGMENTS[(is_churn, is_buy)]
63
+
64
+ churn_bar = f"{'β–ˆ' * int(churn_pct // 5)}{'β–‘' * (20 - int(churn_pct // 5))} {churn_pct}%"
65
+ buy_bar = f"{'β–ˆ' * int(buy_pct // 5)}{'β–‘' * (20 - int(buy_pct // 5))} {buy_pct}%"
66
+
67
+ report = f"## {segment}\n\n"
68
+ report += f"**Recommended Action:** {action}\n\n"
69
+ report += "---\n\n"
70
+ report += "### Prediction Scores\n\n"
71
+ report += f"**Churn Risk:** `{churn_bar}`\n\n"
72
+ report += f"**Purchase Likelihood:** `{buy_bar}`\n\n"
73
+ report += "---\n\n"
74
+ report += "### Key Signals\n\n"
75
+
76
+ signals = []
77
+ if days_since_purchase > 60:
78
+ signals.append(f"⚠️ Last purchase was **{days_since_purchase} days ago** β€” engagement is dropping")
79
+ if support_tickets >= 5:
80
+ signals.append(f"⚠️ **{support_tickets} support tickets** β€” customer may be frustrated")
81
+ if email_open_rate < 0.2:
82
+ signals.append("⚠️ **Low email engagement** β€” re-engagement campaign recommended")
83
+ if purchases >= 10:
84
+ signals.append(f"βœ“ **{purchases} purchases** β€” loyal customer history")
85
+ if email_open_rate >= 0.5:
86
+ signals.append("βœ“ **High email engagement** β€” customer is paying attention")
87
+ if tenure >= 12:
88
+ signals.append(f"βœ“ **{tenure} months** customer β€” long-term relationship")
89
+
90
+ if signals:
91
+ report += "\n".join(signals) + "\n\n"
92
+
93
+ report += "---\n\n"
94
+ report += "> ⚠️ Demo model trained on synthetic data. Your production system will learn "
95
+ report += "from your actual customer history for accurate predictions.\n\n"
96
+ report += "**[β†’ Get a Custom Customer Predictor for Your Business](https://www.lead-ai.us)**"
97
+
98
+ return report
99
+
100
+
101
+ with gr.Blocks(
102
+ title="Lead.AI Customer Predictor",
103
+ theme=gr.themes.Soft(primary_hue="blue"),
104
+ css=".footer { text-align:center; margin-top:20px; color:#666; }"
105
+ ) as demo:
106
+
107
+ gr.Markdown("""
108
+ # 🎯 Lead.AI Customer Predictor
109
+ ### Know Which Customers Are About to Leave β€” Before They Do
110
+
111
+ Enter customer data below. The AI will predict churn risk, purchase likelihood,
112
+ and tell you exactly what action to take.
113
+
114
+ > πŸ’Ό This is a live proof-of-concept. [Request a custom system β†’](https://www.lead-ai.us)
115
+ """)
116
+
117
+ with gr.Row():
118
+ with gr.Column():
119
+ gr.Markdown("### Customer Profile")
120
+ tenure_in = gr.Slider(1, 60, value=12, step=1,
121
+ label="Customer Tenure (months)")
122
+ purchases_in = gr.Slider(0, 50, value=8, step=1,
123
+ label="Total Purchases")
124
+ avg_spend_in = gr.Slider(10, 800, value=150, step=5,
125
+ label="Average Order Value ($)")
126
+ support_in = gr.Slider(0, 15, value=1, step=1,
127
+ label="Support Tickets (last 90 days)")
128
+ days_in = gr.Slider(1, 180, value=30, step=1,
129
+ label="Days Since Last Purchase")
130
+ email_in = gr.Slider(0.0, 1.0, value=0.4, step=0.05,
131
+ label="Email Open Rate (0 = never, 1 = always)")
132
+ btn = gr.Button("πŸ” Analyze This Customer", variant="primary", size="lg")
133
+
134
+ with gr.Column():
135
+ gr.Markdown("### AI Prediction")
136
+ report_md = gr.Markdown()
137
+
138
+ btn.click(
139
+ fn=predict_customer,
140
+ inputs=[tenure_in, purchases_in, avg_spend_in, support_in, days_in, email_in],
141
+ outputs=report_md,
142
+ )
143
+
144
+ gr.Examples(
145
+ examples=[
146
+ [2, 1, 45, 8, 90, 0.05],
147
+ [36, 28, 320, 0, 5, 0.75],
148
+ [8, 12, 180, 4, 45, 0.30],
149
+ [18, 3, 90, 0, 120, 0.10],
150
+ ],
151
+ inputs=[tenure_in, purchases_in, avg_spend_in, support_in, days_in, email_in],
152
+ label="Try These Customer Profiles",
153
+ )
154
+
155
+ gr.Markdown("""
156
+ ---
157
+ <div class="footer">
158
+ 🌐 <a href="https://www.lead-ai.us">www.lead-ai.us</a> &nbsp;|&nbsp;
159
+ πŸ’» <a href="https://github.com/Lead-AI-US/lead-ai-customer-predictor">GitHub</a> &nbsp;|&nbsp;
160
+ πŸ€— <a href="https://huggingface.co/lead-ai-labs">Hugging Face</a>
161
+ <br><br>
162
+ <strong>Need this running on your real customer data?</strong>
163
+ <a href="https://www.lead-ai.us">Request a Custom Lead.AI Setup β†’</a>
164
+ </div>
165
+ """)
166
+
167
+ if __name__ == "__main__":
168
+ demo.launch()