Spaces:
Runtime error
Runtime error
Carlosito16
commited on
Commit
•
605b6aa
1
Parent(s):
21da37b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import plotly.express as px
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
st.set_page_config(
|
8 |
+
page_title = 'Streamlit Sample Dashboard Template',
|
9 |
+
page_icon = '✅',
|
10 |
+
layout = 'wide'
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
pie_new_color_discrete_sequence = [ 'royalblue', 'tomato', 'gold']
|
15 |
+
bar_new_color_discrete_sequence = [ ' royalblue', 'royalblue', 'tomato', 'gold']
|
16 |
+
|
17 |
+
|
18 |
+
def rating_to_sentiment(rating: float):
|
19 |
+
if rating >= 4:
|
20 |
+
sentiment = 'positive'
|
21 |
+
elif rating == 3:
|
22 |
+
sentiment = 'neutral'
|
23 |
+
elif rating <= 2:
|
24 |
+
sentiment = 'negative'
|
25 |
+
|
26 |
+
return sentiment
|
27 |
+
|
28 |
+
|
29 |
+
s = {'a': "rgb(235, 69, 95)",
|
30 |
+
'b': "rgb(255, 184, 76)",
|
31 |
+
'c':'rgb(43, 52, 103)'}
|
32 |
+
|
33 |
+
color_map = {'positive' : "royalblue",
|
34 |
+
'neutral': 'gold',
|
35 |
+
'negative': 'tomato'}
|
36 |
+
|
37 |
+
|
38 |
+
df = pd.read_csv('20230220_selected_df.csv',
|
39 |
+
index_col=0)
|
40 |
+
|
41 |
+
st.write("""
|
42 |
+
# Distribution of topics discussed from *Trustadvisor.com* on **Carrefour**
|
43 |
+
""")
|
44 |
+
|
45 |
+
clean_superclass = ['clean_BE', 'clean_PD', 'clean_DM', 'clean_AS']
|
46 |
+
group_df = df.loc[: , ['ratings'] + clean_superclass ]
|
47 |
+
group_df['sentiment'] = group_df['ratings'].apply(lambda x: rating_to_sentiment(x))
|
48 |
+
group_df['topic_count'] = group_df.iloc[ :, 1:5].sum(axis= 1)
|
49 |
+
heamap_data = group_df.groupby('sentiment').sum().reset_index().iloc[: , 2:6].to_numpy()
|
50 |
+
|
51 |
+
|
52 |
+
pie_fig = px.pie(data_frame= group_df,
|
53 |
+
names = group_df.sentiment,
|
54 |
+
color= 'sentiment',
|
55 |
+
color_discrete_map = color_map,
|
56 |
+
category_orders = {"sentiment": ['positive' ,'neutral' 'negative']},
|
57 |
+
hole= 0.5)
|
58 |
+
|
59 |
+
pie_fig.update_layout(legend=dict(
|
60 |
+
orientation="h",
|
61 |
+
yanchor="middle",
|
62 |
+
y= 1.15,
|
63 |
+
xanchor="center",
|
64 |
+
x= 0.5
|
65 |
+
))
|
66 |
+
|
67 |
+
|
68 |
+
bar_fig = px.histogram(data_frame=group_df,
|
69 |
+
x = 'topic_count',
|
70 |
+
color= 'sentiment',
|
71 |
+
color_discrete_map = color_map,
|
72 |
+
text_auto =True,
|
73 |
+
category_orders = {"sentiment": ['positive' ,'negative' 'neutral']})
|
74 |
+
# color_discrete_sequence = bar_new_color_discrete_sequence,
|
75 |
+
|
76 |
+
|
77 |
+
heatmap_fig = px.imshow(heamap_data,
|
78 |
+
labels=dict(x="4 Super Classes", y="Sentiment"),
|
79 |
+
x=['Buying Experience', 'Product', 'Delivery', 'After Sales'],
|
80 |
+
y=['Negative', 'Neutral', 'Positive'],
|
81 |
+
color_continuous_scale=['royalblue', 'gold', 'tomato'],
|
82 |
+
text_auto=True)
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
class_1_fig = px.pie(data_frame= group_df[group_df['clean_BE'] == 1],
|
87 |
+
names = group_df[group_df['clean_BE'] == 1].sentiment,
|
88 |
+
color = 'sentiment',
|
89 |
+
color_discrete_map = color_map,
|
90 |
+
category_orders = {"sentiment": ['positive' ,'negative' 'neutral']},
|
91 |
+
hole= 0.5)
|
92 |
+
|
93 |
+
class_2_fig = px.pie(data_frame= group_df[group_df['clean_PD'] == 1],
|
94 |
+
names = group_df[group_df['clean_PD'] == 1].sentiment,
|
95 |
+
color = 'sentiment',
|
96 |
+
color_discrete_map = color_map,
|
97 |
+
category_orders = {"sentiment": ['positive' ,'negative' 'neutral']},
|
98 |
+
hole= 0.5)
|
99 |
+
|
100 |
+
class_3_fig = px.pie(data_frame= group_df[group_df['clean_DM'] == 1],
|
101 |
+
names = group_df[group_df['clean_DM'] == 1].sentiment,
|
102 |
+
color = 'sentiment',
|
103 |
+
color_discrete_map = color_map,
|
104 |
+
category_orders = {"sentiment": ['positive' ,'negative' 'neutral']},
|
105 |
+
hole= 0.5)
|
106 |
+
|
107 |
+
class_4_fig = px.pie(data_frame= group_df[group_df['clean_AS'] == 1],
|
108 |
+
names = group_df[group_df['clean_AS'] == 1].sentiment,
|
109 |
+
color = 'sentiment',
|
110 |
+
color_discrete_map = color_map,
|
111 |
+
category_orders = {"sentiment": ['positive' ,'negative' 'neutral']},
|
112 |
+
hole= 0.5)
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
kpi1, kpi2, kpi3 = st.columns(3)
|
118 |
+
|
119 |
+
with kpi1:
|
120 |
+
st.markdown("**All reviewsf**")
|
121 |
+
st.plotly_chart(pie_fig, use_container_width=True)
|
122 |
+
|
123 |
+
|
124 |
+
with kpi2:
|
125 |
+
|
126 |
+
with st.expander("Sentiment Count"):
|
127 |
+
st.dataframe(data=group_df['sentiment'].value_counts().rename_axis('unique_values').reset_index(name='counts'),
|
128 |
+
use_container_width=True)
|
129 |
+
st.plotly_chart(heatmap_fig, use_container_width=True)
|
130 |
+
|
131 |
+
with kpi3:
|
132 |
+
st.markdown("Looking at the how many topics each review is talking about")
|
133 |
+
st.plotly_chart(bar_fig, use_container_width=True)
|
134 |
+
|
135 |
+
|
136 |
+
st.markdown("<hr/>",unsafe_allow_html=True)
|
137 |
+
|
138 |
+
|
139 |
+
st.markdown("## Distribution broken down into 4 super classes")
|
140 |
+
|
141 |
+
class1, class2, class3, class4 = st.columns(4)
|
142 |
+
|
143 |
+
with class1:
|
144 |
+
st.markdown("#### Buying Experience")
|
145 |
+
st.plotly_chart(class_1_fig, use_container_width=True)
|
146 |
+
|
147 |
+
with class2:
|
148 |
+
st.markdown("#### Product")
|
149 |
+
st.plotly_chart(class_2_fig, use_container_width=True)
|
150 |
+
|
151 |
+
with class3:
|
152 |
+
st.markdown("#### Delivery Mode")
|
153 |
+
st.plotly_chart(class_3_fig, use_container_width=True)
|
154 |
+
|
155 |
+
with class4:
|
156 |
+
st.markdown("#### After Sales")
|
157 |
+
st.plotly_chart(class_4_fig, use_container_width=True)
|