File size: 3,129 Bytes
8a0443a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# --- Load data ---
df_games = pd.read_csv("games_data.csv")
df_sales = pd.read_csv("synthetic_sales_data.csv")
df_reviews = pd.read_csv("synthetic_game_reviews.csv")

analyzer = SentimentIntensityAnalyzer()

# --- Helper: sales chart for a selected game ---
def get_sales_chart(title):
    data = df_sales[df_sales["title"] == title].copy()
    data["month"] = pd.to_datetime(data["month"])
    data = data.sort_values("month")

    fig, ax = plt.subplots(figsize=(8, 3))
    ax.plot(data["month"], data["units_sold"], color="#7c3aed", linewidth=2)
    ax.fill_between(data["month"], data["units_sold"], alpha=0.15, color="#7c3aed")
    ax.set_title(f"Monthly Sales โ€” {title}", fontsize=12)
    ax.set_xlabel("Month")
    ax.set_ylabel("Units Sold")
    fig.tight_layout()
    return fig

# --- Helper: pricing recommendation ---
def get_recommendation(title):
    game = df_games[df_games["title"] == title]
    if game.empty:
        return "Game not found."

    score = game["metacritic_score"].values[0]
    price = game["price_eur"].values[0]
    reviews = df_reviews[df_reviews["title"] == title]["review_text"].tolist()
    
    if reviews:
        avg_sentiment = sum(
            analyzer.polarity_scores(r)["compound"] for r in reviews
        ) / len(reviews)
    else:
        avg_sentiment = 0

    sales_avg = df_sales[df_sales["title"] == title]["units_sold"].mean()

    # Rule-based recommendation
    if avg_sentiment > 0.3 and sales_avg > 200:
        rec = "โœ… KEEP PRICE โ€” Strong sentiment and healthy sales."
    elif avg_sentiment > 0.1 and sales_avg < 100:
        rec = "๐Ÿ“ข RUN PROMOTION โ€” Good reviews but low visibility. Boost with a discount campaign."
    elif avg_sentiment < -0.1 and sales_avg < 80:
        rec = "โŒ CONSIDER DROPPING โ€” Poor sentiment and weak sales. Not worth shelf space."
    else:
        rec = "๐Ÿ’› DISCOUNT โ€” Mixed signals. A price reduction may revive interest."

    return (
        f"**{title}**\n\n"
        f"- Metacritic Score: {score}\n"
        f"- Price: โ‚ฌ{price:.2f}\n"
        f"- Avg Monthly Sales: {sales_avg:.0f} units\n"
        f"- Avg Sentiment Score: {avg_sentiment:.2f}\n\n"
        f"### Recommendation: {rec}"
    )

# --- Build the UI ---
titles = sorted(df_games["title"].dropna().unique().tolist())

with gr.Blocks(theme=gr.themes.Base(), title="๐ŸŽฎ Game Pricing Dashboard") as demo:
    gr.Markdown("# ๐ŸŽฎ Video Game Retailer Pricing Dashboard")
    gr.Markdown("Select a game to view its sales trend and get a pricing recommendation.")

    with gr.Row():
        dropdown = gr.Dropdown(choices=titles, label="Select a Game", scale=2)
        btn = gr.Button("Analyse", variant="primary")

    with gr.Row():
        chart_out = gr.Plot(label="Sales Trend")
        rec_out = gr.Markdown(label="Pricing Recommendation")

    btn.click(
        fn=lambda t: (get_sales_chart(t), get_recommendation(t)),
        inputs=dropdown,
        outputs=[chart_out, rec_out]
    )

demo.launch()