Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,60 +35,69 @@ def prepare_features(data):
|
|
| 35 |
|
| 36 |
# Function to get game recommendations
|
| 37 |
def get_recommendations(game_name, data, feature_vectors):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if not
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
closest_match = find_close_match[0]
|
| 45 |
-
index_of_the_game = data.loc[data['name'] == closest_match].index[0]
|
| 46 |
-
|
| 47 |
-
game_similarity = cosine_similarity(feature_vectors)
|
| 48 |
-
similarity_scores = list(enumerate(game_similarity[index_of_the_game]))
|
| 49 |
-
sorted_similar_games = sorted(similarity_scores, key=lambda x: x[1], reverse=True)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
about = data.loc[index, 'short_description'] or "No description available"
|
| 57 |
-
image_url = data.loc[index, 'header_image'] or ""
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
platforms = []
|
| 60 |
-
if
|
| 61 |
-
platforms.append(
|
| 62 |
-
if
|
| 63 |
-
platforms.append(
|
| 64 |
-
if
|
| 65 |
-
platforms.append(
|
| 66 |
-
platforms_str = ", ".join(platforms)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
<div>
|
| 80 |
-
<
|
| 81 |
<p><b>Platforms:</b> {platforms_str}</p>
|
| 82 |
<p><b>Price:</b> ${price}</p>
|
| 83 |
-
<p><b>Metacritic
|
| 84 |
-
<p><b>Positive Reviews:</b> {
|
| 85 |
-
<p>{
|
| 86 |
</div>
|
| 87 |
</div>
|
| 88 |
<hr>
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
return result_html
|
| 92 |
|
| 93 |
# Gradio interface function
|
| 94 |
def recommend_games(game_name, max_age, max_price, min_metacritic):
|
|
|
|
| 35 |
|
| 36 |
# Function to get game recommendations
|
| 37 |
def get_recommendations(game_name, data, feature_vectors):
|
| 38 |
+
titles = data['name'].tolist()
|
| 39 |
+
close_matches = difflib.get_close_matches(game_name, titles)
|
| 40 |
+
|
| 41 |
+
if not close_matches:
|
| 42 |
+
print(f"Couldn't find a close match for: '{game_name}'")
|
| 43 |
+
return "No match found. Try typing a more complete or accurate title."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
match = close_matches[0]
|
| 46 |
+
print(f"Using closest match: {match}")
|
| 47 |
+
game_idx = data[data['name'] == match].index[0]
|
| 48 |
|
| 49 |
+
similarity = cosine_similarity(feature_vectors)
|
| 50 |
+
scores = list(enumerate(similarity[game_idx]))
|
| 51 |
+
similar_games = sorted(scores, key=lambda x: x[1], reverse=True)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
html = ""
|
| 54 |
+
|
| 55 |
+
for i, (idx, score) in enumerate(similar_games[1:10], 1): # skip the first one (it's the same game)
|
| 56 |
+
game = data.loc[idx]
|
| 57 |
+
name = game['name']
|
| 58 |
+
desc = game['short_description'] or "No description provided."
|
| 59 |
+
if len(desc) > 180:
|
| 60 |
+
desc = desc[:180] + "..."
|
| 61 |
+
|
| 62 |
+
img = game.get('header_image', '') or ""
|
| 63 |
+
|
| 64 |
+
# Detect supported platforms
|
| 65 |
platforms = []
|
| 66 |
+
if game.get('windows') == 1:
|
| 67 |
+
platforms.append('Windows')
|
| 68 |
+
if game.get('mac') == 1:
|
| 69 |
+
platforms.append('Mac')
|
| 70 |
+
if game.get('linux') == 1:
|
| 71 |
+
platforms.append('Linux')
|
| 72 |
+
platforms_str = ", ".join(platforms) if platforms else "Unknown"
|
| 73 |
+
|
| 74 |
+
price = game['price']
|
| 75 |
+
meta_score = game.get('metacritic_score')
|
| 76 |
+
meta_display = int(meta_score) if pd.notnull(meta_score) else "N/A"
|
| 77 |
+
|
| 78 |
+
pos = game.get('positive', 0)
|
| 79 |
+
neg = game.get('negative', 0)
|
| 80 |
+
total = pos + neg
|
| 81 |
+
pos_pct = f"{(pos / total * 100):.1f}%" if total > 0 else "N/A"
|
| 82 |
+
|
| 83 |
+
# Build the card HTML
|
| 84 |
+
html += f'''
|
| 85 |
+
<div style="display:flex; margin-bottom:16px;">
|
| 86 |
+
<img src="{img}" width="130" style="margin-right:12px; border-radius:4px;">
|
| 87 |
<div>
|
| 88 |
+
<h4>{i}. {name}</h4>
|
| 89 |
<p><b>Platforms:</b> {platforms_str}</p>
|
| 90 |
<p><b>Price:</b> ${price}</p>
|
| 91 |
+
<p><b>Metacritic:</b> {meta_display}</p>
|
| 92 |
+
<p><b>Positive Reviews:</b> {pos_pct}</p>
|
| 93 |
+
<p>{desc}</p>
|
| 94 |
</div>
|
| 95 |
</div>
|
| 96 |
<hr>
|
| 97 |
+
'''
|
| 98 |
+
|
| 99 |
+
return html
|
| 100 |
|
|
|
|
| 101 |
|
| 102 |
# Gradio interface function
|
| 103 |
def recommend_games(game_name, max_age, max_price, min_metacritic):
|