Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import joblib | |
| import re | |
| import matplotlib.pyplot as plt | |
| products = pd.read_csv("products_master.csv") | |
| model = joblib.load("rf_model.joblib") | |
| def shorten(name, max_len=85): | |
| # Base product = everything before first comma | |
| base = name.split(",")[0].strip() | |
| # Extract storage like "16 GB", "32 GB" | |
| storage_match = re.search(r"(\d+)\s*GB", name) | |
| storage = storage_match.group(0) if storage_match else "" | |
| # Detect colour / variant (longer phrases first) | |
| colors = [ | |
| "Marine Blue", "Pink Kid-Proof Case", "Blue Kid-Proof Case", | |
| "Green Kid-Proof Case", "Tangerine", "Magenta", | |
| "Black", "Blue", "Pink", "Green", "White", "Yellow", "Red" | |
| ] | |
| found_color = "" | |
| for c in colors: | |
| if c.lower() in name.lower(): | |
| found_color = c | |
| break | |
| # Detect ads vs ad-free | |
| n_lower = name.lower() | |
| if "without special offer" in n_lower or "no special offer" in n_lower: | |
| offers = "Ad-Free" | |
| elif "special offer" in n_lower: | |
| offers = "with Ads" | |
| else: | |
| offers = "" | |
| extras = [x for x in [storage, found_color, offers] if x] | |
| label = base + (" – " + " / ".join(extras) if extras else "") | |
| if len(label) > max_len: | |
| label = label[:max_len-1] + "…" | |
| return label | |
| # Build (label, value) pairs and de-duplicate any remaining collisions | |
| raw = [(shorten(n), n) for n in products["name"].tolist()] | |
| seen = {} | |
| dropdown_choices = [] | |
| for label, full in raw: | |
| if label in seen: | |
| seen[label] += 1 | |
| label = f"{label} (v{seen[label]})" | |
| else: | |
| seen[label] = 1 | |
| dropdown_choices.append((label, full)) | |
| dropdown_choices.sort(key=lambda x: x[0]) | |
| def make_recommendation(product_name): | |
| row = products[products["name"] == product_name].iloc[0] | |
| fig, ax = plt.subplots(figsize=(5, 3)) | |
| counts = [row["pct_positive"], row["pct_neutral"], row["pct_negative"]] | |
| ax.bar(["Positive", "Neutral", "Negative"], | |
| counts, color=["#2ecc71", "#95a5a6", "#e74c3c"], edgecolor="black") | |
| ax.set_ylabel("% of reviews"); ax.set_title("Review sentiment breakdown"); ax.set_ylim(0, 1) | |
| action = row["model_prediction"] | |
| emoji = {"Raise": "⬆️", "Hold": "➡️", "Drop": "⬇️"}[action] | |
| summary = f""" | |
| ### {shorten(row["name"], max_len=110)} | |
| **Brand:** {row["brand"]} | |
| **Reviews analyzed:** {int(row["n_reviews"]):,} | |
| **Average rating:** {row["avg_rating"]:.2f} / 5 | |
| **Average sentiment (VADER):** {row["avg_compound"]:.3f} | |
| --- | |
| ### {emoji} Recommendation: **{action} the price** | |
| | Metric | Current | Recommended | | |
| |---|---|---| | |
| | Price | ${row["current_price"]:.2f} | ${row["recommended_price"]:.2f} | | |
| | Profit per unit | ${row["current_profit_per_unit"]:.2f} | ${row["recommended_profit_per_unit"]:.2f} | | |
| **Estimated monthly profit change:** **${row["monthly_profit_change"]:,.2f}** | |
| """ | |
| return summary, fig | |
| with gr.Blocks(title="Amazon Electronics Pricing Recommender") as demo: | |
| gr.Markdown("# 🛒 Amazon Electronics Pricing Recommender") | |
| gr.Markdown("Pick a product to see its sentiment breakdown and a price recommendation based on a Random Forest model trained on real reviews.") | |
| product_dropdown = gr.Dropdown( | |
| choices=dropdown_choices, | |
| label="Select a product", | |
| value=dropdown_choices[0][1], | |
| ) | |
| btn = gr.Button("Get Recommendation", variant="primary") | |
| with gr.Row(): | |
| output_text = gr.Markdown() | |
| output_chart = gr.Plot() | |
| btn.click(make_recommendation, inputs=product_dropdown, outputs=[output_text, output_chart]) | |
| demo.load (make_recommendation, inputs=product_dropdown, outputs=[output_text, output_chart]) | |
| demo.launch() | |