-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
384 lines (327 loc) · 14.3 KB
/
Copy pathapp.py
File metadata and controls
384 lines (327 loc) · 14.3 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import streamlit as st
import os
import random
import numpy as np
from PIL import Image
import pandas as pd
# Set page config with title, icon, and layout
st.set_page_config(
page_title="AI Product Intelligence System",
page_icon="🛍️",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom premium styling
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;700&display=swap');
/* Apply font to elements */
html, body, [class*="css"], .stMarkdown {
font-family: 'Outfit', sans-serif;
}
/* Title styling */
.main-title {
font-size: 3rem;
font-weight: 700;
background: linear-gradient(135deg, #a8a5e6, #6c5ce7, #00d2d3);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
text-align: center;
}
.sub-title {
font-size: 1.2rem;
color: #7f8c8d;
text-align: center;
margin-bottom: 2.5rem;
}
/* Custom cards */
.card {
background-color: #1e1e2f;
border: 1px solid #2d2d44;
border-radius: 16px;
padding: 20px;
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
margin-bottom: 1.5rem;
}
.product-card {
background-color: #151522;
border: 1px solid #252538;
border-radius: 12px;
padding: 12px;
text-align: center;
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
height: 100%;
}
.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(108, 92, 231, 0.25);
border-color: #6c5ce7;
}
.product-title {
font-size: 0.95rem;
font-weight: 600;
margin: 8px 0 4px 0;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
height: 40px;
}
.product-meta {
font-size: 0.8rem;
color: #888;
}
.relationship-badge {
background-color: #6c5ce7;
color: white;
font-size: 0.75rem;
font-weight: bold;
padding: 3px 8px;
border-radius: 20px;
display: inline-block;
margin-bottom: 8px;
}
/* Stat banner metrics */
.metric-box {
background: linear-gradient(135deg, #2a2a40, #1b1b2a);
border: 1px solid #3d3d5c;
border-radius: 12px;
padding: 20px;
text-align: center;
}
.metric-value {
font-size: 2.2rem;
font-weight: 700;
color: #00d2d3;
margin: 5px 0;
}
.metric-label {
font-size: 0.9rem;
color: #aaa;
text-transform: uppercase;
letter-spacing: 1px;
}
</style>
""", unsafe_allow_html=True)
# Imports from custom modules
try:
from src.utils import load_metadata, get_or_create_embeddings
from src.recommender import get_complementary_recommendations
from src.catalog import create_unique_catalog
from src.search import reverse_product_search
except ImportError as e:
st.error(f"Failed to import local modules: {e}")
st.info("Make sure the application is run from the workspace root.")
# Main Application Banner
st.markdown('<div class="main-title">Product Intelligence System</div>', unsafe_allow_html=True)
st.markdown('<div class="sub-title">Gen AI Bootcamp - Day 2 Homework Assignment</div>', unsafe_allow_html=True)
# Load dataset and cache it
@st.cache_resource
def init_system():
with st.spinner("Initializing system and loading dataset..."):
df_raw = load_metadata()
# Default sample to 3000 for responsive runtime
df, img_embs, txt_embs = get_or_create_embeddings(df_raw, sample_size=3000)
return df, img_embs, txt_embs
try:
df, img_embs, txt_embs = init_system()
except Exception as e:
st.error(f"Error initializing system: {e}")
st.info("Verify your internet connection for the initial dataset download.")
st.stop()
# Sidebar Setup
st.sidebar.image("https://img.icons8.com/clouds/200/shopping-bag.png", width=120)
st.sidebar.title("Configuration")
st.sidebar.markdown("Configure hyperparameters for recommendations, text queries, and clustering.")
# App Mode Selector
app_tab = st.sidebar.radio(
"Select Task",
["Task 1: Recommendation Engine", "Task 2: Unique Catalog", "Task 3: Reverse Search"]
)
# Sidebar helper info
st.sidebar.divider()
st.sidebar.subheader("System Information")
st.sidebar.info(f"Loaded Products: **{len(df)}**\nEmbeddings cached locally.")
# ----------------- TASK 1: SMART RECOMMENDATION ENGINE -----------------
if app_tab == "Task 1: Recommendation Engine":
st.header("Task 1: Smart Product Recommendation Engine")
st.write("Find complementary products commonly purchased together (e.g. shoes suggest socks, watches, apparel).")
st.divider()
# Let user pick an item
col_input, col_space = st.columns([2, 3])
with col_input:
subcats = sorted(df['subCategory'].unique())
selected_subcat = st.selectbox("Filter products by Subcategory", subcats, index=subcats.index("Shoes") if "Shoes" in subcats else 0)
# Filter products in subcategory
subcat_df = df[df['subCategory'] == selected_subcat]
prod_names = subcat_df['productDisplayName'].tolist()
prod_ids = subcat_df['id'].tolist()
selected_name = st.selectbox(
"Select Input Product",
prod_names,
index=0 if prod_names else 0
)
query_idx = prod_names.index(selected_name)
selected_id = prod_ids[query_idx]
st.subheader("Selected Item & Smart Complementary Recommendations")
col_prod, col_recs = st.columns([1.5, 3.5])
# Query product details
q_product = df[df['id'] == selected_id].iloc[0]
with col_prod:
st.markdown('<div class="card">', unsafe_allow_html=True)
st.write("##### Input Product Details")
st.image(q_product['image_path'], use_container_width=True)
st.write(f"**Name:** {q_product['productDisplayName']}")
st.write(f"**Category:** {q_product['masterCategory']} / {q_product['subCategory']}")
st.write(f"**Usage:** {q_product['usage']}")
st.write(f"**Color:** {q_product['baseColour']}")
st.markdown('</div>', unsafe_allow_html=True)
with col_recs:
try:
recs = get_complementary_recommendations(selected_id, df, img_embs, top_k=3)
if not recs:
st.info("No complementary products found.")
else:
rec_cols = st.columns(len(recs))
for idx, col_cell in enumerate(rec_cols):
rec = recs[idx]
with col_cell:
st.markdown(f"""
<div class="product-card">
<div class="relationship-badge">{rec['relationship']}</div>
<img src="app/static/{rec['image_path']}" style="width:100%; border-radius:8px; margin-bottom:8px;" onerror="this.src='{rec['image_path']}';">
<div class="product-title" title="{rec['productDisplayName']}">{rec['productDisplayName']}</div>
<div class="product-meta">
<b>Match Score:</b> {rec['score']:.2f}<br>
<b>Gender:</b> {rec['gender']}<br>
<b>Color:</b> {rec['baseColour']}
</div>
</div>
""", unsafe_allow_html=True)
except Exception as e:
st.error(f"Error fetching recommendations: {e}")
# ----------------- TASK 2: UNIQUE PRODUCT CATALOG -----------------
elif app_tab == "Task 2: Unique Catalog":
st.header("Task 2: Unique Product Catalog Creation")
st.write("Group duplicate/near-duplicate products based on image similarity to produce a clean catalog of unique products.")
# Adjust threshold in sidebar
st.sidebar.subheader("Clustering Parameters")
thresh = st.sidebar.slider(
"Cosine Similarity Threshold",
min_value=0.70,
max_value=0.98,
value=0.88,
step=0.01,
help="Higher values create tighter clusters of duplicate items."
)
with st.spinner("Generating unique catalog by clustering embeddings..."):
unique_df, mapping, duplicates, stats = create_unique_catalog(df, img_embs, similarity_threshold=thresh)
st.divider()
# Styled metrics
m1, m2, m3, m4 = st.columns(4)
with m1:
st.markdown(f'<div class="metric-box"><div class="metric-value">{stats["total_original_products"]}</div><div class="metric-label">Original Items</div></div>', unsafe_allow_html=True)
with m2:
st.markdown(f'<div class="metric-box"><div class="metric-value">{stats["total_unique_products"]}</div><div class="metric-label">Unique Catalog Items</div></div>', unsafe_allow_html=True)
with m3:
st.markdown(f'<div class="metric-box"><div class="metric-value">{stats["duplicate_groups_found"]}</div><div class="metric-label">Duplicate Groups</div></div>', unsafe_allow_html=True)
with m4:
st.markdown(f'<div class="metric-box"><div class="metric-value">{stats["compression_percentage"]}%</div><div class="metric-label">Redundancy Removed</div></div>', unsafe_allow_html=True)
st.divider()
col_dup, col_cat = st.columns([2.5, 2.5])
with col_dup:
st.subheader("Browse Duplicate Groups Detected")
st.write("Markets often contain identical/near-identical listings. Here are the clusters grouped by the algorithm:")
if not duplicates:
st.info("No duplicate groups found. Try lowering the Cosine Similarity Threshold in the sidebar.")
else:
selected_cluster_idx = st.selectbox(
"Select Duplicate Cluster to Inspect",
range(len(duplicates)),
format_func=lambda idx: f"Cluster {idx+1} ({len(duplicates[idx]['items'])} items): {duplicates[idx]['representative_name']}"
)
cluster = duplicates[selected_cluster_idx]
st.markdown(f"**Representative Item in Clean Catalog:** `{cluster['representative_name']}` (ID: {cluster['representative_id']})")
st.write("##### Items in this Duplicate Group:")
cols = st.columns(min(4, len(cluster['items'])))
for idx, item in enumerate(cluster['items']):
with cols[idx % 4]:
st.markdown(f"""
<div class="product-card">
<img src="app/static/{item['image_path']}" style="width:100%; border-radius:8px; margin-bottom:8px;" onerror="this.src='{item['image_path']}';">
<div class="product-title" title="{item['productDisplayName']}">{item['productDisplayName']}</div>
<div class="product-meta">
<b>ID:</b> {item['id']}<br>
<b>Color:</b> {item['baseColour']}
</div>
</div>
""", unsafe_allow_html=True)
with col_cat:
st.subheader("Final Cleaned Unique Catalog Preview")
st.write("The deduplicated catalog with standardized names:")
preview_cols = ['id', 'canonicalDisplayName', 'gender', 'masterCategory', 'subCategory', 'articleType', 'baseColour']
st.dataframe(
unique_df[preview_cols].rename(columns={'canonicalDisplayName': 'productDisplayName'}),
use_container_width=True,
height=380
)
# ----------------- TASK 3: REVERSE PRODUCT SEARCH -----------------
elif app_tab == "Task 3: Reverse Search":
st.header("Task 3: Reverse Product Search")
st.write("Search the catalog visually using text queries by matching CLIP text embeddings against product image embeddings.")
st.divider()
# Search box inputs
col_search, col_k = st.columns([3.5, 1.5])
with col_search:
search_query = st.text_input(
"Enter text search query",
placeholder="e.g. blue casual shirt, black sporty running shoe, red print dress, leather bag"
)
with col_k:
k_results = st.slider("Number of results to display", min_value=1, max_value=12, value=4)
st.subheader("Top Matching Products")
if search_query:
with st.spinner(f"Searching for '{search_query}'..."):
results = reverse_product_search(search_query, df, img_embs, top_k=k_results)
if not results:
st.warning("No matching products found.")
else:
cols = st.columns(min(4, len(results)))
for idx, res in enumerate(results):
with cols[idx % 4]:
score_pct = res['score'] * 100
st.markdown(f"""
<div class="product-card">
<div class="relationship-badge" style="background-color: #10ac84;">Match: {score_pct:.1f}%</div>
<img src="app/static/{res['image_path']}" style="width:100%; border-radius:8px; margin-bottom:8px;" onerror="this.src='{res['image_path']}';">
<div class="product-title" title="{res['productDisplayName']}">{res['productDisplayName']}</div>
<div class="product-meta">
<b>Subcategory:</b> {res['subCategory']}<br>
<b>Gender:</b> {res['gender']}<br>
<b>Usage:</b> {res['usage']}
</div>
</div>
""", unsafe_allow_html=True)
else:
st.info("Type a query in the search bar above to query the catalog.")
# Display some sample queries
st.write("##### Try these sample searches:")
sample_queries = [
"blue casual shirt",
"red party dress",
"running shoe",
"sports socks",
"black leather belt",
"sport watch"
]
cols = st.columns(len(sample_queries))
for idx, query in enumerate(sample_queries):
with cols[idx]:
if st.button(query, use_container_width=True):
# We trigger a rerun by setting query in st.session_state
st.session_state.search_query_click = query
# Handle button click query injection
if 'search_query_click' in st.session_state:
st.info(f"Click search bar and hit Enter to search: **{st.session_state.search_query_click}**")