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

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -87
app.py DELETED
@@ -1,87 +0,0 @@
1
- import gradio as gr
2
- import pandas as pd
3
- import joblib
4
- import matplotlib.pyplot as plt
5
-
6
- # Load the data and the trained model
7
- products = pd.read_csv("products_master.csv")
8
- model = joblib.load("rf_model.joblib")
9
-
10
- # --- NEW: build short, readable labels for the dropdown ---
11
- def shorten(name, max_len=55):
12
- # Use only the first comma-separated chunk (drops "8 HD Display, Wi-Fi, ..." stuff)
13
- short = name.split(",")[0].strip()
14
- # Keep color/edition if it appears at the end (e.g. "Tangerine", "Blue", "Pink")
15
- parts = name.split(",")
16
- if len(parts) > 1:
17
- last = parts[-1].split("-")[0].strip()
18
- # If the last chunk is short and looks like a color/variant, append it
19
- if 0 < len(last) <= 25 and last.lower() != short.lower().split()[-1]:
20
- short = f"{short} – {last}"
21
- if len(short) > max_len:
22
- short = short[:max_len-1] + "…"
23
- return short
24
-
25
- # Build (label, value) pairs: label = short, value = full original name
26
- dropdown_choices = sorted(
27
- [(shorten(n), n) for n in products["name"].tolist()],
28
- key=lambda x: x[0]
29
- )
30
- # -----------------------------------------------------------
31
-
32
- def make_recommendation(product_name):
33
- row = products[products["name"] == product_name].iloc[0]
34
-
35
- # Build sentiment chart
36
- fig, ax = plt.subplots(figsize=(5, 3))
37
- counts = [row["pct_positive"], row["pct_neutral"], row["pct_negative"]]
38
- colors = ["#2ecc71", "#95a5a6", "#e74c3c"]
39
- ax.bar(["Positive", "Neutral", "Negative"], counts, color=colors, edgecolor="black")
40
- ax.set_ylabel("% of reviews")
41
- ax.set_title("Review sentiment breakdown")
42
- ax.set_ylim(0, 1)
43
-
44
- action = row["model_prediction"]
45
- emoji = {"Raise": "⬆️", "Hold": "➡️", "Drop": "⬇️"}[action]
46
-
47
- summary = f"""
48
- ### {shorten(row["name"], max_len=80)}
49
-
50
- **Brand:** {row["brand"]}
51
- **Reviews analyzed:** {int(row["n_reviews"]):,}
52
- **Average rating:** {row["avg_rating"]:.2f} / 5
53
- **Average sentiment (VADER):** {row["avg_compound"]:.3f}
54
-
55
- ---
56
-
57
- ### {emoji} Recommendation: **{action} the price**
58
-
59
- | Metric | Current | Recommended |
60
- |---|---|---|
61
- | Price | ${row["current_price"]:.2f} | ${row["recommended_price"]:.2f} |
62
- | Profit per unit | ${row["current_profit_per_unit"]:.2f} | ${row["recommended_profit_per_unit"]:.2f} |
63
-
64
- **Estimated monthly profit change:** **${row["monthly_profit_change"]:,.2f}**
65
- """
66
- return summary, fig
67
-
68
-
69
- with gr.Blocks(title="Amazon Electronics Pricing Recommender") as demo:
70
- gr.Markdown("# 🛒 Amazon Electronics Pricing Recommender")
71
- gr.Markdown("Pick a product to see its sentiment breakdown and a price recommendation based on a Random Forest model trained on real reviews.")
72
-
73
- product_dropdown = gr.Dropdown(
74
- choices=dropdown_choices,
75
- label="Select a product",
76
- value=dropdown_choices[0][1],
77
- )
78
- btn = gr.Button("Get Recommendation", variant="primary")
79
-
80
- with gr.Row():
81
- output_text = gr.Markdown()
82
- output_chart = gr.Plot()
83
-
84
- btn.click(make_recommendation, inputs=product_dropdown, outputs=[output_text, output_chart])
85
- demo.load (make_recommendation, inputs=product_dropdown, outputs=[output_text, output_chart])
86
-
87
- demo.launch()