Spaces:
Runtime error
Runtime error
Reduced recommendation count from 10 to 5
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ import os
|
|
| 11 |
import time
|
| 12 |
|
| 13 |
# Load dataset with proper error handling
|
| 14 |
-
def load_data(file_path='steam.csv', max_rows=
|
| 15 |
try:
|
| 16 |
data = pd.read_csv(file_path, quotechar='"', on_bad_lines='skip', nrows=max_rows)
|
| 17 |
print(f"Successfully loaded {len(data)} games from {file_path}")
|
|
@@ -114,70 +114,6 @@ def detect_platforms(platforms_str):
|
|
| 114 |
|
| 115 |
return os_icons if os_icons else ["❓ Unknown"]
|
| 116 |
|
| 117 |
-
# Get game details for display
|
| 118 |
-
def get_game_details(game_name):
|
| 119 |
-
if not game_name or game_name not in list_of_all_titles:
|
| 120 |
-
return "Game not found in database."
|
| 121 |
-
|
| 122 |
-
try:
|
| 123 |
-
game_data = data.loc[data['name'] == game_name].iloc[0]
|
| 124 |
-
|
| 125 |
-
# Get genres and format them
|
| 126 |
-
genres = str(game_data.get('genres', 'Unknown'))
|
| 127 |
-
genres_list = [g.strip() for g in genres.split(';') if g.strip()]
|
| 128 |
-
genres_display = ", ".join(genres_list) if genres_list else "Unknown"
|
| 129 |
-
|
| 130 |
-
# Get price and format it
|
| 131 |
-
price = game_data.get('price', 0)
|
| 132 |
-
if isinstance(price, (int, float)):
|
| 133 |
-
price_display = f"${price:.2f}" if price > 0 else "Free to Play"
|
| 134 |
-
else:
|
| 135 |
-
price_display = "Price unknown"
|
| 136 |
-
|
| 137 |
-
# Get platforms
|
| 138 |
-
platforms = game_data.get('platforms', '')
|
| 139 |
-
os_list = detect_platforms(platforms)
|
| 140 |
-
platforms_display = " | ".join(os_list)
|
| 141 |
-
|
| 142 |
-
# Format the details
|
| 143 |
-
details = f"## {game_name}\n\n"
|
| 144 |
-
details += f"**Price:** {price_display}\n\n"
|
| 145 |
-
details += f"**Genres:** {genres_display}\n\n"
|
| 146 |
-
details += f"**Platforms:** {platforms_display}\n\n"
|
| 147 |
-
|
| 148 |
-
# Add rating information if available
|
| 149 |
-
if 'positive_ratings' in game_data:
|
| 150 |
-
pos_ratings = int(game_data.get('positive_ratings', 0))
|
| 151 |
-
details += f"**Positive Ratings:** {pos_ratings:,}\n\n"
|
| 152 |
-
|
| 153 |
-
if 'negative_ratings' in game_data:
|
| 154 |
-
neg_ratings = int(game_data.get('negative_ratings', 0))
|
| 155 |
-
details += f"**Negative Ratings:** {neg_ratings:,}\n\n"
|
| 156 |
-
|
| 157 |
-
# Calculate approval percentage if both values exist
|
| 158 |
-
if pos_ratings + neg_ratings > 0:
|
| 159 |
-
approval_percent = (pos_ratings / (pos_ratings + neg_ratings)) * 100
|
| 160 |
-
details += f"**Approval Rate:** {approval_percent:.1f}%\n\n"
|
| 161 |
-
|
| 162 |
-
# Add release date if available
|
| 163 |
-
if 'release_date' in game_data:
|
| 164 |
-
release_date = game_data.get('release_date', 'Unknown')
|
| 165 |
-
details += f"**Release Date:** {release_date}\n\n"
|
| 166 |
-
|
| 167 |
-
# Add developer/publisher if available
|
| 168 |
-
if 'developer' in game_data:
|
| 169 |
-
developer = game_data.get('developer', 'Unknown')
|
| 170 |
-
details += f"**Developer:** {developer}\n\n"
|
| 171 |
-
|
| 172 |
-
if 'publisher' in game_data:
|
| 173 |
-
publisher = game_data.get('publisher', 'Unknown')
|
| 174 |
-
details += f"**Publisher:** {publisher}\n\n"
|
| 175 |
-
|
| 176 |
-
return details
|
| 177 |
-
|
| 178 |
-
except Exception as e:
|
| 179 |
-
return f"Error retrieving game details: {str(e)}"
|
| 180 |
-
|
| 181 |
# Generate a radar chart for game comparison
|
| 182 |
def generate_game_comparison_chart(game_name):
|
| 183 |
if not game_name or game_name not in list_of_all_titles:
|
|
@@ -238,6 +174,7 @@ def generate_game_comparison_chart(game_name):
|
|
| 238 |
return None
|
| 239 |
|
| 240 |
# Recommend function with improved error handling and consistent return structure
|
|
|
|
| 241 |
def recommend_games(user_game_name_input):
|
| 242 |
# Check cache first
|
| 243 |
if user_game_name_input in recommendation_cache:
|
|
@@ -284,8 +221,8 @@ def recommend_games(user_game_name_input):
|
|
| 284 |
recommendations.append(f"✓ You searched for: {closest_match}")
|
| 285 |
game_list.append(closest_match) # Include the searched game in the list
|
| 286 |
|
| 287 |
-
# Process recommendations
|
| 288 |
-
for i, (index, score) in enumerate(sorted_similar_games[1:
|
| 289 |
if score < 0.2: # Higher threshold for better quality
|
| 290 |
continue
|
| 291 |
|
|
@@ -315,7 +252,7 @@ def recommend_games(user_game_name_input):
|
|
| 315 |
# Add to game list
|
| 316 |
game_list.append(game_name)
|
| 317 |
|
| 318 |
-
if len(recommendations) >=
|
| 319 |
break
|
| 320 |
|
| 321 |
result = ("\n".join(recommendations), game_list)
|
|
@@ -419,13 +356,6 @@ def recommend_and_visualize(user_input):
|
|
| 419 |
|
| 420 |
return recommendations + footer, game_list
|
| 421 |
|
| 422 |
-
# Get details for selected game
|
| 423 |
-
def display_game_details(game_name):
|
| 424 |
-
if not game_name:
|
| 425 |
-
return "Please select a game to view details."
|
| 426 |
-
|
| 427 |
-
return get_game_details(game_name)
|
| 428 |
-
|
| 429 |
# Function to create genre distribution chart
|
| 430 |
def create_genre_chart():
|
| 431 |
try:
|
|
@@ -458,6 +388,7 @@ def create_genre_chart():
|
|
| 458 |
return None
|
| 459 |
|
| 460 |
# Improved Gradio UI with added features
|
|
|
|
| 461 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 462 |
gr.Markdown("# 🎮 Steam Game Recommender")
|
| 463 |
gr.Markdown("Enter the name of a game you like and get recommendations based on similarity!")
|
|
@@ -474,23 +405,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 474 |
run_button = gr.Button("Find Recommendations", variant="primary")
|
| 475 |
|
| 476 |
with gr.Row():
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
)
|
| 483 |
-
dropdown = gr.Dropdown(
|
| 484 |
-
label="Select a Game to View Details",
|
| 485 |
-
choices=[],
|
| 486 |
-
interactive=True,
|
| 487 |
-
info="Choose a game to see its details"
|
| 488 |
-
)
|
| 489 |
-
with gr.Column(scale=1):
|
| 490 |
-
game_details = gr.Markdown(
|
| 491 |
-
label="Game Details",
|
| 492 |
-
value="Select a game from the dropdown to view details."
|
| 493 |
-
)
|
| 494 |
|
| 495 |
with gr.Tab("Statistics"):
|
| 496 |
with gr.Row():
|
|
@@ -524,8 +443,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 524 |
|
| 525 |
1. Enter the name of a game you enjoy in the search box
|
| 526 |
2. Click "Find Recommendations" to see similar games
|
| 527 |
-
3.
|
| 528 |
-
4. Explore the Statistics tab to see distributions of game prices and genres
|
| 529 |
|
| 530 |
### Dataset
|
| 531 |
|
|
@@ -583,15 +501,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 583 |
return []
|
| 584 |
|
| 585 |
# Combined function to update recommendations and history
|
|
|
|
| 586 |
def recommend_and_update_history(user_input):
|
| 587 |
rec_text, game_list = recommend_and_visualize(user_input)
|
| 588 |
history = update_search_history(user_input)
|
| 589 |
-
return rec_text,
|
| 590 |
|
| 591 |
run_button.click(
|
| 592 |
fn=recommend_and_update_history,
|
| 593 |
inputs=input_box,
|
| 594 |
-
outputs=[output_text,
|
| 595 |
show_progress=True
|
| 596 |
)
|
| 597 |
|
|
@@ -599,17 +518,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 599 |
input_box.submit(
|
| 600 |
fn=recommend_and_update_history,
|
| 601 |
inputs=input_box,
|
| 602 |
-
outputs=[output_text,
|
| 603 |
show_progress=True
|
| 604 |
)
|
| 605 |
|
| 606 |
-
# Display game details when a game is selected
|
| 607 |
-
dropdown.change(
|
| 608 |
-
fn=display_game_details,
|
| 609 |
-
inputs=dropdown,
|
| 610 |
-
outputs=game_details
|
| 611 |
-
)
|
| 612 |
-
|
| 613 |
# Clear history button
|
| 614 |
clear_history_button.click(
|
| 615 |
fn=clear_history,
|
|
|
|
| 11 |
import time
|
| 12 |
|
| 13 |
# Load dataset with proper error handling
|
| 14 |
+
def load_data(file_path='steam.csv', max_rows=27075):
|
| 15 |
try:
|
| 16 |
data = pd.read_csv(file_path, quotechar='"', on_bad_lines='skip', nrows=max_rows)
|
| 17 |
print(f"Successfully loaded {len(data)} games from {file_path}")
|
|
|
|
| 114 |
|
| 115 |
return os_icons if os_icons else ["❓ Unknown"]
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# Generate a radar chart for game comparison
|
| 118 |
def generate_game_comparison_chart(game_name):
|
| 119 |
if not game_name or game_name not in list_of_all_titles:
|
|
|
|
| 174 |
return None
|
| 175 |
|
| 176 |
# Recommend function with improved error handling and consistent return structure
|
| 177 |
+
# MODIFIED: Changed to only return 5 recommendations instead of 10
|
| 178 |
def recommend_games(user_game_name_input):
|
| 179 |
# Check cache first
|
| 180 |
if user_game_name_input in recommendation_cache:
|
|
|
|
| 221 |
recommendations.append(f"✓ You searched for: {closest_match}")
|
| 222 |
game_list.append(closest_match) # Include the searched game in the list
|
| 223 |
|
| 224 |
+
# Process recommendations - MODIFIED: Changed to only show 5 recommendations
|
| 225 |
+
for i, (index, score) in enumerate(sorted_similar_games[1:11]): # Only process first 10 to find 5 good ones
|
| 226 |
if score < 0.2: # Higher threshold for better quality
|
| 227 |
continue
|
| 228 |
|
|
|
|
| 252 |
# Add to game list
|
| 253 |
game_list.append(game_name)
|
| 254 |
|
| 255 |
+
if len(recommendations) >= 6: # 5 recommendations + original search - MODIFIED
|
| 256 |
break
|
| 257 |
|
| 258 |
result = ("\n".join(recommendations), game_list)
|
|
|
|
| 356 |
|
| 357 |
return recommendations + footer, game_list
|
| 358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
# Function to create genre distribution chart
|
| 360 |
def create_genre_chart():
|
| 361 |
try:
|
|
|
|
| 388 |
return None
|
| 389 |
|
| 390 |
# Improved Gradio UI with added features
|
| 391 |
+
# MODIFIED: Removed game details feature
|
| 392 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 393 |
gr.Markdown("# 🎮 Steam Game Recommender")
|
| 394 |
gr.Markdown("Enter the name of a game you like and get recommendations based on similarity!")
|
|
|
|
| 405 |
run_button = gr.Button("Find Recommendations", variant="primary")
|
| 406 |
|
| 407 |
with gr.Row():
|
| 408 |
+
output_text = gr.Textbox(
|
| 409 |
+
label="Recommendations",
|
| 410 |
+
lines=15,
|
| 411 |
+
interactive=False
|
| 412 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
with gr.Tab("Statistics"):
|
| 415 |
with gr.Row():
|
|
|
|
| 443 |
|
| 444 |
1. Enter the name of a game you enjoy in the search box
|
| 445 |
2. Click "Find Recommendations" to see similar games
|
| 446 |
+
3. Explore the Statistics tab to see distributions of game prices and genres
|
|
|
|
| 447 |
|
| 448 |
### Dataset
|
| 449 |
|
|
|
|
| 501 |
return []
|
| 502 |
|
| 503 |
# Combined function to update recommendations and history
|
| 504 |
+
# MODIFIED: Removed dropdown from outputs
|
| 505 |
def recommend_and_update_history(user_input):
|
| 506 |
rec_text, game_list = recommend_and_visualize(user_input)
|
| 507 |
history = update_search_history(user_input)
|
| 508 |
+
return rec_text, history
|
| 509 |
|
| 510 |
run_button.click(
|
| 511 |
fn=recommend_and_update_history,
|
| 512 |
inputs=input_box,
|
| 513 |
+
outputs=[output_text, search_history],
|
| 514 |
show_progress=True
|
| 515 |
)
|
| 516 |
|
|
|
|
| 518 |
input_box.submit(
|
| 519 |
fn=recommend_and_update_history,
|
| 520 |
inputs=input_box,
|
| 521 |
+
outputs=[output_text, search_history],
|
| 522 |
show_progress=True
|
| 523 |
)
|
| 524 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
# Clear history button
|
| 526 |
clear_history_button.click(
|
| 527 |
fn=clear_history,
|