Spaces:
Sleeping
Sleeping
Phạm Anh Tuấn
commited on
Commit
·
a9200d2
1
Parent(s):
882e0e7
add data analyze
Browse files
app.py
CHANGED
@@ -1,4 +1,63 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import json
|
3 |
import streamlit as st
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
from wordcloud import WordCloud
|
7 |
|
8 |
+
# Define the Streamlit app
|
9 |
+
st.title("Data Analysis and Visualization")
|
10 |
+
|
11 |
+
# File upload and processing
|
12 |
+
uploaded_file = st.file_uploader("Upload JSON File", type=["json"])
|
13 |
+
if uploaded_file:
|
14 |
+
loaded_dict = json.load(uploaded_file)
|
15 |
+
df = pd.DataFrame(loaded_dict)
|
16 |
+
st.subheader("Dataframe (df)")
|
17 |
+
st.write(df)
|
18 |
+
|
19 |
+
# Group by and aggregate data
|
20 |
+
grouped = df.groupby('A').agg({'S': ['count', lambda x: (x == 'great').sum(), lambda x: (x == 'ok').sum(), lambda x: (x == 'bad').sum()]})
|
21 |
+
grouped.columns = grouped.columns.map('_'.join)
|
22 |
+
grouped = grouped.reset_index()
|
23 |
+
grouped = grouped.rename(columns={'A': 'Aspect', 'S_count': 'Freq', 'S_<lambda_0>': 'Great', 'S_<lambda_1>': 'Ok', 'S_<lambda_2>': 'Bad'})
|
24 |
+
|
25 |
+
st.subheader("Top Aspects by Frequency")
|
26 |
+
st.write(grouped.sort_values(by="Freq", ascending=False).head(5))
|
27 |
+
|
28 |
+
# Sentiment Distribution Chart
|
29 |
+
sentiment_distribution = df["S"].value_counts(normalize=True) * 100
|
30 |
+
palette_color = sns.color_palette('bright')
|
31 |
+
|
32 |
+
st.subheader("Sentiment Distribution")
|
33 |
+
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))
|
34 |
+
|
35 |
+
ax1.pie(sentiment_distribution, labels=sentiment_distribution.index, autopct='%1.1f%%', startangle=140)
|
36 |
+
ax1.axis('equal')
|
37 |
+
ax1.set_title("Sentiment Distribution %")
|
38 |
+
|
39 |
+
sns.countplot(x="S", data=df, palette=palette_color, ax=ax2)
|
40 |
+
ax2.set_title("Sentiment Distribution Counts")
|
41 |
+
|
42 |
+
st.pyplot(fig)
|
43 |
+
|
44 |
+
# Word Cloud
|
45 |
+
aspect_terms = " ".join(df["A"])
|
46 |
+
wordcloud = WordCloud(
|
47 |
+
width=800,
|
48 |
+
height=400,
|
49 |
+
background_color='white',
|
50 |
+
max_words=100,
|
51 |
+
colormap='inferno',
|
52 |
+
contour_width=3,
|
53 |
+
contour_color='red',
|
54 |
+
).generate(aspect_terms)
|
55 |
+
|
56 |
+
st.subheader("Word Cloud for Most Mentioned Aspects")
|
57 |
+
plt.figure(figsize=(10, 5))
|
58 |
+
plt.imshow(wordcloud, interpolation='bilinear')
|
59 |
+
plt.title("Most mentioned aspect terms")
|
60 |
+
plt.axis("off")
|
61 |
+
st.pyplot()
|
62 |
+
|
63 |
+
st.sidebar.markdown("**Upload a JSON file to get started.**")
|