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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import pickle as pkl
|
| 3 |
+
import smtplib
|
| 4 |
+
from email.mime.multipart import MIMEMultipart
|
| 5 |
+
from email.mime.text import MIMEText
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import base64
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def set_light_background(image_file):
|
| 11 |
+
with open(image_file, "rb") as image:
|
| 12 |
+
encoded = base64.b64encode(image.read()).decode()
|
| 13 |
+
ext = image_file.split('.')[-1]
|
| 14 |
+
|
| 15 |
+
st.markdown(
|
| 16 |
+
f"""
|
| 17 |
+
<style>
|
| 18 |
+
.stApp {{
|
| 19 |
+
background: linear-gradient(
|
| 20 |
+
rgba(255, 255, 255, 0.7),
|
| 21 |
+
rgba(255, 255, 255, 0.7)
|
| 22 |
+
),
|
| 23 |
+
url("data:image/{ext};base64,{encoded}");
|
| 24 |
+
background-size: cover;
|
| 25 |
+
background-repeat: no-repeat;
|
| 26 |
+
background-attachment: fixed;
|
| 27 |
+
}}
|
| 28 |
+
h1, h2, h3, h4, h5, h6 {{
|
| 29 |
+
color: #1a1a1a;
|
| 30 |
+
}}
|
| 31 |
+
.stApp {{
|
| 32 |
+
color: #202020;
|
| 33 |
+
}}
|
| 34 |
+
</style>
|
| 35 |
+
""",
|
| 36 |
+
unsafe_allow_html=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
set_light_background("musicbg.jpg")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# 🎯 Add Image at the Top
|
| 44 |
+
st.image("inno.jpg", use_container_width=True,width=500)
|
| 45 |
+
st.markdown(
|
| 46 |
+
f"<h1 style='text-align: center;'>Subscription & Business Analytics</h1>",
|
| 47 |
+
unsafe_allow_html=True)
|
| 48 |
+
# 🎯 Add Image at the Top
|
| 49 |
+
st.image("music.png", use_container_width=True,width=500)
|
| 50 |
+
|
| 51 |
+
st.header("Business and Data Understanding")
|
| 52 |
+
st.subheader("Business Statements")
|
| 53 |
+
st.write("""
|
| 54 |
+
- The business problem statement is **music streaming service** that offers"
|
| 55 |
+
" different subscription plans (Free, Premium, Family). The main goal is to analyze"
|
| 56 |
+
" user behavior and predict customer churn—whether a user will leave the platform .""")
|
| 57 |
+
|
| 58 |
+
st.subheader("Business Objective")
|
| 59 |
+
st.write("""
|
| 60 |
+
- Identify key factors contributing to customer churn (e.g., subscription type, engagement metrics, payment method).
|
| 61 |
+
- Improve customer retention strategies by targeting at-risk users with promotions, better recommendations, or engagement campaigns.
|
| 62 |
+
- Enhance user experience by understanding listening habits, subscription patterns, and customer service interactions.
|
| 63 |
+
- Optimize marketing efforts to encourage conversions from free users to premium plans.""")
|
| 64 |
+
|
| 65 |
+
st.subheader("Business Constraints")
|
| 66 |
+
st.write("""
|
| 67 |
+
- Business constraints are limitations or restrictions that affect decision-making,
|
| 68 |
+
operations, or strategic planning within a business. They can include:
|
| 69 |
+
|
| 70 |
+
- Financial Constraints: Budget limitations, funding availability, or cost-cutting measures.
|
| 71 |
+
- Time Constraints: Project deadlines, time-to-market pressures, or regulatory timelines.
|
| 72 |
+
- Resource Constraints: Limited workforce, technology, or raw materials.
|
| 73 |
+
- Legal & Compliance Constraints: Industry regulations, data privacy laws (e.g., GDPR), or contractual obligations.
|
| 74 |
+
- Market Constraints: Customer demand, competition, or economic conditions.
|
| 75 |
+
- Technical Constraints: Software/hardware limitations, system integrations, or scalability issues.
|
| 76 |
+
- Operational Constraints: Supply chain restrictions, production capabilities, or logistics limitations.""")
|
| 77 |
+
|
| 78 |
+
st.header("Data Understanding")
|
| 79 |
+
st.write("""
|
| 80 |
+
- *Dataset Size:* 125000 rows × 20 columns.
|
| 81 |
+
- *Data Types:* float64(3), int64(11), object(4)
|
| 82 |
+
- *Data Features:*'age', 'location', 'subscription_type', 'payment_plan',
|
| 83 |
+
'num_subscription_pauses', 'customer_service_inquiries', 'signup_date',
|
| 84 |
+
'weekly_hours', 'average_session_length' 'song_skip_rate',
|
| 85 |
+
'weekly_songs_played', 'weekly_unique_songs', 'num_favorite_artists',
|
| 86 |
+
'num_platform_friends', 'num_playlists_created', 'num_shared_playlists',
|
| 87 |
+
'notifications_clicked', 'churned'. """)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
st.write(r"https://www.kaggle.com/competitions/streaming-subscription-churn-model/overview")
|
| 92 |
+
st.write("Demo data")
|
| 93 |
+
data = pd.read_csv("data.csv")
|
| 94 |
+
st.write(data)
|
| 95 |
+
dff = st.file_uploader("Upload File:")
|
| 96 |
+
|
| 97 |
+
with open("pipe.pkl","rb") as file:
|
| 98 |
+
pipe = pkl.load(file)
|
| 99 |
+
|
| 100 |
+
with open("model.pkl","rb") as file:
|
| 101 |
+
model = pkl.load(file)
|
| 102 |
+
|
| 103 |
+
if dff:
|
| 104 |
+
df =pd.read_csv(dff)
|
| 105 |
+
req = df
|
| 106 |
+
test = pipe.transform(req)
|
| 107 |
+
|
| 108 |
+
predicted = model.predict(test)
|
| 109 |
+
df["churned"] = predicted
|
| 110 |
+
|
| 111 |
+
st.table(df[["customer_id","churned"]])
|
| 112 |
+
|
| 113 |
+
ans = {
|
| 114 |
+
0: {
|
| 115 |
+
"name": "name",
|
| 116 |
+
"email": "email",
|
| 117 |
+
"mail": {
|
| 118 |
+
"subject": "🎶 Discover New Music You'll Love, name!",
|
| 119 |
+
"body": "Hey name,\n\nWe noticed you have a fantastic taste for music, "
|
| 120 |
+
"exploring tons of unique songs every week! To help you find even more tracks you'll love, "
|
| 121 |
+
"we've put together some recommendations just for you.\n\nBased on your listening habits, "
|
| 122 |
+
"we think you might enjoy:\n\n* **Daily Mixes:** Personalized playlists refreshed daily with music "
|
| 123 |
+
"we think you'll be excited about.\n* **New Music Friday:** Stay up-to-date with the freshest releases "
|
| 124 |
+
"from your favorite genres and artists.\n* **Genre Playlists:** Dive deeper into genres you love or explore "
|
| 125 |
+
"something completely new.\n\nReady to discover your next favorite song? Click here to explore: [Link to App/Music Platform]\n\nHappy listening,\nYour Music Team"
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
1: {
|
| 129 |
+
"name": "name",
|
| 130 |
+
"email": "email",
|
| 131 |
+
"mail": {
|
| 132 |
+
"subject": "We Miss You, Moon! Come Back for More Music & Family Fun!",
|
| 133 |
+
"body": "Hi name,\n\nWe've noticed you've been away from our music platform, "
|
| 134 |
+
"and we wanted to reach out and see if everything is alright."
|
| 135 |
+
" We truly value you as a Family plan subscriber and want to ensure you and your family are getting the most out of our service.\n\nTo welcome you back, "
|
| 136 |
+
"we'd like to offer you a special **30-day free trial** to re-engage with all the latest music, podcasts, "
|
| 137 |
+
"and features you might have missed. Rediscover millions of songs, create new playlists, "
|
| 138 |
+
"and share the joy of music with your family again.\n\nClick here to reactivate your free trial and start listening: [Link to Reactivation Page]\n\nWe hope to see you back soon!\n\nBest regards,\nYour Music Team"
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
st.bar_chart(df["churned"].value_counts())
|
| 144 |
+
|
| 145 |
+
mails_data = pd.DataFrame(ans).T
|
| 146 |
+
|
| 147 |
+
if st.button('Send Mail'):
|
| 148 |
+
for i in df.index:
|
| 149 |
+
cust_id = df.iloc[i]["customer_id"]
|
| 150 |
+
name = df.iloc[i]["name"]
|
| 151 |
+
email_id = df.iloc[i]["email"]
|
| 152 |
+
pred = df.iloc[i]["churned"]
|
| 153 |
+
|
| 154 |
+
sender_email = "moon24012407@gmail.com"
|
| 155 |
+
receiver_email = email_id
|
| 156 |
+
password = "rjig xxky lwfc xkcb"
|
| 157 |
+
|
| 158 |
+
# Email content
|
| 159 |
+
subject = mails_data.iloc[pred]['mail']['subject'].replace('name',name)
|
| 160 |
+
|
| 161 |
+
body = mails_data.iloc[pred]['mail']['body'].replace('name',name)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# Create the email
|
| 165 |
+
message = MIMEMultipart()
|
| 166 |
+
message["From"] = sender_email
|
| 167 |
+
message["To"] = receiver_email
|
| 168 |
+
message["Subject"] = subject
|
| 169 |
+
message.attach(MIMEText(body, "plain"))
|
| 170 |
+
|
| 171 |
+
# Set up the SMTP server and send the email
|
| 172 |
+
try:
|
| 173 |
+
server = smtplib.SMTP("smtp.gmail.com", 587)
|
| 174 |
+
server.starttls() # Secure the connection
|
| 175 |
+
server.login(sender_email, password)
|
| 176 |
+
server.sendmail(sender_email, receiver_email, message.as_string())
|
| 177 |
+
server.quit()
|
| 178 |
+
st.write(f"{cust_id} : Email sent successfully!")
|
| 179 |
+
except Exception as e:
|
| 180 |
+
st.write(f"{cust_id} : Failed to send email: {e}")
|
| 181 |
+
st.write("Accurace : 0.82328")
|