Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pickle
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Function to load pickle files
|
| 5 |
def read_pickle_files(pickle_file):
|
|
@@ -14,8 +100,6 @@ 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()}
|
| 17 |
-
|
| 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
|
|
@@ -32,7 +116,7 @@ def get_recommendations(product_id, recommendation_data):
|
|
| 32 |
return []
|
| 33 |
|
| 34 |
# Streamlit App Layout
|
| 35 |
-
st.title("
|
| 36 |
|
| 37 |
# Dropdown for selecting recommendation type
|
| 38 |
recommendation_type = st.selectbox("Select recommendation type:", ["Cross-sell", "Up-sell"])
|
|
@@ -54,19 +138,26 @@ 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,
|
| 63 |
|
| 64 |
# Display recommendations for the selected product
|
| 65 |
if selected_product_id:
|
| 66 |
recommendations = get_recommendations(selected_product_id, recommendations_data)
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if recommendations:
|
| 69 |
-
st.subheader(f"Recommendations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
for recommendation in recommendations:
|
| 71 |
product_name = recommendation.get('product_name')
|
| 72 |
recommended_product_id = recommendation.get('product_id')
|
|
@@ -74,9 +165,24 @@ if selected_product_id:
|
|
| 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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import pickle
|
| 3 |
+
|
| 4 |
+
# # Function to load pickle files
|
| 5 |
+
# def read_pickle_files(pickle_file):
|
| 6 |
+
# with open(pickle_file, 'rb') as f:
|
| 7 |
+
# return pickle.load(f)
|
| 8 |
+
|
| 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()}
|
| 17 |
+
|
| 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
|
| 25 |
+
# return [product_id_to_name[product_id] for product_id in product_ids if product_id in product_id_to_name]
|
| 26 |
+
|
| 27 |
+
# # Extract recommendations for a specific product_id
|
| 28 |
+
# def get_recommendations(product_id, recommendation_data):
|
| 29 |
+
# for product in recommendation_data:
|
| 30 |
+
# if product['product_id'] == product_id:
|
| 31 |
+
# return product['recommendations']
|
| 32 |
+
# return []
|
| 33 |
+
|
| 34 |
+
# # Streamlit App Layout
|
| 35 |
+
# st.title("Cross-Sell & Up-Sell 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
|
| 41 |
+
# if recommendation_type == "Cross-sell":
|
| 42 |
+
# recommendations_data = cross_sell_data
|
| 43 |
+
# elif recommendation_type == "Up-sell":
|
| 44 |
+
# recommendations_data = upsell_data
|
| 45 |
+
|
| 46 |
+
# # Get the list of product names for the dropdown
|
| 47 |
+
# product_list = extract_product_list(recommendations_data)
|
| 48 |
+
|
| 49 |
+
# # Dropdown for selecting a product by name
|
| 50 |
+
# selected_product_name = st.selectbox("Select a product:", product_list)
|
| 51 |
+
|
| 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, use_column_width=False, width=200) # 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)
|
| 67 |
+
|
| 68 |
+
# if recommendations:
|
| 69 |
+
# #st.subheader(f"Recommendations:")
|
| 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.")
|
| 83 |
+
|
| 84 |
import streamlit as st
|
| 85 |
import pickle
|
| 86 |
+
from html_information2 import html2
|
| 87 |
+
|
| 88 |
+
st.set_page_config(layout="wide")
|
| 89 |
|
| 90 |
# Function to load pickle files
|
| 91 |
def read_pickle_files(pickle_file):
|
|
|
|
| 100 |
|
| 101 |
# Create a mapping from product_id to product name for dropdown
|
| 102 |
product_name_to_id = {name: uid for name, uid in uid_name_pairs.items()}
|
|
|
|
|
|
|
| 103 |
product_id_to_name = {uid: name for name, uid in uid_name_pairs.items()}
|
| 104 |
|
| 105 |
# Function to extract product list from recommendation data
|
|
|
|
| 116 |
return []
|
| 117 |
|
| 118 |
# Streamlit App Layout
|
| 119 |
+
st.title("Cross-Sell & Up-Sell Recommendations")
|
| 120 |
|
| 121 |
# Dropdown for selecting recommendation type
|
| 122 |
recommendation_type = st.selectbox("Select recommendation type:", ["Cross-sell", "Up-sell"])
|
|
|
|
| 138 |
|
| 139 |
# Display the image of the selected product using the image URL
|
| 140 |
if selected_product_id:
|
| 141 |
+
#st.subheader(f"Selected Product: {selected_product_name}")
|
| 142 |
|
| 143 |
# Check if the product's ID has an associated image HTML and use the image URL
|
| 144 |
if selected_product_id in uid_image_html_pairs:
|
| 145 |
image_url = uid_image_html_pairs[selected_product_id]
|
| 146 |
+
st.image(image_url, use_column_width=False, width=450) # Set width to make image smaller
|
| 147 |
|
| 148 |
# Display recommendations for the selected product
|
| 149 |
if selected_product_id:
|
| 150 |
recommendations = get_recommendations(selected_product_id, recommendations_data)
|
| 151 |
+
reccomendation_names = []
|
| 152 |
+
reccomendation_images = []
|
| 153 |
|
| 154 |
if recommendations:
|
| 155 |
+
#st.subheader(f"Recommendations:")
|
| 156 |
+
if len(recommendations)>10:
|
| 157 |
+
recommendations= recommendations[:10]
|
| 158 |
+
else:
|
| 159 |
+
pass
|
| 160 |
+
|
| 161 |
for recommendation in recommendations:
|
| 162 |
product_name = recommendation.get('product_name')
|
| 163 |
recommended_product_id = recommendation.get('product_id')
|
|
|
|
| 165 |
# Display the image of each recommended product using the image URL
|
| 166 |
if recommended_product_id in uid_image_html_pairs:
|
| 167 |
recommended_image_url = uid_image_html_pairs[recommended_product_id]
|
| 168 |
+
#st.image(recommended_image_url, caption=product_name, use_column_width=False, width=150) # Set width to make images smaller
|
| 169 |
+
|
| 170 |
+
reccomendation_names.append(product_name)
|
| 171 |
+
reccomendation_images.append(recommended_image_url)
|
| 172 |
+
|
| 173 |
# Display the product name
|
| 174 |
+
#st.write(f"Product Name: {product_name}")
|
| 175 |
else:
|
| 176 |
st.write("No recommendations found for this product.")
|
| 177 |
+
|
| 178 |
+
mid_section = ""
|
| 179 |
+
for index, value in enumerate(reccomendation_names):
|
| 180 |
+
|
| 181 |
+
# Use <br> to display each line separately
|
| 182 |
+
mid_section += f"""<div class="item">
|
| 183 |
+
<div id="image-container"><img src='{reccomendation_images[index]}' /></div>
|
| 184 |
+
<p style="font-size: 16px; font-weight: bold; white-space: normal; word-wrap: break-word;">{str(reccomendation_names[index])}</p>
|
| 185 |
+
</div>"""
|
| 186 |
+
mid_html = html2 + mid_section + """</div></div></body>"""
|
| 187 |
+
st.markdown(mid_html, unsafe_allow_html=True)
|
| 188 |
+
|