-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
297 lines (222 loc) · 10 KB
/
app.py
File metadata and controls
297 lines (222 loc) · 10 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
import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
st.set_page_config(
page_title="eCommerce Rercommender", # => The title
page_icon="🛒",
layout="wide", # wide
initial_sidebar_state="auto") # collapsed
##########################################
## Load and Prep Data ##
##########################################
@st.cache
def load_data():
df = pd.read_csv('top25.csv')
return df
df = load_data()
brands=['Samsung', 'Apple', 'Huawei', 'LG', 'Lenovo']
cols = ['product_id','category_code','brand','price', 'price_category']
##########################################
## Style and Formatting ##
##########################################
hide_table_row_index = """
<style>
thead tr th:first-child {display:none}
tbody th {display:none}
</style> """
center_heading_text = """
<style>
.col_heading {text-align: center !important}
</style> """
center_row_text = """
<style>
td {text-align: center !important}
</style> """
# Inject CSS with Markdown
st.markdown(hide_table_row_index, unsafe_allow_html=True)
st.markdown(center_heading_text, unsafe_allow_html=True)
st.markdown(center_row_text, unsafe_allow_html=True)
brands=['Samsung', 'Apple', 'Huawei', 'LG', 'Lenovo']
cols = ['product_id','category_code','brand','price', 'price_category']
heading_properties = [('font-size', '16px'),('text-align', 'center'),
('color', 'white'), ('font-weight', 'bold'),
('background', 'gray'),('border', '1.2px solid black')]
cell_properties = [('font-size', '16px'),('text-align', 'center')]
# definite the styler function
dfstyle = [{"selector": "th", "props": heading_properties},
{"selector": "td", "props": cell_properties}]
def make_pretty(styler):
styler.set_properties(**{'background': 'mistyrose', 'border': '1.2px solid'})
styler.hide(axis='index')
styler.set_table_styles(dfstyle)
styler.format(precision=2)
return styler
##########################################
## Title, Tabs, and Sidebar ##
##########################################
st.title("Let's go shopping!")
st.markdown('''##### <span style="color:gray">Predict consumer preference and recommend related products</span>
''', unsafe_allow_html=True)
cols_1, cols_2, cols_3 = st.sidebar.columns([1,8,1])
with cols_1:
st.write("")
with cols_2:
st.image('data_streamlit/ecommerce.png', use_column_width=True)
with cols_3:
st.write("")
st.sidebar.markdown(" ## About eCommerce Recommender")
st.sidebar.markdown("1️⃣ model based on cusumer preference places 16k+ products into 3000+ brands buckets.")
st.sidebar.markdown('2️⃣ model finds the feature based similar produts to each single product.')
st.sidebar.markdown('3️⃣ model predicts the most suitable corssed products of each single.')
st.sidebar.info("Read more about how the model works and see the code on our [Github](https://github.com/sailormoonvicky/eCommerce).", icon="ℹ️")
col1, col2,col3, col4, col5 = st.columns(5)
tab_start, tab_samsung, tab_apple, tab_huawei, tab_lg, tab_lenovo = st.tabs(['Start', 'Samsung', 'Apple', 'Huawei', 'LG', 'Lenovo'])
with col1:
image = Image.open('data_streamlit/{}.png'.format(brands[0]))
st.image(image, caption='{}'.format(brands[0]))
with col2:
image = Image.open('data_streamlit/{}.png'.format(brands[1]))
st.image(image, caption='{}'.format(brands[1]))
with col3:
image = Image.open('data_streamlit/{}.png'.format(brands[2]))
st.image(image, caption='{}'.format(brands[2]))
with col4:
image = Image.open('data_streamlit/{}.png'.format(brands[3]))
st.image(image, caption='{}'.format(brands[3]))
with col5:
image = Image.open('data_streamlit/{}.png'.format(brands[4]))
st.image(image, caption='{}'.format(brands[4]))
with tab_start:
st.write('Welcome!')
st.markdown('Contributors: Christian Jergen,Héléna Antoniadis, Zhenghan Hu')
st.markdown('Supervisors: Julio Quintana, Lorcan Rae')
##########################################
## Tab ##
##########################################
def expand_brand(i):
expand_brand = st.expander("Best sellers of {}".format(brands[i]), expanded=False)
with expand_brand:
product = st.selectbox("Find the best sellers of {}:".format(brands[i]), df[df.brand==brands[i].lower()].product_id)
st.write(f'''
###### <div style="text-align: center"> According to our model, </span> </div>
###### <div style="text-align: center"> <span style="color:indianred">[{product}] </span> is one of the Best sellers of <span style="color:indianred"> {brands[i]} </span> in 2020-2021.</span> </div>
''', unsafe_allow_html=True)
col1_1,col1_2,col1_3=st.columns(3)
with col1_1:
st.write('')
with col1_2:
st.image(f'data_streamlit/{df[df.product_id==product].metadata.tolist()[0]}.png')
with col1_3:
st.write('')
styler_product = df[df.product_id == product][cols].style.pipe(make_pretty)
return st.table(styler_product)
def expand_similarity():
expand_similarity = st.expander("Similar recommender you maybe like")
with expand_similarity:
#number slider
slider1, slider2 = st.columns([4,10])
with slider1:
num_entries = st.slider('Number of recommenders to show:', 0, 10, 5, step =5)
with slider2:
st.write('')
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe like these: </span> </div>
''', unsafe_allow_html=True)
#select the recommender to check the crossed recommended pruducts
recommend_df = df[cols][:num_entries]
recommender = st.selectbox("Select the product you're interested:", recommend_df.product_id)
recommend_styler = recommend_df.style.pipe(make_pretty)
st.table(recommend_styler)
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe also like these: </span> </div>
##### <div style="text-align: center"> Crossed products recommender: </span> </div>
''', unsafe_allow_html=True)
cross_df = df[df.product_id == recommender][cols][:10]
cross_styler = cross_df.style.pipe(make_pretty)
st.table(cross_styler)
with tab_samsung:
expand_brand(0)
###################
expand_similarity()
###################
with tab_apple:
expand_brand(1)
###################
expand_similarity = st.expander("Similar recommender you maybe like")
with expand_similarity:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe like these: </span> </div>
''', unsafe_allow_html=True)
recommend_df = df[cols][:10]
recommend_styler = recommend_df.style.pipe(make_pretty)
st.table(recommend_styler)
###################
expand_cross = st.expander("Crossed products recommender")
with expand_cross:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe also like these: </span> </div>
''', unsafe_allow_html=True)
cross_df = df[cols][10:30]
cross_styler = cross_df.style.pipe(make_pretty)
st.table(cross_styler)
with tab_huawei:
expand_brand(2)
###################
expand_similarity = st.expander("Similar recommender you maybe like")
with expand_similarity:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe like these: </span> </div>
''', unsafe_allow_html=True)
recommend_df = df[cols][:10]
recommend_styler = recommend_df.style.pipe(make_pretty)
st.table(recommend_styler)
###################
expand_cross = st.expander("Crossed products recommender")
with expand_cross:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe also like these: </span> </div>
''', unsafe_allow_html=True)
cross_df = df[cols][10:30]
cross_styler = cross_df.style.pipe(make_pretty)
st.table(cross_styler)
with tab_lg:
expand_brand(3)
###################
expand_similarity = st.expander("Similar recommender you maybe like")
with expand_similarity:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe like these: </span> </div>
''', unsafe_allow_html=True)
recommend_df = df[cols][:10]
recommend_styler = recommend_df.style.pipe(make_pretty)
st.table(recommend_styler)
###################
expand_cross = st.expander("Crossed products recommender")
with expand_cross:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe also like these: </span> </div>
''', unsafe_allow_html=True)
cross_df = df[cols][10:30]
cross_styler = cross_df.style.pipe(make_pretty)
st.table(cross_styler)
with tab_lenovo:
expand_brand(4)
###################
expand_similarity = st.expander("Similar recommender you maybe like")
with expand_similarity:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe like these: </span> </div>
''', unsafe_allow_html=True)
recommend_df = df[cols][:10]
recommend_styler = recommend_df.style.pipe(make_pretty)
st.table(recommend_styler)
###################
expand_cross = st.expander("Crossed products recommender")
with expand_cross:
st.write(f'''
##### <div style="text-align: center"> According to our model, you maybe also like these: </span> </div>
''', unsafe_allow_html=True)
cross_df = df[cols][10:30]
cross_styler = cross_df.style.pipe(make_pretty)
st.table(cross_styler)