import pickle
import streamlit as st
from html_information2 import html2
st.set_page_config(page_title="My App", page_icon=":money_with_wings:", layout="wide", initial_sidebar_state="auto")
st.header("Frequently Bought Together Recommendations")
def read_pickle_files(pickle_file):
with open(pickle_file, 'rb') as f:
return pickle.load(f)
# Load sephora pickle files
corrected_fp_growth_results_sephora = read_pickle_files("sephora_corrected_fp_growth_results_cleaned.pkl")
all_products_with_names_sephora = read_pickle_files ("item_catalog.pkl")
dictionary_of_transactions_sephora = read_pickle_files("transaction_metadata.pkl")
images_sephora = read_pickle_files("uid_url_map.pkl")
item_costs_sephora_data = read_pickle_files("wavg_item_costs_sephora.pkl")
# Load digital pickle files
corrected_fp_growth_results = read_pickle_files("corrected_fp_growth_results.pkl")
all_products_with_names = read_pickle_files ("all_products_with_names.pkl")
dictionary_of_transactions = read_pickle_files("reliance_digital_transactions.pkl")
item_costs_digital_data = read_pickle_files("avg_item_costs_reliance_digital_wa.pkl")
# Dropdown for selecting the dataset
dataset_choice = st.selectbox(
"Select A Dataset",
["Sephora Order Complete Dataset", "Reliance Digital Order Complete Dataset"]
)
if dataset_choice == "Sephora Order Complete Dataset":
list_of_products_to_display = {}
for itemid in (list(all_products_with_names_sephora.keys())): #for items in the total list of items in all transactions
if itemid in (list(corrected_fp_growth_results_sephora.keys())): #if the item has a result from fp-growth
list_of_products_to_display[itemid]= all_products_with_names_sephora[itemid] #show it in the drop down menu
name_to_id = {name: product_id for product_id, name in list_of_products_to_display.items()} # Reverse the dictionary to map product names to IDs
selected_product_name = st.selectbox('Select A Product:', list(name_to_id.keys())) # Create a dropdown menu with the product names
# Get the corresponding product_id
query_id = name_to_id[selected_product_name]
url = "https://www.sephora.com/"
st.write("Created using the clickstream order-completed and catalog data from [Sephora.com](%s)" % url)
st.write("Cost of chosen item:", str(round(item_costs_sephora_data[query_id], 2)))
query_url = images_sephora[int(query_id)]
st.image(query_url, width=500)
if dataset_choice == "Reliance Digital Order Complete Dataset":
list_of_products_to_display = {}
for itemid in (list(all_products_with_names.keys())): #for items in the total list of items in all transactions
if itemid[0:3]!="600":
if itemid in (list(corrected_fp_growth_results.keys())): #if the item has a result from fp-growth
list_of_products_to_display[itemid]= all_products_with_names[itemid] #show it in the drop down menu
name_to_id = {name: product_id for product_id, name in list_of_products_to_display.items()} # Reverse the dictionary to map product names to IDs
selected_product_name = st.selectbox('Select A Product:', list(name_to_id.keys())) # Create a dropdown menu with the product names
# Get the corresponding product_id
query_id = name_to_id[selected_product_name]
url = "https://www.reliancedigital.in/"
st.write("Created using the clickstream order-completed data from [reliancedigital.com](%s)" % url)
st.write("An item is only recommended if it costs less than the chosen item.")
st.write("Cost of chosen item:", str(round(item_costs_digital_data[query_id], 2)))
# Inject custom CSS for the tab headings
st.markdown(
"""
""",
unsafe_allow_html=True
)
tab2, tab3 = st.tabs(["Frequently Bought Together Demo", "Historical Order Data"])
with tab2:
if dataset_choice == "Sephora Order Complete Dataset":
if query_id in corrected_fp_growth_results_sephora:
# Separate the sorted items into IDs and counts
item_ids = [item for item in corrected_fp_growth_results_sephora[query_id]]
item_counts = [corrected_fp_growth_results_sephora[query_id][item] for item in corrected_fp_growth_results_sephora[query_id]]
item_costs_sephora = [item_costs_sephora_data.get(item, "cost missing") for item in corrected_fp_growth_results_sephora[query_id]]
item_image = []
for item in corrected_fp_growth_results_sephora[query_id]:
try:
# Attempt to retrieve the image URL
image_url = images_sephora[int(item)]
if not image_url: # If the URL is empty or None, skip it
raise ValueError("Empty image URL")
item_image.append(image_url)
except (KeyError, ValueError, TypeError):
# Handle missing or invalid image URLs by appending a default image URL
item_image.append("default_image_url")
confidence_list = []
transactions_list_sephora = []
for i in dictionary_of_transactions_sephora:
transactions_list_sephora.append(i["transaction"])
transactions_with_query_item = len([transaction for transaction in transactions_list_sephora if int(query_id) in transaction])
copurchase_count = []
# Generate a list of product names to display
product_names = []
for each_item in corrected_fp_growth_results_sephora[query_id]:
product_names.append(all_products_with_names_sephora.get(each_item, "Unknown Product"))
for recommended_item in item_ids:
transactions_with_item_and_query_item = []
transactions_with_item_and_query_item.extend([transaction for transaction in transactions_list_sephora if (int(recommended_item) in transaction) and (int(query_id) in transaction)])
number_of_transactions_with_recommended_item_and_query_item = len(transactions_with_item_and_query_item)
copurchase_count.append(number_of_transactions_with_recommended_item_and_query_item)
confidence_list.append(number_of_transactions_with_recommended_item_and_query_item / transactions_with_query_item)
mid_section = ""
for index, value in enumerate(product_names):
count_info = f"Co-purchased {copurchase_count[index]}/{transactions_with_query_item} times"
item_counts_info = f"item-count {item_counts[index]} "
confidence_info = f"Confidence: {round(confidence_list[index], 3)}"
item_cost_info_sephora = f"Cost: {round(item_costs_sephora[index],2)}"
# Use
to display each line separately
mid_section += f"""
{str(product_names[index])}
{count_info}
{confidence_info}
{item_cost_info_sephora}