| import pickle |
| import streamlit as st |
| from html_information import html |
|
|
|
|
| def read_pickle_files(pickle_file): |
| with open(pickle_file, 'rb') as f: |
| return pickle.load(f) |
|
|
|
|
| def streamlit_carousel(header_name: str, rec_item_url: list, |
| rec_item_name: list) -> None: |
| st.header(header_name) |
| if header_name == "Up-Sell Recommendations" or header_name == "Cross-Sell Recommendations": |
| st.write("Based on Products added to the Cart") |
| mid_section = "" |
| for index, value in enumerate(rec_item_url): |
| mid_section += """<div class="item"><div id="image-container"><img src='""" + str(value) + """' /></div><p>""" + str(rec_item_name[index]) + """</p></div>""" |
|
|
| mid_html = html + mid_section + """</div></div></body>""" |
| st.markdown(mid_html, unsafe_allow_html=True) |
|
|
| def get_mapped_values(uid_list, dict_map): |
| res = [] |
| for val in uid_list: |
| try: |
| res.append(dict_map[val]) |
| except: |
| continue |
| return res |
|
|
| def carousel_wrapper(rec_list, query_id, uid_media_map, uid_name_map, header_name, exception_header_name): |
| for val in rec_list: |
| if val["product_id"] == query_id: |
| text_rec_list = val["recommendations"] |
| if text_rec_list: |
| text_rec_url = [] |
| text_rec_name = [] |
| for val in text_rec_list: |
| text_rec_url.append(uid_media_map[val["product_id"]]) |
| text_rec_name.append(uid_name_map[val["product_id"]]) |
| streamlit_carousel(header_name, text_rec_url, text_rec_name) |
| else: |
| st.write(exception_header_name) |
|
|
|
|
| filtered_products_list = read_pickle_files("filtered_products_list.pkl") |
| recommendation_products_list = read_pickle_files("recommendation_products_list.pkl") |
|
|
| product_dict = {item['product_id']: item for item in filtered_products_list} |
|
|
| uid_name_map = read_pickle_files("uid_name_map.pkl") |
| uid_url_map = read_pickle_files("uid_url_map.pkl") |
|
|
|
|
| st.set_page_config(page_title="My App", page_icon=":guardsman:", layout="wide", initial_sidebar_state="auto") |
| st.header("Frequently Bought Together Recommendations") |
| uid_list = list(product_dict) |
| uid_name_list = get_mapped_values(uid_list, uid_name_map) |
| st.subheader("Choose a Product") |
| index = st.selectbox("Product List", range(len(uid_name_list)), format_func=lambda x: uid_name_list[x]) |
| query_id = uid_list[index] |
| query_url = uid_url_map[query_id] |
| st.image(query_url, width=200) |
|
|
|
|
| carousel_wrapper(recommendation_products_list, query_id, uid_url_map, uid_name_map, "Frequently bought together items", "No BT recommendations found") |
|
|