JenetGhumman commited on
Commit
3da5d54
Β·
verified Β·
1 Parent(s): 7949562

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -33
app.py CHANGED
@@ -5,51 +5,59 @@ from amazon_apparel_recommender import price_quality_recommendations
5
  # Load data
6
  df = pd.read_csv("assets/cleaned_metadata.csv")
7
 
8
- # Define dropdown options
9
- brands = sorted(df['brand'].dropna().unique().tolist())
10
- colors = sorted(df['color'].dropna().unique().tolist())
11
- product_types = sorted(df['product_type_name'].dropna().unique().tolist())
12
-
13
- # Function to get ASINs based on selected filters
14
- def get_filtered_asins(brand, color, product_type):
15
- filtered = df[
16
  (df['brand'] == brand) &
17
  (df['color'] == color) &
18
  (df['product_type_name'] == product_type)
19
- ]
20
- options = filtered[['asin', 'title']].drop_duplicates()
21
- asin_options = [f"{row['asin']} β€” {row['title'][:60]}" for _, row in options.iterrows()]
22
- return asin_options
23
-
24
- # Function to extract ASIN and recommend products
25
- def recommend_from_asin(selected):
26
- if not selected:
27
- return "Please select a product."
28
- asin = selected.split(" β€” ")[0]
 
 
 
29
  results = price_quality_recommendations(asin, df)
30
  if results.empty:
31
- return "No recommendations found for this product."
32
- return results[['title', 'brand', 'price', 'review_score']].head(5)
 
 
 
33
 
34
- # Build Gradio UI
35
- with gr.Blocks() as demo:
36
  gr.Markdown("Amazon Apparel Recommender")
37
- gr.Markdown("Select product filters to view similar item suggestions.")
38
 
39
  with gr.Row():
40
- brand_input = gr.Dropdown(choices=brands, label="Brand", interactive=True)
41
- color_input = gr.Dropdown(choices=colors, label="Color", interactive=True)
42
- type_input = gr.Dropdown(choices=product_types, label="Product Type", interactive=True)
43
 
44
- asin_dropdown = gr.Dropdown(label="Select Matching Product (ASIN β€” Title)", interactive=True)
45
 
46
  with gr.Row():
47
- filter_btn = gr.Button(" Find Matching Products")
48
- recommend_btn = gr.Button(" Get Recommendations")
49
 
50
- output = gr.Dataframe(headers=["Title", "Brand", "Price", "Rating"])
51
 
52
- filter_btn.click(fn=get_filtered_asins, inputs=[brand_input, color_input, type_input], outputs=asin_dropdown)
53
- recommend_btn.click(fn=recommend_from_asin, inputs=asin_dropdown, outputs=output)
 
54
 
55
- demo.launch()
 
 
5
  # Load data
6
  df = pd.read_csv("assets/cleaned_metadata.csv")
7
 
8
+ # Precompute dropdown options
9
+ brands = sorted(df['brand'].dropna().unique())
10
+ colors = sorted(df['color'].dropna().unique())
11
+ product_types = sorted(df['product_type_name'].dropna().unique())
12
+
13
+ # Filter ASINs based on dropdown selections
14
+ def filter_products(brand, color, product_type):
15
+ filtered_df = df[
16
  (df['brand'] == brand) &
17
  (df['color'] == color) &
18
  (df['product_type_name'] == product_type)
19
+ ][['asin', 'title']].drop_duplicates()
20
+
21
+ if filtered_df.empty:
22
+ return ["No matches found"]
23
+
24
+ return [f"{row['asin']} β€” {row['title'][:80]}" for _, row in filtered_df.iterrows()]
25
+
26
+ # Recommend products based on selected ASIN
27
+ def recommend_products(asin_combo):
28
+ if not asin_combo or asin_combo.startswith("No matches"):
29
+ return pd.DataFrame(columns=["Title", "Brand", "Price", "Rating"])
30
+
31
+ asin = asin_combo.split(" β€” ")[0]
32
  results = price_quality_recommendations(asin, df)
33
  if results.empty:
34
+ return pd.DataFrame(columns=["Title", "Brand", "Price", "Rating"])
35
+
36
+ return results[['title', 'brand', 'price', 'review_score']].head(5).rename(
37
+ columns={"title": "Title", "brand": "Brand", "price": "Price", "review_score": "Rating"}
38
+ )
39
 
40
+ # Build Gradio interface
41
+ with gr.Blocks(title="Amazon Apparel Recommender") as app:
42
  gr.Markdown("Amazon Apparel Recommender")
43
+ gr.Markdown("Select brand, color, and product type to view similar item recommendations.")
44
 
45
  with gr.Row():
46
+ brand_input = gr.Dropdown(label="Brand", choices=brands, interactive=True)
47
+ color_input = gr.Dropdown(label="Color", choices=colors, interactive=True)
48
+ type_input = gr.Dropdown(label="Product Type", choices=product_types, interactive=True)
49
 
50
+ asin_input = gr.Dropdown(label="Matching Product (ASIN β€” Title)", interactive=True)
51
 
52
  with gr.Row():
53
+ filter_btn = gr.Button("Find Matching Products")
54
+ recommend_btn = gr.Button("Get Recommendations")
55
 
56
+ output_table = gr.Dataframe(headers=["Title", "Brand", "Price", "Rating"], interactive=False)
57
 
58
+ # Logic binding
59
+ filter_btn.click(fn=filter_products, inputs=[brand_input, color_input, type_input], outputs=asin_input)
60
+ recommend_btn.click(fn=recommend_products, inputs=asin_input, outputs=output_table)
61
 
62
+ # Launch app
63
+ app.launch()