-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
216 lines (164 loc) · 5.99 KB
/
app.py
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
# -*- coding: utf-8 -*-
"""
WhatsApp Chat Analyser
This script uses Streamlit to create a web application for analyzing WhatsApp chat data.
It allows users to upload a text file containing a WhatsApp chat conversation and provides various analysis metrics and visualizations.
Author: Abhishek Santosh Gupta
GitHub: github.com/1abhi6
"""
import streamlit as st
from abc import ABC, abstractmethod
from preprocessor import Preprocess
from components import (
Plot,
SubHeader,
PADDING_TOP
)
from analysis import (
Analyse,
UserList,
GroupSpecificAnalysis
)
class Sidebar(ABC):
"""
Abstract base class for the sidebar of the WhatsApp Chat Analyser web application.
"""
def __init__(self):
st.sidebar.title('WhatsApp Chat Analyser')
self.sidebar()
def sidebar(self):
"""
Create the sidebar components and functionality.
"""
upload_file = st.sidebar.file_uploader(
'Choose a text file',
help='Upload a .txt file of chat without media exported from WhatsApp.'
)
if upload_file is not None:
bytes_data = upload_file.getvalue()
data = bytes_data.decode('utf-8')
try:
preprocessor_obj = Preprocess(data)
self.df = preprocessor_obj.text_to_df()
self.user_list_obj = UserList(self.df)
users_list = self.user_list_obj.user_list()
self.selected_user = st.sidebar.selectbox(
'Show Analysis', users_list)
self.analysis_obj = Analyse(self.df, self.selected_user)
self.show_analysis_btn()
except Exception as e:
st.sidebar.error(e)
@abstractmethod
def show_analysis_btn(self):
"""
Abstract method to be implemented by subclasses.
Show the button to trigger the analysis.
"""
pass
class Main(Sidebar):
"""
Main class for the WhatsApp Chat Analyser web application.
"""
def __init__(self):
"""
Initialize the Main class and set the page configuration.
"""
# Page configuration
st.set_page_config(
layout='wide', page_title="Abhi's WhatsApp Analyser", page_icon='📊')
super().__init__()
def show_analysis_btn(self):
"""
Override the show_analysis_btn method from the Sidebar class.
Show the analysis button and trigger the analysis when clicked.
"""
if st.sidebar.button('Show Chat Analysis'):
if not self.selected_user == 'Overall':
col1, col2, col3 = st.columns(3)
with col1:
st.write()
with col2:
# Give custom padding at top
st.markdown(PADDING_TOP, unsafe_allow_html=True)
st.title("{}'s Analysis".format(self.selected_user))
with col3:
st.write()
else:
col1, col2, col3 = st.columns(3)
with col1:
st.write()
with col2:
# Give custom padding at top
st.markdown(PADDING_TOP, unsafe_allow_html=True)
st.title('Overall Analysis')
with col3:
st.write()
self.group_specific_analysis = GroupSpecificAnalysis(
self.df, self.selected_user)
self.plot = Plot(self.df, self.selected_user)
st.divider()
self.quick_metric()
st.divider()
self.plot.plot_daily_timeline()
st.divider()
self.plot.plot_timeline()
st.divider()
self.plot.plot_most_active_day_of_week()
st.divider()
self.plot.plot_most_active_month()
self.plot_most_active_users()
st.divider()
self.plot.plot_word_cloud()
st.divider()
try:
self.plot.plot_most_common_words()
except Exception:
st.write(
'The data you provided has fewer metrics to show more about the selected user.')
st.divider()
self.plot.plot_most_used_emoji()
st.divider()
self.plot.plot_activity_heatmap()
def quick_metric(self):
"""
Show quick metrics of the chat conversation.
"""
SubHeader(
subheader='Quick Metrics',
tooltip='Quick overview of the entire chat.'
)
col1, col2, col3, col4 = st.columns(4)
with col1:
num_messages = self.analysis_obj.num_messages()
st.metric(label='Total Messages', value=num_messages)
with col2:
total_words = self.analysis_obj.total_words()
st.metric(label='Total Words', value=total_words)
with col3:
media_shared = self.analysis_obj.media_shared()
st.metric(label='Media Shared', value=media_shared)
with col4:
links_shared = self.analysis_obj.links_shared()
st.metric(label='Links Shared', value=links_shared)
def plot_most_active_users(self):
"""
Plot the most active users in the chat conversation.
"""
if self.selected_user == 'Overall':
st.divider()
col1, col2 = st.columns(2)
with col1:
SubHeader(
subheader='Most Active Users (Percentage)',
tooltip='Most active user with percentage of chats.'
)
users = self.group_specific_analysis.most_active_users_percentage()
st.dataframe(users)
with col2:
SubHeader(
subheader='Most Active Users',
tooltip='Most active user with number of chats.'
)
self.plot.plot_most_active_users()
if __name__ == '__main__':
Main()