""" Lead.AI Customer Predictor — Live Demo Predicts customer churn and purchase likelihood for small businesses. Visit https://www.lead-ai.us for a custom deployment. """ import gradio as gr import numpy as np from sklearn.ensemble import GradientBoostingClassifier import warnings warnings.filterwarnings("ignore") # ── Train demo models ───────────────────────────────────────────────────────── np.random.seed(99) n = 3000 tenure = np.random.randint(1, 60, n) purchases = np.random.randint(0, 50, n) avg_spend = np.random.uniform(10, 800, n) support_tix = np.random.randint(0, 15, n) days_since = np.random.randint(1, 180, n) email_opens = np.random.uniform(0, 1, n) X = np.column_stack([tenure, purchases, avg_spend, support_tix, days_since, email_opens]) # Churn: high support + long since last purchase + low email engagement churn_score = (support_tix * 0.3 + days_since * 0.01 - purchases * 0.05 - email_opens * 0.5 - tenure * 0.005) y_churn = (churn_score > np.percentile(churn_score, 65)).astype(int) # Purchase: recent + engaged + history buy_score = (purchases * 0.4 + email_opens * 0.3 - days_since * 0.008 + avg_spend * 0.001) y_buy = (buy_score > np.percentile(buy_score, 50)).astype(int) churn_model = GradientBoostingClassifier(n_estimators=100, random_state=42) churn_model.fit(X, y_churn) buy_model = GradientBoostingClassifier(n_estimators=100, random_state=42) buy_model.fit(X, y_buy) SEGMENTS = { (False, True): ("🌟 High-Value Active", "This customer is engaged and ready to buy. Prioritize for upsell offers."), (False, False): ("✅ Stable Retained", "Low churn risk but not currently primed to buy. Nurture with content."), (True, True): ("⚡ At-Risk, Still Buying", "Buying but showing churn signals. Act now with a retention offer."), (True, False): ("🚨 High Churn Risk", "This customer is disengaging. Send a personal win-back message today."), } def predict_customer(tenure, purchases, avg_spend, support_tickets, days_since_purchase, email_open_rate): feats = np.array([[tenure, purchases, avg_spend, support_tickets, days_since_purchase, email_open_rate]]) churn_prob = churn_model.predict_proba(feats)[0][1] buy_prob = buy_model.predict_proba(feats)[0][1] churn_pct = round(churn_prob * 100, 1) buy_pct = round(buy_prob * 100, 1) is_churn = churn_pct >= 50 is_buy = buy_pct >= 50 segment, action = SEGMENTS[(is_churn, is_buy)] churn_bar = f"{'█' * int(churn_pct // 5)}{'░' * (20 - int(churn_pct // 5))} {churn_pct}%" buy_bar = f"{'█' * int(buy_pct // 5)}{'░' * (20 - int(buy_pct // 5))} {buy_pct}%" report = f"## {segment}\n\n" report += f"**Recommended Action:** {action}\n\n" report += "---\n\n" report += "### Prediction Scores\n\n" report += f"**Churn Risk:** `{churn_bar}`\n\n" report += f"**Purchase Likelihood:** `{buy_bar}`\n\n" report += "---\n\n" report += "### Key Signals\n\n" signals = [] if days_since_purchase > 60: signals.append(f"⚠️ Last purchase was **{days_since_purchase} days ago** — engagement is dropping") if support_tickets >= 5: signals.append(f"⚠️ **{support_tickets} support tickets** — customer may be frustrated") if email_open_rate < 0.2: signals.append("⚠️ **Low email engagement** — re-engagement campaign recommended") if purchases >= 10: signals.append(f"✓ **{purchases} purchases** — loyal customer history") if email_open_rate >= 0.5: signals.append("✓ **High email engagement** — customer is paying attention") if tenure >= 12: signals.append(f"✓ **{tenure} months** customer — long-term relationship") if signals: report += "\n".join(signals) + "\n\n" report += "---\n\n" report += "> ⚠️ Demo model trained on synthetic data. Your production system will learn " report += "from your actual customer history for accurate predictions.\n\n" report += "**[→ Get a Custom Customer Predictor for Your Business](https://www.lead-ai.us)**" return report with gr.Blocks( title="Lead.AI Customer Predictor", theme=gr.themes.Soft(primary_hue="blue"), css=".footer { text-align:center; margin-top:20px; color:#666; }" ) as demo: gr.Markdown(""" # 🎯 Lead.AI Customer Predictor ### Know Which Customers Are About to Leave — Before They Do Enter customer data below. The AI will predict churn risk, purchase likelihood, and tell you exactly what action to take. > 💼 This is a live proof-of-concept. [Request a custom system →](https://www.lead-ai.us) """) with gr.Row(): with gr.Column(): gr.Markdown("### Customer Profile") tenure_in = gr.Slider(1, 60, value=12, step=1, label="Customer Tenure (months)") purchases_in = gr.Slider(0, 50, value=8, step=1, label="Total Purchases") avg_spend_in = gr.Slider(10, 800, value=150, step=5, label="Average Order Value ($)") support_in = gr.Slider(0, 15, value=1, step=1, label="Support Tickets (last 90 days)") days_in = gr.Slider(1, 180, value=30, step=1, label="Days Since Last Purchase") email_in = gr.Slider(0.0, 1.0, value=0.4, step=0.05, label="Email Open Rate (0 = never, 1 = always)") btn = gr.Button("🔍 Analyze This Customer", variant="primary", size="lg") with gr.Column(): gr.Markdown("### AI Prediction") report_md = gr.Markdown() btn.click( fn=predict_customer, inputs=[tenure_in, purchases_in, avg_spend_in, support_in, days_in, email_in], outputs=report_md, ) gr.Examples( examples=[ [2, 1, 45, 8, 90, 0.05], [36, 28, 320, 0, 5, 0.75], [8, 12, 180, 4, 45, 0.30], [18, 3, 90, 0, 120, 0.10], ], inputs=[tenure_in, purchases_in, avg_spend_in, support_in, days_in, email_in], label="Try These Customer Profiles", ) gr.Markdown(""" ---
""") if __name__ == "__main__": demo.launch()