Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 5 |
+
|
| 6 |
+
# --- Load data ---
|
| 7 |
+
df_games = pd.read_csv("games_data.csv")
|
| 8 |
+
df_sales = pd.read_csv("synthetic_sales_data.csv")
|
| 9 |
+
df_reviews = pd.read_csv("synthetic_game_reviews.csv")
|
| 10 |
+
|
| 11 |
+
analyzer = SentimentIntensityAnalyzer()
|
| 12 |
+
|
| 13 |
+
# --- Helper: sales chart for a selected game ---
|
| 14 |
+
def get_sales_chart(title):
|
| 15 |
+
data = df_sales[df_sales["title"] == title].copy()
|
| 16 |
+
data["month"] = pd.to_datetime(data["month"])
|
| 17 |
+
data = data.sort_values("month")
|
| 18 |
+
|
| 19 |
+
fig, ax = plt.subplots(figsize=(8, 3))
|
| 20 |
+
ax.plot(data["month"], data["units_sold"], color="#7c3aed", linewidth=2)
|
| 21 |
+
ax.fill_between(data["month"], data["units_sold"], alpha=0.15, color="#7c3aed")
|
| 22 |
+
ax.set_title(f"Monthly Sales โ {title}", fontsize=12)
|
| 23 |
+
ax.set_xlabel("Month")
|
| 24 |
+
ax.set_ylabel("Units Sold")
|
| 25 |
+
fig.tight_layout()
|
| 26 |
+
return fig
|
| 27 |
+
|
| 28 |
+
# --- Helper: pricing recommendation ---
|
| 29 |
+
def get_recommendation(title):
|
| 30 |
+
game = df_games[df_games["title"] == title]
|
| 31 |
+
if game.empty:
|
| 32 |
+
return "Game not found."
|
| 33 |
+
|
| 34 |
+
score = game["metacritic_score"].values[0]
|
| 35 |
+
price = game["price_eur"].values[0]
|
| 36 |
+
reviews = df_reviews[df_reviews["title"] == title]["review_text"].tolist()
|
| 37 |
+
|
| 38 |
+
if reviews:
|
| 39 |
+
avg_sentiment = sum(
|
| 40 |
+
analyzer.polarity_scores(r)["compound"] for r in reviews
|
| 41 |
+
) / len(reviews)
|
| 42 |
+
else:
|
| 43 |
+
avg_sentiment = 0
|
| 44 |
+
|
| 45 |
+
sales_avg = df_sales[df_sales["title"] == title]["units_sold"].mean()
|
| 46 |
+
|
| 47 |
+
# Rule-based recommendation
|
| 48 |
+
if avg_sentiment > 0.3 and sales_avg > 200:
|
| 49 |
+
rec = "โ
KEEP PRICE โ Strong sentiment and healthy sales."
|
| 50 |
+
elif avg_sentiment > 0.1 and sales_avg < 100:
|
| 51 |
+
rec = "๐ข RUN PROMOTION โ Good reviews but low visibility. Boost with a discount campaign."
|
| 52 |
+
elif avg_sentiment < -0.1 and sales_avg < 80:
|
| 53 |
+
rec = "โ CONSIDER DROPPING โ Poor sentiment and weak sales. Not worth shelf space."
|
| 54 |
+
else:
|
| 55 |
+
rec = "๐ DISCOUNT โ Mixed signals. A price reduction may revive interest."
|
| 56 |
+
|
| 57 |
+
return (
|
| 58 |
+
f"**{title}**\n\n"
|
| 59 |
+
f"- Metacritic Score: {score}\n"
|
| 60 |
+
f"- Price: โฌ{price:.2f}\n"
|
| 61 |
+
f"- Avg Monthly Sales: {sales_avg:.0f} units\n"
|
| 62 |
+
f"- Avg Sentiment Score: {avg_sentiment:.2f}\n\n"
|
| 63 |
+
f"### Recommendation: {rec}"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# --- Build the UI ---
|
| 67 |
+
titles = sorted(df_games["title"].dropna().unique().tolist())
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(theme=gr.themes.Base(), title="๐ฎ Game Pricing Dashboard") as demo:
|
| 70 |
+
gr.Markdown("# ๐ฎ Video Game Retailer Pricing Dashboard")
|
| 71 |
+
gr.Markdown("Select a game to view its sales trend and get a pricing recommendation.")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
dropdown = gr.Dropdown(choices=titles, label="Select a Game", scale=2)
|
| 75 |
+
btn = gr.Button("Analyse", variant="primary")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
chart_out = gr.Plot(label="Sales Trend")
|
| 79 |
+
rec_out = gr.Markdown(label="Pricing Recommendation")
|
| 80 |
+
|
| 81 |
+
btn.click(
|
| 82 |
+
fn=lambda t: (get_sales_chart(t), get_recommendation(t)),
|
| 83 |
+
inputs=dropdown,
|
| 84 |
+
outputs=[chart_out, rec_out]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
demo.launch()
|