Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,8 @@ def read_pickle_files(pickle_file):
|
|
| 9 |
# Load the necessary pickle files
|
| 10 |
cross_sell_data = read_pickle_files("fynd.cross_sell_recommendations-000000000000000000000001s.pkl")
|
| 11 |
upsell_data = read_pickle_files("fynd.up_sell_recommendations_000000000000000000000002s.pkl")
|
| 12 |
-
catalog_data = read_pickle_files("clickstream_data_sephora_sephora_products_1724404940894(1).pkl")
|
| 13 |
uid_name_pairs = read_pickle_files("uid_name_pairs.pkl")
|
|
|
|
| 14 |
|
| 15 |
# Create a mapping from product_id to product name for dropdown
|
| 16 |
product_name_to_id = {name: uid for name, uid in uid_name_pairs.items()}
|
|
@@ -18,16 +18,7 @@ product_name_to_id = {name: uid for name, uid in uid_name_pairs.items()}
|
|
| 18 |
# Create a reverse mapping from product_id to product_name for display purposes
|
| 19 |
product_id_to_name = {uid: name for name, uid in uid_name_pairs.items()}
|
| 20 |
|
| 21 |
-
# Function to
|
| 22 |
-
def get_product_image_url(product_id, catalog_data):
|
| 23 |
-
for product in catalog_data:
|
| 24 |
-
if product['uid'] == product_id:
|
| 25 |
-
medias = product.get('medias', [])
|
| 26 |
-
if medias and 'url' in medias[0]:
|
| 27 |
-
return medias[0]['url']
|
| 28 |
-
return None
|
| 29 |
-
|
| 30 |
-
# Extract product names from recommendation data
|
| 31 |
def extract_product_list(recommendation_data):
|
| 32 |
product_ids = [entry['product_id'] for entry in recommendation_data]
|
| 33 |
# Map the product IDs to names for the dropdown
|
|
@@ -43,7 +34,7 @@ def get_recommendations(product_id, recommendation_data):
|
|
| 43 |
# Streamlit App Layout
|
| 44 |
st.title("Product Recommendations")
|
| 45 |
|
| 46 |
-
# Dropdown for selecting type
|
| 47 |
recommendation_type = st.selectbox("Select recommendation type:", ["Cross-sell", "Up-sell"])
|
| 48 |
|
| 49 |
# Choose the appropriate data based on recommendation type
|
|
@@ -61,6 +52,15 @@ selected_product_name = st.selectbox("Select a product:", product_list)
|
|
| 61 |
# Get the selected product's ID using the name
|
| 62 |
selected_product_id = product_name_to_id.get(selected_product_name)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# Display recommendations for the selected product
|
| 65 |
if selected_product_id:
|
| 66 |
recommendations = get_recommendations(selected_product_id, recommendations_data)
|
|
@@ -69,10 +69,14 @@ if selected_product_id:
|
|
| 69 |
st.subheader(f"Recommendations for {selected_product_name}")
|
| 70 |
for recommendation in recommendations:
|
| 71 |
product_name = recommendation.get('product_name')
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
st.write(f"{product_name}")
|
| 77 |
else:
|
| 78 |
st.write("No recommendations found for this product.")
|
|
|
|
| 9 |
# Load the necessary pickle files
|
| 10 |
cross_sell_data = read_pickle_files("fynd.cross_sell_recommendations-000000000000000000000001s.pkl")
|
| 11 |
upsell_data = read_pickle_files("fynd.up_sell_recommendations_000000000000000000000002s.pkl")
|
|
|
|
| 12 |
uid_name_pairs = read_pickle_files("uid_name_pairs.pkl")
|
| 13 |
+
uid_image_html_pairs = read_pickle_files("uid_image_html_pairs.pkl")
|
| 14 |
|
| 15 |
# Create a mapping from product_id to product name for dropdown
|
| 16 |
product_name_to_id = {name: uid for name, uid in uid_name_pairs.items()}
|
|
|
|
| 18 |
# Create a reverse mapping from product_id to product_name for display purposes
|
| 19 |
product_id_to_name = {uid: name for name, uid in uid_name_pairs.items()}
|
| 20 |
|
| 21 |
+
# Function to extract product list from recommendation data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def extract_product_list(recommendation_data):
|
| 23 |
product_ids = [entry['product_id'] for entry in recommendation_data]
|
| 24 |
# Map the product IDs to names for the dropdown
|
|
|
|
| 34 |
# Streamlit App Layout
|
| 35 |
st.title("Product Recommendations")
|
| 36 |
|
| 37 |
+
# Dropdown for selecting recommendation type
|
| 38 |
recommendation_type = st.selectbox("Select recommendation type:", ["Cross-sell", "Up-sell"])
|
| 39 |
|
| 40 |
# Choose the appropriate data based on recommendation type
|
|
|
|
| 52 |
# Get the selected product's ID using the name
|
| 53 |
selected_product_id = product_name_to_id.get(selected_product_name)
|
| 54 |
|
| 55 |
+
# Display the image of the selected product using the image URL
|
| 56 |
+
if selected_product_id:
|
| 57 |
+
st.subheader(f"Selected Product: {selected_product_name}")
|
| 58 |
+
|
| 59 |
+
# Check if the product's ID has an associated image HTML and use the image URL
|
| 60 |
+
if selected_product_id in uid_image_html_pairs:
|
| 61 |
+
image_url = uid_image_html_pairs[selected_product_id]
|
| 62 |
+
st.image(image_url, caption=selected_product_name, use_column_width=False, width=150) # Set width to make image smaller
|
| 63 |
+
|
| 64 |
# Display recommendations for the selected product
|
| 65 |
if selected_product_id:
|
| 66 |
recommendations = get_recommendations(selected_product_id, recommendations_data)
|
|
|
|
| 69 |
st.subheader(f"Recommendations for {selected_product_name}")
|
| 70 |
for recommendation in recommendations:
|
| 71 |
product_name = recommendation.get('product_name')
|
| 72 |
+
recommended_product_id = recommendation.get('product_id')
|
| 73 |
+
|
| 74 |
+
# Display the image of each recommended product using the image URL
|
| 75 |
+
if recommended_product_id in uid_image_html_pairs:
|
| 76 |
+
recommended_image_url = uid_image_html_pairs[recommended_product_id]
|
| 77 |
+
st.image(recommended_image_url, caption=product_name, use_column_width=False, width=150) # Set width to make images smaller
|
| 78 |
|
| 79 |
+
# Display the product name
|
| 80 |
+
st.write(f"Product Name: {product_name}")
|
|
|
|
| 81 |
else:
|
| 82 |
st.write("No recommendations found for this product.")
|