-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
228 lines (187 loc) · 8.21 KB
/
app.py
File metadata and controls
228 lines (187 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import seaborn as sns
import streamlit as st
import matplotlib.pyplot as plt
import preprocessor, helper
import datetime
import time
# Set page configuration for Streamlit app
st.set_page_config(
page_title="ChatSense Chat Analyzer",
layout="wide",
initial_sidebar_state="expanded"
)
# Sidebar Logo - styled 'CS' with red and white colors using HTML & CSS
st.sidebar.markdown(
"""
<div style="background-color:#B22222; padding:15px; text-align:center; border-radius:8px; margin-bottom:10px;">
<span style="color:white; font-weight:bold; font-size:32px; font-family:monospace;">C</span>
<span style="color:#B22222; background:white; font-weight:bold; font-size:32px; font-family:monospace; padding:0 6px; border-radius:4px;">S</span>
</div>
""",
unsafe_allow_html=True
)
# Sidebar title and description
st.sidebar.title("🧠 ChatSense Chat Analyzer")
st.sidebar.markdown("Upload your WhatsApp chat export text file and explore insightful analytics! 🚀")
# Initialize session state
if "show_analysis" not in st.session_state:
st.session_state["show_analysis"] = False
# File uploader to upload WhatsApp chat .txt file
uploaded_file = st.sidebar.file_uploader("Choose a file", type=["txt"])
# Check if a file is uploaded
if uploaded_file is not None:
# Read uploaded file bytes and decode to string
bytes_data = uploaded_file.getvalue()
data = bytes_data.decode("utf-8")
# Preprocess raw chat data into a DataFrame
# df = preprocessor.preprocess(data)
with st.spinner("Analyzing your chat..."):
df = preprocessor.preprocess(data)
time.sleep(1) # optional: simulate delay
# Fetch unique users from chat excluding 'group_notification'
user_list = df['user'].unique().tolist()
if 'group_notification' in user_list:
user_list.remove('group_notification')
user_list.sort()
user_list.insert(0, "Overall") # Add 'Overall' option for aggregate analysis
# Dropdown to select user or overall chat analysis
selected_user = st.sidebar.selectbox("Show analysis wrt", user_list)
# Button triggers analysis display
# When button is clicked, set session state to True
if st.sidebar.button("Show Analysis"):
st.session_state["show_analysis"] = True
# Only show analysis if session state is True
if st.session_state["show_analysis"]:
# Top statistics section
st.markdown("## 📊 Top Statistics")
num_messages, words, num_media_messages, num_links = helper.fetch_stats(selected_user, df)
# Display key metrics in columns
col1, col2, col3, col4 = st.columns(4)
col1.metric("💬 Total Messages", f"{num_messages}")
col2.metric("📝 Total Words", f"{words}")
col3.metric("🖼️ Media Shared", f"{num_media_messages}")
col4.metric("🔗 Links Shared", f"{num_links}")
# Monthly timeline plot inside expandable container
with st.expander("📅 Monthly Timeline"):
timeline = helper.monthly_timeline(selected_user, df)
fig, ax = plt.subplots(figsize=(12, 4))
ax.plot(timeline['time'], timeline['message'], color='green', marker='o')
plt.xticks(rotation=45)
plt.tight_layout()
st.pyplot(fig)
# Daily timeline plot inside expandable container
with st.expander("📅 Daily Timeline"):
daily_timeline = helper.daily_timeline(selected_user, df)
fig, ax = plt.subplots(figsize=(12, 4))
ax.plot(daily_timeline['only_date'], daily_timeline['message'], color='black', marker='o')
plt.xticks(rotation=45)
plt.tight_layout()
st.pyplot(fig)
# Activity Map section showing busiest day and month
st.markdown("## 🔥 Activity Map")
col1, col2 = st.columns(2)
# Most busy day bar chart
with col1:
st.subheader("📅 Most Busy Day")
busy_day = helper.week_activity_map(selected_user, df)
fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(busy_day.index, busy_day.values, color='purple')
plt.xticks(rotation=45)
st.pyplot(fig)
# Most busy month bar chart
with col2:
st.subheader("📆 Most Busy Month")
busy_month = helper.month_activity_map(selected_user, df)
fig, ax = plt.subplots(figsize=(6, 4))
ax.bar(busy_month.index, busy_month.values, color='orange')
plt.xticks(rotation=45)
st.pyplot(fig)
# Weekly activity heatmap visualization
st.markdown("## 📅 Weekly Activity Heatmap")
user_heatmap = helper.activity_heatmap(selected_user, df)
fig, ax = plt.subplots(figsize=(12, 4))
sns.heatmap(user_heatmap, cmap='YlGnBu', linewidths=0.5, linecolor='gray', ax=ax)
st.pyplot(fig)
# Show most busy users only if overall analysis selected
if selected_user == "Overall":
st.markdown("## 👥 Most Busy Users")
x, new_df = helper.most_busy_users(df)
col1, col2 = st.columns([3, 2])
# Bar chart for top users
with col1:
fig, ax = plt.subplots(figsize=(8, 4))
ax.bar(x.index, x.values, color='red')
plt.xticks(rotation=45)
st.pyplot(fig)
# Dataframe with percentages of user activity
with col2:
st.dataframe(new_df.style.highlight_max(axis=0))
# Word Cloud section
st.markdown("## ☁️ Word Cloud")
df_wc = helper.create_wordCloud(selected_user, df)
# Display word cloud if available, else show warning
if df_wc is not None:
fig, ax = plt.subplots(figsize=(8, 4))
ax.imshow(df_wc)
ax.axis('off')
st.pyplot(fig)
else:
st.warning("😕 No sufficient words found to generate a Word Cloud for this user.")
# Most common words bar chart
st.markdown("## 📝 Most Common Words")
most_common_df = helper.most_common_words(selected_user, df)
if most_common_df.empty:
st.warning("😕 No common words found for this user.")
else:
fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(most_common_df[0], most_common_df[1], color='teal')
plt.xticks(rotation=45)
st.pyplot(fig)
# Emoji analysis section
st.markdown("## 😀 Emoji Analysis")
emoji_df = helper.emoji_helper(selected_user, df)
col1, col2 = st.columns(2)
with col1:
# Display emoji dataframe or warning if empty
if emoji_df.empty:
st.warning("😕 No emojis found for this user.")
else:
st.dataframe(emoji_df)
with col2:
# Display pie chart of top emojis if data is available
if not emoji_df.empty:
fig, ax = plt.subplots(figsize=(5, 5))
ax.pie(emoji_df[1].head(), labels=emoji_df[0].head(), autopct="%0.2f%%", startangle=140, colors=sns.color_palette("pastel"))
st.pyplot(fig)
# ---- DOWNLOAD CSV ----
st.markdown("## 📥 Download Stats")
user_df = df[df['user'] == selected_user] if selected_user != "Overall" else df
csv = user_df.to_csv(index=False)
st.download_button("Download Raw Chat Data as CSV", data=csv, file_name='chat_data.csv', mime='text/csv')
# import datetime
if uploaded_file is not None:
# ... all your analysis code here ...
# Footer at the end of the app
current_year = datetime.datetime.now().year
st.markdown(
f"""
<hr>
<p style='text-align:center; font-size:14px; color:gray;'>
Made with ❤️ by Kush Gupta | © {current_year}
</p>
""",
unsafe_allow_html=True
)
else:
st.info("👈 Please upload a WhatsApp chat export `.txt` file to get started.")
# Optional: show footer even if no file uploaded
current_year = datetime.datetime.now().year
st.markdown(
f"""
<hr>
<p style='text-align:center; font-size:14px; color:gray;'>
Made with ❤️ by Kush Gupta | © {current_year}
</p>
""",
unsafe_allow_html=True
)