Shraddha Gami commited on
Commit
a57df44
·
1 Parent(s): 8e5b51a

Add Frequently bought together streamlit app

Browse files
app.py CHANGED
@@ -1,4 +1,67 @@
 
1
  import streamlit as st
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
  import streamlit as st
3
+ from html_information import html
4
 
5
+
6
+ def read_pickle_files(pickle_file):
7
+ with open(pickle_file, 'rb') as f:
8
+ return pickle.load(f)
9
+
10
+
11
+ def streamlit_carousel(header_name: str, rec_item_url: list,
12
+ rec_item_name: list) -> None:
13
+ st.header(header_name)
14
+ if header_name == "Up-Sell Recommendations" or header_name == "Cross-Sell Recommendations":
15
+ st.write("Based on Products added to the Cart")
16
+ mid_section = ""
17
+ for index, value in enumerate(rec_item_url):
18
+ mid_section += """<div class="item"><div id="image-container"><img src='""" + str(value) + """' /></div><p>""" + str(rec_item_name[index]) + """</p></div>"""
19
+
20
+ mid_html = html + mid_section + """</div></div></body>"""
21
+ st.markdown(mid_html, unsafe_allow_html=True)
22
+
23
+ def get_mapped_values(uid_list, dict_map):
24
+ res = []
25
+ for val in uid_list:
26
+ try:
27
+ res.append(dict_map[val])
28
+ except:
29
+ continue
30
+ return res
31
+
32
+ def carousel_wrapper(rec_list, query_id, uid_media_map, uid_name_map, header_name, exception_header_name):
33
+ for val in rec_list:
34
+ if val["product_id"] == query_id:
35
+ text_rec_list = val["recommendations"]
36
+ if text_rec_list:
37
+ text_rec_url = []
38
+ text_rec_name = []
39
+ for val in text_rec_list:
40
+ text_rec_url.append(uid_media_map[val["product_id"]])
41
+ text_rec_name.append(uid_name_map[val["product_id"]])
42
+ streamlit_carousel(header_name, text_rec_url, text_rec_name)
43
+ else:
44
+ st.write(exception_header_name)
45
+
46
+
47
+ filtered_products_list = read_pickle_files("filtered_products_list.pkl")
48
+ recommendation_products_list = read_pickle_files("recommendation_products_list.pkl")
49
+
50
+ product_dict = {item['product_id']: item for item in filtered_products_list}
51
+
52
+ uid_name_map = read_pickle_files("uid_name_map.pkl")
53
+ uid_url_map = read_pickle_files("uid_url_map.pkl")
54
+
55
+
56
+ st.set_page_config(page_title="My App", page_icon=":guardsman:", layout="wide", initial_sidebar_state="auto")
57
+ st.header("Frequently Bought Together Recommendations")
58
+ uid_list = list(product_dict)
59
+ uid_name_list = get_mapped_values(uid_list, uid_name_map)
60
+ st.subheader("Choose a Product")
61
+ index = st.selectbox("Product List", range(len(uid_name_list)), format_func=lambda x: uid_name_list[x])
62
+ query_id = uid_list[index]
63
+ query_url = uid_url_map[query_id]
64
+ st.image(query_url, width=200)
65
+
66
+
67
+ carousel_wrapper(recommendation_products_list, query_id, uid_url_map, uid_name_map, "Frequently bought together items", "No BT recommendations found")
filtered_products_list.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c43b0ccf5edb9b29730a864e4ee419447ecc6d9b6454ea91a1308a6016251da
3
+ size 135198
html_information.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ html = """
2
+ <style>
3
+ body{
4
+ font-family: sans-serif;
5
+ }
6
+ h1{
7
+ font-weight: 100;
8
+ }
9
+ .container{
10
+ width: 100%;
11
+ display:block;
12
+ overflow:hidden;
13
+ }
14
+ .carousel{
15
+ display:block;
16
+ width: 100%;
17
+ height: 320px;
18
+ background: white;
19
+ overflow-x: scroll;
20
+ padding: 10px;
21
+ margin: 0;
22
+ white-space: nowrap;
23
+ border-top: 2px solid rgba(0, 0, 0, 0.1);
24
+ border-bottom: 2px solid rgba(0, 0, 0, 0.1);
25
+ }
26
+ .item {
27
+ display: inline-block;
28
+ overflow: hidden;
29
+ width: 250px;
30
+ margin: 0 10px;
31
+ height: calc(100%);
32
+ background: rgba(0, 0, 0, 0.05) no-repeat center center;
33
+ background-size: cover;
34
+ position:relative;
35
+ border-radius: 20px;
36
+ box-shadow: 0 0 10px #dfdfdf;
37
+ }
38
+ .item p {
39
+ padding: 20px;
40
+ word-break: break-all;
41
+ white-space: break-spaces;
42
+ overflow: hidden;
43
+ display: -webkit-box;
44
+ -webkit-line-clamp: 4;
45
+ -webkit-box-orient: vertical;
46
+ margin: 0;
47
+ align: center;
48
+ }
49
+ #image-container{
50
+ width: 100%;
51
+ height: 60%;
52
+ text-align:center;
53
+ font-size: 9em;
54
+ color: white;
55
+ overflow: hidden;
56
+ }
57
+ #image-container img{
58
+ width: 100%;
59
+ height: 100%;
60
+ object-fit: contain;
61
+ }
62
+
63
+ </style>
64
+ </head>
65
+ <body>
66
+ <div class="container">
67
+ <div class="carousel">
68
+
69
+ """
recommendation_products_list.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1d247deb860fbdcd24a13d63b30c56b937658e0dabfbcd592e67e22de0ec40f4
3
+ size 146662
uid_name_map.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:897e0af2023a81ec6720d9bff75195f3d775e955b577b2508d5391e9bedea787
3
+ size 943869
uid_url_map.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d6c7ba7fb131e191bd0aea75aaf80ed22d6916c359bcd8b2aed3f40096190ff4
3
+ size 2905809