Spaces:
Runtime error
Runtime error
Adding Filters
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ def load_data():
|
|
| 17 |
|
| 18 |
# Prepare the feature vectors for similarity calculation
|
| 19 |
def prepare_features(data):
|
| 20 |
-
selected_features = ['genres', 'price', 'average_playtime_2weeks', 'tags'
|
| 21 |
|
| 22 |
for feature in selected_features:
|
| 23 |
data[feature] = data[feature].fillna('')
|
|
@@ -26,8 +26,7 @@ def prepare_features(data):
|
|
| 26 |
data['genres'] + ' ' +
|
| 27 |
data['price'].astype(str) + ' ' +
|
| 28 |
data['average_playtime_2weeks'].astype(str) + ' ' +
|
| 29 |
-
data['tags'].astype(str)
|
| 30 |
-
data['average_playtime_forever'].astype(str)
|
| 31 |
)
|
| 32 |
|
| 33 |
vectorizer = TfidfVectorizer()
|
|
@@ -94,16 +93,25 @@ def get_recommendations(game_name, data, feature_vectors):
|
|
| 94 |
|
| 95 |
|
| 96 |
# Gradio interface function
|
| 97 |
-
def recommend_games(game_name):
|
| 98 |
data = load_data()
|
| 99 |
if data is None:
|
| 100 |
return "Failed to load data. Please check the data file."
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
feature_vectors = prepare_features(data)
|
| 103 |
recommendations_html = get_recommendations(game_name, data, feature_vectors)
|
| 104 |
|
| 105 |
return recommendations_html
|
| 106 |
-
|
| 107 |
|
| 108 |
# Format the output for Gradio
|
| 109 |
result_texts = []
|
|
@@ -126,23 +134,31 @@ def recommend_games(game_name):
|
|
| 126 |
|
| 127 |
# Create the Gradio interface
|
| 128 |
with gr.Blocks(title="Steam Game Recommender") as demo:
|
| 129 |
-
gr.Markdown("# Steam Game Recommender")
|
| 130 |
-
gr.Markdown("Enter
|
| 131 |
-
|
| 132 |
with gr.Row():
|
| 133 |
-
input_text = gr.Textbox(label="
|
| 134 |
-
submit_btn = gr.Button("Get Recommendations")
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
submit_btn.click(
|
| 140 |
fn=recommend_games,
|
| 141 |
-
inputs=input_text,
|
| 142 |
outputs=output_text
|
| 143 |
)
|
| 144 |
|
| 145 |
|
|
|
|
| 146 |
# Launch the app
|
| 147 |
if __name__ == "__main__":
|
| 148 |
demo.launch()
|
|
|
|
| 17 |
|
| 18 |
# Prepare the feature vectors for similarity calculation
|
| 19 |
def prepare_features(data):
|
| 20 |
+
selected_features = ['genres', 'price', 'average_playtime_2weeks', 'tags']
|
| 21 |
|
| 22 |
for feature in selected_features:
|
| 23 |
data[feature] = data[feature].fillna('')
|
|
|
|
| 26 |
data['genres'] + ' ' +
|
| 27 |
data['price'].astype(str) + ' ' +
|
| 28 |
data['average_playtime_2weeks'].astype(str) + ' ' +
|
| 29 |
+
data['tags'].astype(str)
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
vectorizer = TfidfVectorizer()
|
|
|
|
| 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 |
+
|
| 111 |
feature_vectors = prepare_features(data)
|
| 112 |
recommendations_html = get_recommendations(game_name, data, feature_vectors)
|
| 113 |
|
| 114 |
return recommendations_html
|
|
|
|
| 115 |
|
| 116 |
# Format the output for Gradio
|
| 117 |
result_texts = []
|
|
|
|
| 134 |
|
| 135 |
# Create the Gradio interface
|
| 136 |
with gr.Blocks(title="Steam Game Recommender") as demo:
|
| 137 |
+
gr.Markdown("# 🎮 Steam Game Recommender")
|
| 138 |
+
gr.Markdown("Enter a game you like and customize filters to get similar suggestions.")
|
| 139 |
+
|
| 140 |
with gr.Row():
|
| 141 |
+
input_text = gr.Textbox(label="🎯 Favorite Game")
|
|
|
|
| 142 |
|
| 143 |
with gr.Row():
|
| 144 |
+
max_age_slider = gr.Slider(0, 21, value=17, label="Max Age Rating (Avoid Adult Games)")
|
| 145 |
+
max_price_slider = gr.Slider(0.0, 100.0, value=60.0, step=0.5, label="Maximum Price ($)")
|
| 146 |
+
min_metacritic_slider = gr.Slider(0, 100, value=50, step=1, label="Minimum Metacritic Score")
|
| 147 |
+
|
| 148 |
+
with gr.Row():
|
| 149 |
+
submit_btn = gr.Button("🔍 Get Recommendations")
|
| 150 |
+
|
| 151 |
+
with gr.Row():
|
| 152 |
+
output_text = gr.Markdown(label="🧠 Recommendations")
|
| 153 |
+
|
| 154 |
submit_btn.click(
|
| 155 |
fn=recommend_games,
|
| 156 |
+
inputs=[input_text, max_age_slider, max_price_slider, min_metacritic_slider],
|
| 157 |
outputs=output_text
|
| 158 |
)
|
| 159 |
|
| 160 |
|
| 161 |
+
|
| 162 |
# Launch the app
|
| 163 |
if __name__ == "__main__":
|
| 164 |
demo.launch()
|