nataliala commited on
Commit
7cfd42e
·
verified ·
1 Parent(s): 67ddb41

Upload app-5.py

Browse files
Files changed (1) hide show
  1. app-5.py +111 -0
app-5.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import joblib
4
+ import re
5
+ import matplotlib.pyplot as plt
6
+
7
+ products = pd.read_csv("products_master.csv")
8
+ model = joblib.load("rf_model.joblib")
9
+
10
+ def shorten(name, max_len=85):
11
+ # Base product = everything before first comma
12
+ base = name.split(",")[0].strip()
13
+
14
+ # Extract storage like "16 GB", "32 GB"
15
+ storage_match = re.search(r"(\d+)\s*GB", name)
16
+ storage = storage_match.group(0) if storage_match else ""
17
+
18
+ # Detect colour / variant (longer phrases first)
19
+ colors = [
20
+ "Marine Blue", "Pink Kid-Proof Case", "Blue Kid-Proof Case",
21
+ "Green Kid-Proof Case", "Tangerine", "Magenta",
22
+ "Black", "Blue", "Pink", "Green", "White", "Yellow", "Red"
23
+ ]
24
+ found_color = ""
25
+ for c in colors:
26
+ if c.lower() in name.lower():
27
+ found_color = c
28
+ break
29
+
30
+ # Detect ads vs ad-free
31
+ n_lower = name.lower()
32
+ if "without special offer" in n_lower or "no special offer" in n_lower:
33
+ offers = "Ad-Free"
34
+ elif "special offer" in n_lower:
35
+ offers = "with Ads"
36
+ else:
37
+ offers = ""
38
+
39
+ extras = [x for x in [storage, found_color, offers] if x]
40
+ label = base + (" – " + " / ".join(extras) if extras else "")
41
+
42
+ if len(label) > max_len:
43
+ label = label[:max_len-1] + "…"
44
+ return label
45
+
46
+ # Build (label, value) pairs and de-duplicate any remaining collisions
47
+ raw = [(shorten(n), n) for n in products["name"].tolist()]
48
+ seen = {}
49
+ dropdown_choices = []
50
+ for label, full in raw:
51
+ if label in seen:
52
+ seen[label] += 1
53
+ label = f"{label} (v{seen[label]})"
54
+ else:
55
+ seen[label] = 1
56
+ dropdown_choices.append((label, full))
57
+ dropdown_choices.sort(key=lambda x: x[0])
58
+
59
+ def make_recommendation(product_name):
60
+ row = products[products["name"] == product_name].iloc[0]
61
+
62
+ fig, ax = plt.subplots(figsize=(5, 3))
63
+ counts = [row["pct_positive"], row["pct_neutral"], row["pct_negative"]]
64
+ ax.bar(["Positive", "Neutral", "Negative"],
65
+ counts, color=["#2ecc71", "#95a5a6", "#e74c3c"], edgecolor="black")
66
+ ax.set_ylabel("% of reviews"); ax.set_title("Review sentiment breakdown"); ax.set_ylim(0, 1)
67
+
68
+ action = row["model_prediction"]
69
+ emoji = {"Raise": "⬆️", "Hold": "➡️", "Drop": "⬇️"}[action]
70
+
71
+ summary = f"""
72
+ ### {shorten(row["name"], max_len=110)}
73
+
74
+ **Brand:** {row["brand"]}
75
+ **Reviews analyzed:** {int(row["n_reviews"]):,}
76
+ **Average rating:** {row["avg_rating"]:.2f} / 5
77
+ **Average sentiment (VADER):** {row["avg_compound"]:.3f}
78
+
79
+ ---
80
+
81
+ ### {emoji} Recommendation: **{action} the price**
82
+
83
+ | Metric | Current | Recommended |
84
+ |---|---|---|
85
+ | Price | ${row["current_price"]:.2f} | ${row["recommended_price"]:.2f} |
86
+ | Profit per unit | ${row["current_profit_per_unit"]:.2f} | ${row["recommended_profit_per_unit"]:.2f} |
87
+
88
+ **Estimated monthly profit change:** **${row["monthly_profit_change"]:,.2f}**
89
+ """
90
+ return summary, fig
91
+
92
+
93
+ with gr.Blocks(title="Amazon Electronics Pricing Recommender") as demo:
94
+ gr.Markdown("# 🛒 Amazon Electronics Pricing Recommender")
95
+ gr.Markdown("Pick a product to see its sentiment breakdown and a price recommendation based on a Random Forest model trained on real reviews.")
96
+
97
+ product_dropdown = gr.Dropdown(
98
+ choices=dropdown_choices,
99
+ label="Select a product",
100
+ value=dropdown_choices[0][1],
101
+ )
102
+ btn = gr.Button("Get Recommendation", variant="primary")
103
+
104
+ with gr.Row():
105
+ output_text = gr.Markdown()
106
+ output_chart = gr.Plot()
107
+
108
+ btn.click(make_recommendation, inputs=product_dropdown, outputs=[output_text, output_chart])
109
+ demo.load (make_recommendation, inputs=product_dropdown, outputs=[output_text, output_chart])
110
+
111
+ demo.launch()