Bnava13 commited on
Commit
dbeda2b
ยท
verified ยท
1 Parent(s): 42a1647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -45
app.py CHANGED
@@ -9,7 +9,7 @@ import numpy as np
9
  def load_data():
10
  try:
11
  # For Hugging Face Spaces deployment, you might need to adjust this path
12
- data = pd.read_csv('games_march2025_cleaned.csv', nrows=88899, on_bad_lines='skip', engine='python')
13
  return data
14
  except Exception as e:
15
  print(f"Error loading data: {e}")
@@ -51,7 +51,8 @@ def get_recommendations(game_name, data, feature_vectors):
51
 
52
  result_html = ""
53
 
54
- for i, (index, score) in enumerate(sorted_similar_games[1:10], 1):
 
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 ""
@@ -65,19 +66,22 @@ def get_recommendations(game_name, data, feature_vectors):
65
  platforms.append("Linux")
66
  platforms_str = ", ".join(platforms) or "Unknown"
67
 
 
68
  price = data.loc[index, 'price']
69
  pos = data.loc[index, 'positive']
70
  neg = data.loc[index, 'negative']
71
  total_reviews = pos + neg
72
  pos_ratio = f"{(pos / total_reviews * 100):.1f}%" if total_reviews > 0 else "N/A"
73
 
 
74
  result_html += f"""
75
  <div style="display:flex; align-items:flex-start; margin-bottom:20px;">
76
  <img src="{image_url}" style="width:150px; height:auto; margin-right:15px; border-radius:8px;">
77
  <div>
78
- <h3>{i}. {name} <small>(Similarity: {score:.2f})</small></h3>
79
  <p><b>Platforms:</b> {platforms_str}</p>
80
  <p><b>Price:</b> ${price}</p>
 
81
  <p><b>Positive Reviews:</b> {pos_ratio}</p>
82
  <p>{about}</p>
83
  </div>
@@ -87,29 +91,20 @@ def get_recommendations(game_name, data, feature_vectors):
87
 
88
  return result_html
89
 
 
90
  # Gradio interface function
91
- def recommend_games(game_name, max_age, max_price, min_pos_neg_ratio):
92
  data = load_data()
93
  if data is None:
94
  return "Failed to load data. Please check the data file."
95
-
96
- # Fill NA values to avoid division errors
97
- data['positive'] = data['positive'].fillna(0)
98
- data['negative'] = data['negative'].fillna(0)
99
-
100
- # Calculate the positive-to-negative ratio (avoid division by zero)
101
- data['pos_neg_ratio'] = data.apply(
102
- lambda row: (row['positive'] / row['negative']) if row['negative'] > 0 else row['positive'],
103
- axis=1
104
- )
105
-
106
- # Apply filters
107
  data = data[
108
  (data['required_age'] <= max_age) &
109
  (data['price'] <= max_price) &
110
- (data['pos_neg_ratio'] >= min_pos_neg_ratio)
111
  ].reset_index(drop=True)
112
-
113
  if data.empty:
114
  return "No games found matching your filter criteria."
115
 
@@ -119,51 +114,32 @@ def recommend_games(game_name, max_age, max_price, min_pos_neg_ratio):
119
  return recommendations_html
120
 
121
 
122
-
123
- # Format the output for Gradio
124
- result_texts = []
125
- result_images = []
126
-
127
- for result, image_url in recommendations:
128
- result_texts.append(result)
129
- if image_url and str(image_url) != 'nan':
130
- result_images.append(image_url)
131
- else:
132
- # Use a placeholder image if no image URL is available
133
- result_images.append(None)
134
-
135
- # Create a gallery of results
136
- results_html = ""
137
- for i, (text, img) in enumerate(zip(result_texts, result_images)):
138
- results_html += text
139
-
140
- return results_html, result_images
141
-
142
  # Create the Gradio interface
143
  with gr.Blocks(title="Steam Game Recommender") as demo:
144
- gr.Markdown("# ๐ŸŽฎ Steam Game Recommender")
145
- gr.Markdown("Enter a game you like and adjust filters to get the best matches.")
146
 
147
  with gr.Row():
148
- input_text = gr.Textbox(label="๐ŸŽฏ Favorite Game")
149
 
150
  with gr.Row():
151
  max_age_slider = gr.Slider(0, 21, value=17, label="Max Age Rating (Avoid Adult Games)")
152
  max_price_slider = gr.Slider(0.0, 100.0, value=60.0, step=0.5, label="Maximum Price ($)")
153
- min_pos_neg_slider = gr.Slider(0.0, 10.0, value=2.0, step=0.1, label="Min Positive:Negative Ratio")
154
 
155
  with gr.Row():
156
- submit_btn = gr.Button("๐Ÿ” Get Recommendations")
157
 
158
  with gr.Row():
159
- output_text = gr.Markdown(label="๐Ÿง  Recommendations")
160
 
161
  submit_btn.click(
162
  fn=recommend_games,
163
- inputs=[input_text, max_age_slider, max_price_slider, min_pos_neg_slider],
164
  outputs=output_text
165
  )
166
 
 
167
  # Launch the app
168
  if __name__ == "__main__":
169
  demo.launch()
 
9
  def load_data():
10
  try:
11
  # For Hugging Face Spaces deployment, you might need to adjust this path
12
+ data = pd.read_csv('games_march2025_cleaned.csv', nrows=20000, on_bad_lines='skip', engine='python')
13
  return data
14
  except Exception as e:
15
  print(f"Error loading data: {e}")
 
51
 
52
  result_html = ""
53
 
54
+ for i, game in enumerate(sorted_similar_games[1:10], 1):
55
+ index = game[0]
56
  name = data.loc[index, 'name']
57
  about = data.loc[index, 'short_description'] or "No description available"
58
  image_url = data.loc[index, 'header_image'] or ""
 
66
  platforms.append("Linux")
67
  platforms_str = ", ".join(platforms) or "Unknown"
68
 
69
+ metacritic = data.loc[index, 'metacritic_score']
70
  price = data.loc[index, 'price']
71
  pos = data.loc[index, 'positive']
72
  neg = data.loc[index, 'negative']
73
  total_reviews = pos + neg
74
  pos_ratio = f"{(pos / total_reviews * 100):.1f}%" if total_reviews > 0 else "N/A"
75
 
76
+ # Combine into HTML
77
  result_html += f"""
78
  <div style="display:flex; align-items:flex-start; margin-bottom:20px;">
79
  <img src="{image_url}" style="width:150px; height:auto; margin-right:15px; border-radius:8px;">
80
  <div>
81
+ <h3>{i}. {name}</h3>
82
  <p><b>Platforms:</b> {platforms_str}</p>
83
  <p><b>Price:</b> ${price}</p>
84
+ <p><b>Metacritic Score:</b> {metacritic if pd.notnull(metacritic) else "N/A"}</p>
85
  <p><b>Positive Reviews:</b> {pos_ratio}</p>
86
  <p>{about}</p>
87
  </div>
 
91
 
92
  return result_html
93
 
94
+
95
  # Gradio interface function
96
+ def recommend_games(game_name, max_age, max_price, min_metacritic):
97
  data = load_data()
98
  if data is None:
99
  return "Failed to load data. Please check the data file."
100
+
101
+ # Apply filters BEFORE feature preparation
 
 
 
 
 
 
 
 
 
 
102
  data = data[
103
  (data['required_age'] <= max_age) &
104
  (data['price'] <= max_price) &
105
+ ((data['metacritic_score'].fillna(0) >= min_metacritic) | data['metacritic_score'].isna())
106
  ].reset_index(drop=True)
107
+
108
  if data.empty:
109
  return "No games found matching your filter criteria."
110
 
 
114
  return recommendations_html
115
 
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # Create the Gradio interface
118
  with gr.Blocks(title="Steam Game Recommender") as demo:
119
+ gr.Markdown("Steam Game Recommender")
120
+ gr.Markdown("Enter a game you like and customize filters to get similar suggestions.")
121
 
122
  with gr.Row():
123
+ input_text = gr.Textbox(label="Favorite Game")
124
 
125
  with gr.Row():
126
  max_age_slider = gr.Slider(0, 21, value=17, label="Max Age Rating (Avoid Adult Games)")
127
  max_price_slider = gr.Slider(0.0, 100.0, value=60.0, step=0.5, label="Maximum Price ($)")
128
+ min_metacritic_slider = gr.Slider(0, 100, value=50, step=1, label="Minimum Metacritic Score")
129
 
130
  with gr.Row():
131
+ submit_btn = gr.Button("Get Recommendations")
132
 
133
  with gr.Row():
134
+ output_text = gr.Markdown(label="Recommendations")
135
 
136
  submit_btn.click(
137
  fn=recommend_games,
138
+ inputs=[input_text, max_age_slider, max_price_slider, min_metacritic_slider],
139
  outputs=output_text
140
  )
141
 
142
+
143
  # Launch the app
144
  if __name__ == "__main__":
145
  demo.launch()