import pandas as pd import difflib from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity import gradio as gr import numpy as np # Load the data def load_data(): try: data = pd.read_csv('games_march2025_cleaned.csv', nrows=20000, on_bad_lines='skip', engine='python') return data except Exception as e: print(f"Error loading data: {e}") return None # Prepare the feature vectors for similarity calculation def prepare_features(data): selected_features = ['genres', 'price', 'average_playtime_2weeks', 'tags'] for feature in selected_features: data[feature] = data[feature].fillna('') combined_features = ( data['genres'] + ' ' + data['price'].astype(str) + ' ' + data['average_playtime_2weeks'].astype(str) + ' ' + data['tags'].astype(str) ) vectorizer = TfidfVectorizer() feature_vectors = vectorizer.fit_transform(combined_features) return feature_vectors # Function to get game recommendations def get_recommendations(game_name, data, feature_vectors): titles = data['name'].tolist() close_matches = difflib.get_close_matches(game_name, titles) if not close_matches: print(f"Couldn't find a close match for: '{game_name}'") return "No match found. Try typing a more complete or accurate title." match = close_matches[0] print(f"Using closest match: {match}") game_idx = data[data['name'] == match].index[0] similarity = cosine_similarity(feature_vectors) scores = list(enumerate(similarity[game_idx])) similar_games = sorted(scores, key=lambda x: x[1], reverse=True) html = "" for i, (idx, score) in enumerate(similar_games[1:10], 1): # skip the first one (it's the same game) game = data.loc[idx] name = game['name'] desc = game['short_description'] or "No description provided." if len(desc) > 180: desc = desc[:180] + "..." img = game.get('header_image', '') or "" # Detect supported platforms platforms = [] if game.get('windows') == 1: platforms.append('Windows') if game.get('mac') == 1: platforms.append('Mac') if game.get('linux') == 1: platforms.append('Linux') platforms_str = ", ".join(platforms) if platforms else "Unknown" price = game['price'] meta_score = game.get('metacritic_score') meta_display = int(meta_score) if pd.notnull(meta_score) else "N/A" pos = game.get('positive', 0) neg = game.get('negative', 0) total = pos + neg pos_pct = f"{(pos / total * 100):.1f}%" if total > 0 else "N/A" # Build the card HTML html += f'''
Platforms: {platforms_str}
Price: ${price}
Metacritic: {meta_display}
Positive Reviews: {pos_pct}
{desc}