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()