sachithcheruvaturfynd commited on
Commit
1283880
·
verified ·
1 Parent(s): 6801e2f

Upload 9 files

Browse files
README.md CHANGED
@@ -1,12 +1,10 @@
1
  ---
2
- title: Frequently Bought Together 2
3
- emoji: 💻
4
  colorFrom: red
5
- colorTo: purple
6
  sdk: streamlit
7
- sdk_version: 1.38.0
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: README
3
+ emoji: 🐠
4
  colorFrom: red
5
+ colorTo: blue
6
  sdk: streamlit
 
 
7
  pinned: false
8
  ---
9
 
10
+ Edit this `README.md` markdown file to author your organization card.
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import streamlit as st
3
+ from html_information import html
4
+
5
+ def read_pickle_files(pickle_file):
6
+ with open(pickle_file, 'rb') as f:
7
+ return pickle.load(f)
8
+
9
+ def streamlit_carousel(header_name: str, rec_item_url: list,
10
+ rec_item_name: list, rec_item_count: list = None) -> None:
11
+ st.header(header_name)
12
+ if header_name in ["Up-Sell Recommendations", "Cross-Sell Recommendations"]:
13
+ st.write("Based on Products added to the Cart")
14
+ mid_section = ""
15
+ for index, value in enumerate(rec_item_url):
16
+ count_info = f" - Purchased {rec_item_count[index]} times" if rec_item_count else ""
17
+ mid_section += f"""<div class="item"><div id="image-container"><img src='{str(value)}' /></div><p>{str(rec_item_name[index])}{count_info}</p></div>"""
18
+
19
+ mid_html = html + mid_section + """</div></div></body>"""
20
+ st.markdown(mid_html, unsafe_allow_html=True)
21
+
22
+ def get_mapped_values(uid_list, dict_map):
23
+ res = []
24
+ for val in uid_list:
25
+ try:
26
+ res.append(dict_map[val])
27
+ except:
28
+ continue
29
+ return res
30
+
31
+ def carousel_wrapper(rec_list, query_id, uid_media_map, uid_name_map, header_name, exception_header_name):
32
+ for val in rec_list:
33
+ if val["product_id"] == query_id:
34
+ text_rec_list = val["recommendations"]
35
+ if text_rec_list:
36
+ text_rec_url = []
37
+ text_rec_name = []
38
+ for val in text_rec_list:
39
+ text_rec_url.append(uid_media_map[val["product_id"]])
40
+ text_rec_name.append(uid_name_map[val["product_id"]])
41
+ streamlit_carousel(header_name, text_rec_url, text_rec_name)
42
+ else:
43
+ st.write(exception_header_name)
44
+
45
+ # New function to display example transactions
46
+ # def display_example_transactions(transactions_list, query_id, uid_media_map, uid_name_map):
47
+ # example_transactions = [transaction for transaction in transactions_list if query_id in transaction]
48
+ # if len(example_transactions) > 15:
49
+ # example_transactions = example_transactions[:15]
50
+
51
+ # transaction_items = []
52
+ # for transaction in example_transactions:
53
+ # transaction_items.extend([item for item in transaction if item != query_id])
54
+
55
+ # transaction_urls = get_mapped_values(transaction_items, uid_media_map)
56
+ # transaction_names = get_mapped_values(transaction_items, uid_name_map)
57
+
58
+ # streamlit_carousel("Example Transactions", transaction_urls, transaction_names)
59
+
60
+ def display_example_transactions(transactions_list, query_id, uid_name_map):
61
+ st.header("Example Transactions")
62
+
63
+ # Filter transactions that contain the selected product
64
+ example_transactions = [transaction for transaction in transactions_list if query_id in transaction]
65
+ if len(example_transactions) > 10:
66
+ example_transactions = example_transactions[:10]
67
+
68
+ if example_transactions:
69
+ for i, transaction in enumerate(example_transactions):
70
+ transaction_names = get_mapped_values([item for item in transaction if item != query_id], uid_name_map)
71
+ formatted_transaction = "<br>".join(transaction_names) # Join with HTML line breaks
72
+ st.markdown(f"**Transaction {i+1}:**<br>{formatted_transaction}", unsafe_allow_html=True)
73
+ else:
74
+ st.write("No transactions found for this item.")
75
+
76
+
77
+
78
+ # New function to display frequently bought together items with count
79
+ def display_frequently_bought_together(bought_together_dict, query_id, uid_media_map, uid_name_map):
80
+ if query_id in bought_together_dict:
81
+ bought_together_items = bought_together_dict[query_id]
82
+
83
+ # Sort the items based on the count in descending order
84
+ sorted_items = sorted(bought_together_items.items(), key=lambda x: x[1], reverse=True)
85
+
86
+ # Separate the sorted items into IDs and counts
87
+ item_ids = [item[0] for item in sorted_items]
88
+ item_counts = [item[1] for item in sorted_items]
89
+
90
+ # Retrieve URLs and names based on the sorted item IDs
91
+ item_urls = get_mapped_values(item_ids, uid_media_map)
92
+ item_names = get_mapped_values(item_ids, uid_name_map)
93
+
94
+ # Display the carousel with sorted items
95
+ streamlit_carousel("Frequently Bought Together (with Count)", item_urls, item_names, item_counts)
96
+ else:
97
+ st.write("No frequent purchases found for this item.")
98
+
99
+
100
+ # Loading existing and new pickle files
101
+ filtered_products_list = read_pickle_files("filtered_products_list.pkl")
102
+ recommendation_products_list = read_pickle_files("recommendation_products_list.pkl")
103
+ transactions_list = read_pickle_files("transactions.pkl")
104
+ bought_together_dict = read_pickle_files("bought_together_dict_orders_data.pkl")
105
+
106
+ product_dict = {item['product_id']: item for item in filtered_products_list}
107
+ uid_name_map = read_pickle_files("uid_name_map.pkl")
108
+ uid_url_map = read_pickle_files("uid_url_map.pkl")
109
+
110
+ st.set_page_config(page_title="My App", page_icon=":guardsman:", layout="wide", initial_sidebar_state="auto")
111
+ st.header("Frequently Bought Together Recommendations")
112
+
113
+ uid_list = list(product_dict)
114
+ uid_name_list = get_mapped_values(uid_list, uid_name_map)
115
+ st.subheader("Choose a Product")
116
+ index = st.selectbox("Product List", range(len(uid_name_list)), format_func=lambda x: uid_name_list[x])
117
+ query_id = uid_list[index]
118
+ query_url = uid_url_map[query_id]
119
+ st.image(query_url, width=200)
120
+
121
+ carousel_wrapper(recommendation_products_list, query_id, uid_url_map, uid_name_map, "Frequently bought together items", "No BT recommendations found")
122
+
123
+ # Display frequently bought together items with count carousel
124
+ display_frequently_bought_together(bought_together_dict, query_id, uid_url_map, uid_name_map)
125
+
126
+ # Display example transactions carousel
127
+ # display_example_transactions(transactions_list, query_id, uid_url_map, uid_name_map)
128
+ display_example_transactions(transactions_list, query_id, uid_name_map)
129
+
130
+
131
+
bought_together_dict_orders_data.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba09baa0fb07d318cf1cade7a392eb4441cfc8abd36b31e4fc31867577156fea
3
+ size 72032
filtered_products_list.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d12bc11ffdd732b0045865d5039aaf4d1134309a6da437637907911b92a9e388
3
+ size 569187
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:d12bc11ffdd732b0045865d5039aaf4d1134309a6da437637907911b92a9e388
3
+ size 569187
transactions.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b65fd3738b532c2eaf962a8a4abf0118b58897ae1aaaec236f42a77c6fc2e979
3
+ size 277180
uid_name_map.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f69ddda1fd27e6713dd98d8494b31ffb593a798f81eb859bbfa6bceb36139aaa
3
+ size 769524
uid_url_map.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:99b5da65445ac38124ffb2cdfddf7a37a8a4a4a848d689195a3899f91a708b5a
3
+ size 2880289