Bnava13 commited on
Commit
1207d3b
·
verified ·
1 Parent(s): 9cc7f36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -43
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
- list_of_all_titles = data['name'].tolist()
39
- find_close_match = difflib.get_close_matches(game_name, list_of_all_titles)
40
-
41
- if not find_close_match:
42
- return "No match found for the game name. Please try another title."
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
- result_html = ""
 
 
52
 
53
- for i, game in enumerate(sorted_similar_games[1:10], 1):
54
- index = game[0]
55
- name = data.loc[index, 'name']
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 data.loc[index, 'windows'] == 1:
61
- platforms.append("Windows")
62
- if data.loc[index, 'mac'] == 1:
63
- platforms.append("Mac")
64
- if data.loc[index, 'linux'] == 1:
65
- platforms.append("Linux")
66
- platforms_str = ", ".join(platforms) or "Unknown"
67
-
68
- metacritic = data.loc[index, 'metacritic_score']
69
- price = data.loc[index, 'price']
70
- pos = data.loc[index, 'positive']
71
- neg = data.loc[index, 'negative']
72
- total_reviews = pos + neg
73
- pos_ratio = f"{(pos / total_reviews * 100):.1f}%" if total_reviews > 0 else "N/A"
74
-
75
- # Combine into HTML
76
- result_html += f"""
77
- <div style="display:flex; align-items:flex-start; margin-bottom:20px;">
78
- <img src="{image_url}" style="width:150px; height:auto; margin-right:15px; border-radius:8px;">
 
 
79
  <div>
80
- <h3>{i}. {name}</h3>
81
  <p><b>Platforms:</b> {platforms_str}</p>
82
  <p><b>Price:</b> ${price}</p>
83
- <p><b>Metacritic Score:</b> {metacritic if pd.notnull(metacritic) else "N/A"}</p>
84
- <p><b>Positive Reviews:</b> {pos_ratio}</p>
85
- <p>{about}</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):