-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
377 lines (295 loc) · 14.2 KB
/
app.py
File metadata and controls
377 lines (295 loc) · 14.2 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import streamlit as st
import matplotlib.pyplot as plt
import dataPreprocessor, utils
import math
import datetime
import warnings
warnings.filterwarnings("ignore")
st.set_page_config(page_title="ChatStat ",page_icon=":chart:",layout="wide")
st.title(":chart: ChatStat | The WhatsApp Chat Analyzer")
# ---- SIDEBAR ----
st.sidebar.title("Welcome To ChatStat !")
st.sidebar.subheader("Filters")
analysis_filter = st.sidebar.multiselect(
"Analysis",
options=["Top Statistics", "Most Mentioned (Tagged) User","Daily Timeline","Activity Map","User Who Chats the Most","Word Cloud","Emoji Usage Analysis","Sentiment Analysis"],
default="Top Statistics"
)
# ---- UPLOAD SECTION ----
st.markdown("##")
st.write(":point_right: WhatsApp > Chat > Three dots > More > Export chat > Without media > Send or save the exported .txt file to your device.")
uploaded_file = st.file_uploader(":file_folder: Upload a WhatsApp Chat Exported (*.txt) File to Get Insights:",type=["txt"])
if uploaded_file:
df = dataPreprocessor.preprocess(uploaded_file)
with st.expander("Processed WhatsApp Chat Data"):
st.dataframe(df)
# ---- SIDEBAR ----
user_list = df['User'].unique().tolist()
user_list.insert(0,"All")
selected_user = st.sidebar.selectbox("User",user_list)
if st.button("Show Analysis"):
plot_data = [] # list containing tuples of names and corresponding figure of all plots generated
plot_color = '#25d366' # Set the color for the plot (hex code for a shade of green)
# Top Stats Area
if "Top Statistics" in analysis_filter:
message_count, words_count, media_count, links_count, emojis_count = utils.top_stats(selected_user,df)
st.title("Top Statistics")
col1, col2, col3 = st.columns(3)
col4, col5, col6 = st.columns(3)
with col1:
st.header("Total Messages")
st.title(message_count)
with col2:
st.header("Total Words")
st.title(words_count)
with col3:
st.header("Average Words per Message")
st.title(math.ceil(words_count/message_count))
with col4:
st.header("Media Shared")
st.title(media_count)
with col5:
st.header("Links Shared")
st.title(links_count)
with col6:
st.header("Emojis Shared")
st.title(emojis_count)
st.markdown("##")
# Most tagged user analysis area
if "Most Mentioned (Tagged) User" in analysis_filter:
# finding the User Who got mentioned the Most (group level)
if selected_user == 'All':
most_tagged_user_df = utils.most_tagged_users(df)
st.title("Most Mentioned (Tagged) User")
if not most_tagged_user_df.empty:
fig, ax = plt.subplots()
ax.barh(most_tagged_user_df['Tagged Users'], most_tagged_user_df['Frequency'], color=plot_color )
ax.set_ylabel('Tagged User')
ax.set_xlabel('Frequency')
ax.set_title('Tagged Users Frequency')
st.pyplot(fig)
# Append the figure and a corresponding name to the plot_data list
plot_data.append(("MostMentionedUsers", fig))
else:
st.info("No user has been tagged in the group.")
else:
# Display a warning message for the case when the selected user is not 'All'
st.warning(f"Warning: You've selected a specific user: {selected_user}. Please note that 'Most Mentioned (Tagged) User' analysis is for all users.")
st.markdown("##")
# User Who Chats the Most Area
if "User Who Chats the Most" in analysis_filter:
# finding the User Who Chats the Most (group level)
if selected_user == 'All':
st.title('User Who Chats the Most')
top_user_sr, top_users_contribution_df = utils.most_chat_users(df)
col1, col2 = st.columns(2)
with col1:
fig, ax = plt.subplots()
ax.bar(top_user_sr.index, top_user_sr.values,color=plot_color )
plt.xticks(rotation='vertical')
ax.set_xlabel('User')
ax.set_ylabel('Message Count')
ax.set_title('Top Users by Message Count')
st.pyplot(fig)
# Append the figure and a corresponding name to the plot_data list
plot_data.append(("UserWhoChatsTheMost", fig))
with col2:
st.dataframe(top_users_contribution_df)
else:
# Display a warning message for the case when the selected user is not 'All'
st.warning(f"Warning: You've selected a specific user: {selected_user}. Please note that 'User Who Chats the Most' analysis is for all users.")
st.markdown("##")
# Emoji Usage Analysis Area
if "Emoji Usage Analysis" in analysis_filter:
emojis_freq_df = utils.emoji_analysis(selected_user, df)
st.title("Emoji Usage Analysis")
if not emojis_freq_df.empty:
col1, col2 = st.columns(2)
# Plotting Top Emojis
with col1:
fig, ax = plt.subplots()
emoji_x = [f'Emoji {idx}' for idx in range(len(emojis_freq_df["Frequency"].head()))]
ax.bar(emoji_x, emojis_freq_df["Frequency"].head(), color=plot_color)
ax.set_ylabel('Frequency')
ax.set_title("Top Emojis by Usage Frequency")
st.pyplot(fig)
# Append the figure and a corresponding name to the plot_data list
plot_data.append(("EmojiUsageAnalysis", fig))
# Displaying Emoji Frequency DataFrame
with col2:
st.dataframe(emojis_freq_df)
else:
st.info("No Emoji has been shared.")
st.markdown("##")
# Check if "Daily Timeline" is selected in the analysis_filter
if "Daily Timeline" in analysis_filter:
# Display the title for the Daily Timeline section
st.title("Daily Timeline")
# Get the daily timeline data using the utils module
daily_timeline = utils.get_daily_timeline(selected_user, df).to_numpy()
# Ensure there is data available
if len(daily_timeline) == 0:
st.warning("No data available for Daily Timeline analysis.")
else:
# Create a subplot for the plot
fig, ax = plt.subplots()
# Subsample the dates for better readability
subsample_factor = max(len(daily_timeline) // 15, 1)
dates = daily_timeline[:, 0][::subsample_factor]
msg_count = daily_timeline[:, 1][::subsample_factor]
# Plot the data
ax.plot(dates, msg_count, color=plot_color)
# Set labels and title for the plot
ax.set_xlabel('Date')
ax.set_ylabel('Message Count')
ax.set_title('Message Count Over Date')
# Rotate x-axis labels for better readability
plt.xticks(rotation='vertical')
# Set the height of the plot
fig.set_figheight(3)
# Display the plot using Streamlit
st.pyplot(fig)
# Append the figure and a corresponding name to the plot_data list
plot_data.append(("DailyTimeline", fig))
# Add a markdown separator
st.markdown("##")
# Activity map Area
if "Activity Map" in analysis_filter:
st.title('Activity Map')
col1,col2 = st.columns(2)
with col1:
active_day_sr = utils.get_week_activity_map(selected_user,df)
fig,ax = plt.subplots()
ax.bar(active_day_sr.index,active_day_sr.values,color=plot_color)
ax.set_xlabel('Days of the week')
ax.set_ylabel('Message Count')
ax.set_title('Top messaging days')
plt.xticks(rotation='vertical')
st.pyplot(fig)
plot_data.append(("TopMessagingDays", fig))
with col2:
active_month_sr = utils.get_month_activity_map(selected_user, df)
fig, ax = plt.subplots()
ax.bar(active_month_sr.index, active_month_sr.values,color=plot_color)
ax.set_xlabel('Month name')
ax.set_ylabel('Message Count')
ax.set_title('Top messaging months')
plt.xticks(rotation='vertical')
st.pyplot(fig)
plot_data.append(("TopMessagingMonths", fig))
# Activity heatmap
day_hour_heatmap = utils.get_day_hour_heatmap(selected_user, df)
title = 'Day vs. Hour Activity Heatmap'
fig, ax = plt.subplots()
cax = ax.matshow(day_hour_heatmap, cmap='YlGnBu') # Using a blue-green colormap
fig.set_figheight(3) # Set height in inches
# Set ticks and labels
plt.xticks(range(len(day_hour_heatmap.columns)), day_hour_heatmap.columns)
plt.yticks(range(len(day_hour_heatmap.index)), day_hour_heatmap.index)
plt.gca().xaxis.set_tick_params(which='both', bottom=False)
# Add color bar
cbar = fig.colorbar(cax)
cbar.set_label('Message Count')
# Set the title
ax.set_title("Day vs. Hour Activity Heatmap")
st.pyplot(fig)
plot_data.append(("DayHourActivityHeatmap", fig))
st.markdown("##")
# WordClouds Area
if "Word Cloud" in analysis_filter:
st.title("Word Cloud")
col1,col2 = st.columns(2)
wc,most_common_word_df = utils.generate_wordcloud(selected_user, df)
if wc is not None:
with col1:
fig, ax = plt.subplots()
# Remove the axis and tick labels
ax.set_axis_off()
# Display the word cloud
ax.imshow(wc)
# Plot the word cloud in Streamlit
st.pyplot(fig)
plot_data.append(("WordCloud", fig))
with col2:
st.dataframe(most_common_word_df)
else:
st.warning("Insufficient chat text for creating a meaningful word cloud.")
st.markdown("##")
# -----Sentiment analysis Area------------------
if "Sentiment Analysis" in analysis_filter:
st.title("Sentiment Analysis")
col1, col2 = st.columns(2)
with col1:
positive, negative, neutral = utils.sentiment_analysis(selected_user, df)
labels = ['Positive', 'Negative', 'Neutral']
data = [positive, negative, neutral]
# Create a donut chart
fig, ax = plt.subplots()
ax.pie(data, startangle=90, wedgeprops=dict(width=0.4), autopct='%.1f%%', textprops={'rotation': 90})
ax.axis('equal')
# Add a circle in the center to create a donut hole
center_circle = plt.Circle((0, 0), 0.70, fc='white')
fig.gca().add_artist(center_circle)
# Display the legends
plt.legend(labels, loc='best')
st.pyplot(fig)
plot_data.append(("SentimentAnalysis", fig))
with col2:
if selected_user == 'All':
# Get the sentiment contributors
contributors = utils.user_sentiment_contributors(df)
# Display the most positive, negative, and neutral contributors
st.write("Most Positive Chat Contributor:", contributors['Most Positive User'])
st.write("Most Negative Chat Contributor:", contributors['Most Negative User'])
st.write("Most Neutral Chat Contributor:", contributors['Most Neutral User'])
st.markdown("##")
# ---- DOWNLOAD SECTION Area ----
if plot_data:
all_plots_zip_data = utils.generate_all_plots_zip(plot_data)
# Define button CSS
button_style = """
background-color: #25D366;
color: #fff;
border: none;
border-radius: 4px;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
"""
st.markdown(f'<a href="data:file/zip;base64,{all_plots_zip_data}" download="all_plots.zip"><button style="{button_style}">Download All Plots</button></a>', unsafe_allow_html=True)
st.markdown("##")
#---- Footer Area----
hide_st_style = """
<style>
#MainMenu {visibility : hidden;}
header {visibility : hidden;}
</style>
"""
st.markdown(hide_st_style,unsafe_allow_html=True)
# Get the current date and time
now = datetime.datetime.now()
# Format the copyright information
copyright = f"© {now.year} ChatStat"
footer = """
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: white;
color: black;
text-align: center;
}
</style>
<footer class="footer">
\U0001F512 We do not share or store your data beyond the scope of this application.<br>
"""+ copyright +""".
Developed with \U00002764 by Tirthesh Jain & Aditya Tomar
</footer>
"""
st.markdown(footer,unsafe_allow_html=True)