bt-generalize / entry.py
subhankarfynd's picture
Add initial files for Bougth Together Results
c3f3d16
raw
history blame
3.34 kB
import streamlit as st
import pickle
from html_information import html
def load_pickle_file(file_name):
with open(file_name, '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)
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, uid_map_dict):
res = []
for val in uid_list:
res.append(uid_map_dict[val])
return res
uid_name_map = load_pickle_file('generalize_uid_name_map.pkl')
uid_media_map = load_pickle_file('generalize_uid_media_map.pkl')
bt_no_color_rec = load_pickle_file('bt_no_color.pkl')
bt_color_rec = load_pickle_file('bt_color.pkl')
bt_color_no_cat_order = load_pickle_file('bt_color_no_cat_order.pkl')
st.set_page_config(page_title="My App", page_icon=":guardsman:", layout="wide", initial_sidebar_state="auto")
st.header("Bought Together Recommendations")
uid_list = list(uid_name_map)
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]
print(query_id)
print()
query_url = uid_media_map[query_id]
st.image(query_url, width=200)
for val in bt_no_color_rec:
if val["product_id"] == query_id:
text_rec_list = val["recommendations"]
print(text_rec_list)
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("BT Recommendations No Color", text_rec_url, text_rec_name)
else:
st.write("No BT recommendations found")
for val in bt_color_rec:
if val["product_id"] == query_id:
text_rec_list = val["recommendations"]
print(text_rec_list)
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("BT Recommendations Color", text_rec_url, text_rec_name)
else:
st.write("No BT recommendations found")
for val in bt_color_no_cat_order:
if val["product_id"] == query_id:
text_rec_list = val["recommendations"]
print(text_rec_list)
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("BT Recommendations Color No Category Order", text_rec_url, text_rec_name)
else:
st.write("No BT recommendations found No Category Order")