Update app.py
Browse files
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 |
-
#
|
| 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 |
-
#
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
(df['brand'] == brand) &
|
| 17 |
(df['color'] == color) &
|
| 18 |
(df['product_type_name'] == product_type)
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
results = price_quality_recommendations(asin, df)
|
| 30 |
if results.empty:
|
| 31 |
-
return "
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Build Gradio
|
| 35 |
-
with gr.Blocks() as
|
| 36 |
gr.Markdown("Amazon Apparel Recommender")
|
| 37 |
-
gr.Markdown("Select product
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
-
brand_input = gr.Dropdown(
|
| 41 |
-
color_input = gr.Dropdown(
|
| 42 |
-
type_input = gr.Dropdown(
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
-
filter_btn = gr.Button("
|
| 48 |
-
recommend_btn = gr.Button("
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
| 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()
|