import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
import preprocessor
import helper
st.sidebar.title("Whatsapp Chat Analyzer")
uploaded_file = st.sidebar.file_uploader("Choose a file")
if uploaded_file is not None:
bytes_data = uploaded_file.getvalue()
data = bytes_data.decode("utf-8")
df = preprocessor.preprocess(data)
# Show Analysis wrt
user_list = df['user'].unique().tolist()
user_list.sort()
user_list.insert(0, "Overall")
selected_users = st.sidebar.selectbox("Show Analysis wrt", user_list)
if st.sidebar.button("Show Analysis"):
#stats Area
num_messages, words, num_media_messages, num_links = helper.fetch_stats(selected_users, df)
st.markdown(f'
TOP STATISTICS
', unsafe_allow_html=True)
col1, col2, col3 = st.columns(3)
with col1:
st.markdown(f'Total Messages
', unsafe_allow_html=True)
st.markdown(f'{num_messages}
', unsafe_allow_html=True)
with col2:
st.markdown(f'Total Words
', unsafe_allow_html=True)
st.markdown(f'{words}
', unsafe_allow_html=True)
with col3:
st.markdown(f'Total links Shared
', unsafe_allow_html=True)
st.markdown(f'{num_links}
', unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown(f'Total Media Shared
', unsafe_allow_html=True)
st.markdown(f'{num_media_messages}
', unsafe_allow_html=True)
with col2:
avg_messages_per_day = helper.avg_messages_per_day(selected_users,df)
st.markdown(f'Avg No of Messages Per day
', unsafe_allow_html=True)
st.markdown(f'{avg_messages_per_day}
', unsafe_allow_html=True)
if selected_users == 'Overall':
col1, col2, col3 = st.columns(3)
with col1:
first_last_msg = helper.first_last_msg(df)
st.markdown(f'First Message Sent
', unsafe_allow_html=True)
st.dataframe(first_last_msg.head(1))
with col2:
first_last_msg = helper.first_last_msg(df)
st.markdown(f'Last Message Sent
', unsafe_allow_html=True)
st.dataframe(first_last_msg.tail(1))
with col3:
connected_days = helper.connected_days(df)
st.markdown(f'Total Connected Days
', unsafe_allow_html=True)
st.markdown(f'{connected_days}
', unsafe_allow_html=True)
with col1:
max_active_day = helper.max_active_day(df)
st.markdown(f'Most Active Day
', unsafe_allow_html=True)
st.dataframe(max_active_day.head(1))
with col2:
max_active_month = helper.max_active_month(selected_users,df)
st.markdown(f'Most Active Month
', unsafe_allow_html=True)
st.dataframe(max_active_month.head(1))
# month wise
st.markdown(f'MONTH WISE
', unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown(f'GRAPH
', unsafe_allow_html=True)
timeline = helper.monthly_timeline(selected_users, df)
fig, ax = plt.subplots()
ax.plot(timeline['time'], timeline['message'], color='green')
plt.xticks(rotation='vertical')
st.pyplot(fig)
with col2:
timeline = helper.monthly_timeline_msg(selected_users, df)
st.markdown(f'Message Counts
', unsafe_allow_html=True)
st.dataframe(timeline)
# finding the busiest people in the group(Group Level)
if selected_users == 'Overall':
st.markdown(f'MOST BUSY USERS
', unsafe_allow_html=True)
x, new_df = helper.most_busy_users(df)
fig, ax = plt.subplots()
col1, col2 = st.columns(2)
with col1:
st.markdown(f'GRAPH
', unsafe_allow_html=True)
num_bars = len(x)
colors = plt.cm.rainbow(np.linspace(0, 1, num_bars))
ax.bar(x.index, x.values, color=colors,alpha=0.5)
plt.xticks(rotation='vertical')
st.pyplot(fig)
with col2:
st.markdown(f'CONTRIBUTIONS
', unsafe_allow_html=True)
st.dataframe(new_df)
# WordCloud
st.markdown(f'WORD WISE ANALYSIS
', unsafe_allow_html=True)
st.markdown(f'WordCloud
', unsafe_allow_html=True)
df_wc = helper.create_wordcloud(selected_users, df)
fig, ax = plt.subplots()
ax.imshow(df_wc)
st.pyplot(fig)
# # Emoji analysis
# st.markdown(f'EMOJI ANALYSIS
', unsafe_allow_html=True)
# col1,col2 = st.columns(2)
# with col1:
# st.markdown(f'Top 5 Emojis Used
', unsafe_allow_html=True)
# emoji_df = helper.emoji_helper(selected_users, df)
# st.dataframe(emoji_df.head(5))
# with col2:
# st.markdown(f'Pie Chart
', unsafe_allow_html=True)
# fig, ax = plt.subplots()
# ax.pie(emoji_df['Count '], labels=emoji_df['Emoji '], autopct='%1.2f%%', startangle=90)
# ax.axis('equal')
# st.pyplot(fig)
st.markdown(f'More features will be added soon
', unsafe_allow_html=True)