-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
100 lines (84 loc) · 3.85 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import joblib
import streamlit as st
import pandas as pd
from pathlib import Path
# Define the parent directory path
PARENT_PATH = Path(__file__).resolve().parent
# Load datasets
product_details_df = pd.read_csv(PARENT_PATH / 'dataset/product_details.csv')
product_details_df['Details'] = product_details_df['Product ID'].astype(str) + ' (' \
+ product_details_df['Category'] + ' ' \
'$' + product_details_df['Price'].astype(str) + ')'
# Load association rules
rules = joblib.load('rules.joblib')
def fetch_image(product_id):
"""Fetches the image path for a given product ID."""
image_path = f"images/{product_id}.png"
return image_path
def recommend_products(product_id, num_recommendations=10):
"""Recommends products based on a given product ID."""
related_products = rules[(rules['antecedents'] == frozenset({product_id}))]
related_products_sorted = related_products.sort_values(by='lift', ascending=False)
related_products_sorted = related_products_sorted['consequents'].apply(lambda x: list(x))
recommended_products = related_products_sorted.values[0]
for list_product in related_products_sorted:
for product in list_product:
if product not in recommended_products:
recommended_products.append(product)
if len(recommended_products) > num_recommendations:
recommended_products = recommended_products[:10]
break
return recommended_products
def recommend(product_id):
"""Generates recommendations for a given product ID."""
recommended_products = recommend_products(product_id)
product_names = []
product_images = []
for product_id in recommended_products:
product_info = product_details_df[product_details_df['Product ID'] == product_id]
image = fetch_image(product_info['Product ID'].values[0])
product_names.append(product_info['Category'].values[0])
product_images.append(image)
return product_names, product_images
def main():
"""Main function to run the Streamlit app."""
st.header('Product Recommender System')
product_list = product_details_df['Details'].values
selected_product = st.selectbox(
"Type or select a movie from the dropdown",
product_list
)
if st.button('Show Recommendation'):
selected_product_id = int(selected_product.split(' ')[0])
recommended_product_names, recommended_product_images = recommend(selected_product_id)
# Display text explaining user choice
st.write(f"You choose this product")
# Display example result image
left_co, cent_co,last_co = st.columns(3)
with cent_co:
st.image(fetch_image(selected_product_id), caption=f"Product ID {selected_product_id}")
# Display text explaining user choice
st.write(f"You may like these product")
# Display recommended products
col1, col2, col3 = st.columns(3)
with col1:
st.text(recommended_product_names[0])
st.image(recommended_product_images[0])
with col2:
st.text(recommended_product_names[1])
st.image(recommended_product_images[1])
with col3:
st.text(recommended_product_names[2])
st.image(recommended_product_images[2])
col4, col5, col6 = st.columns(3)
with col4:
st.text(recommended_product_names[3])
st.image(recommended_product_images[3])
with col5:
st.text(recommended_product_names[4])
st.image(recommended_product_images[4])
with col6:
st.text(recommended_product_names[5])
st.image(recommended_product_images[5])
if __name__ == "__main__":
main()