Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,9 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
# Load the data
|
| 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:
|
|
@@ -91,57 +90,52 @@ def get_recommendations(game_name, data, feature_vectors):
|
|
| 91 |
|
| 92 |
return result_html
|
| 93 |
|
| 94 |
-
|
| 95 |
# Gradio interface function
|
| 96 |
-
def recommend_games(game_name):
|
| 97 |
data = load_data()
|
| 98 |
if data is None:
|
| 99 |
-
return "Failed to load data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
|
|
|
|
|
|
| 101 |
feature_vectors = prepare_features(data)
|
| 102 |
recommendations_html = get_recommendations(game_name, data, feature_vectors)
|
| 103 |
|
| 104 |
return recommendations_html
|
| 105 |
|
| 106 |
-
|
| 107 |
-
# Format the output for Gradio
|
| 108 |
-
result_texts = []
|
| 109 |
-
result_images = []
|
| 110 |
-
|
| 111 |
-
for result, image_url in recommendations:
|
| 112 |
-
result_texts.append(result)
|
| 113 |
-
if image_url and str(image_url) != 'nan':
|
| 114 |
-
result_images.append(image_url)
|
| 115 |
-
else:
|
| 116 |
-
# Use a placeholder image if no image URL is available
|
| 117 |
-
result_images.append(None)
|
| 118 |
-
|
| 119 |
-
# Create a gallery of results
|
| 120 |
-
results_html = ""
|
| 121 |
-
for i, (text, img) in enumerate(zip(result_texts, result_images)):
|
| 122 |
-
results_html += text
|
| 123 |
-
|
| 124 |
-
return results_html, result_images
|
| 125 |
-
|
| 126 |
# Create the Gradio interface
|
| 127 |
with gr.Blocks(title="Steam Game Recommender") as demo:
|
| 128 |
-
gr.Markdown("
|
| 129 |
-
gr.Markdown("Enter
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
submit_btn = gr.Button("Get Recommendations")
|
| 134 |
-
|
| 135 |
with gr.Row():
|
| 136 |
output_text = gr.Markdown(label="Recommendations")
|
| 137 |
-
|
| 138 |
submit_btn.click(
|
| 139 |
fn=recommend_games,
|
| 140 |
-
inputs=input_text,
|
| 141 |
outputs=output_text
|
| 142 |
)
|
| 143 |
|
| 144 |
-
|
| 145 |
# Launch the app
|
| 146 |
if __name__ == "__main__":
|
| 147 |
demo.launch()
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# Load the data
|
| 9 |
def load_data():
|
| 10 |
try:
|
|
|
|
| 11 |
data = pd.read_csv('games_march2025_cleaned.csv', nrows=20000, on_bad_lines='skip', engine='python')
|
| 12 |
return data
|
| 13 |
except Exception as e:
|
|
|
|
| 90 |
|
| 91 |
return result_html
|
| 92 |
|
|
|
|
| 93 |
# Gradio interface function
|
| 94 |
+
def recommend_games(game_name, max_age, max_price, min_metacritic):
|
| 95 |
data = load_data()
|
| 96 |
if data is None:
|
| 97 |
+
return "Failed to load data."
|
| 98 |
+
|
| 99 |
+
# Apply filters BEFORE feature preparation
|
| 100 |
+
data = data[
|
| 101 |
+
(data['required_age'] <= max_age) &
|
| 102 |
+
(data['price'] <= max_price) &
|
| 103 |
+
((data['metacritic_score'].fillna(0) >= min_metacritic) | data['metacritic_score'].isna())
|
| 104 |
+
].reset_index(drop=True)
|
| 105 |
|
| 106 |
+
if data.empty:
|
| 107 |
+
return "No games found."
|
| 108 |
+
|
| 109 |
feature_vectors = prepare_features(data)
|
| 110 |
recommendations_html = get_recommendations(game_name, data, feature_vectors)
|
| 111 |
|
| 112 |
return recommendations_html
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# Create the Gradio interface
|
| 115 |
with gr.Blocks(title="Steam Game Recommender") as demo:
|
| 116 |
+
gr.Markdown("Steam Game Recommender")
|
| 117 |
+
gr.Markdown("Enter a game you like and customize filters to get similar suggestions.")
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
input_text = gr.Textbox(label="Input Steam Game")
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
+
max_age_slider = gr.Slider(0, 21, value=17, label="Max Age Rating (Avoid Adult Games)")
|
| 124 |
+
max_price_slider = gr.Slider(0.0, 100.0, value=60.0, step=0.5, label="Maximum Price ($)")
|
| 125 |
+
min_metacritic_slider = gr.Slider(0, 100, value=50, step=1, label="Minimum Metacritic Score")
|
| 126 |
+
|
| 127 |
+
with gr.Row():
|
| 128 |
submit_btn = gr.Button("Get Recommendations")
|
| 129 |
+
|
| 130 |
with gr.Row():
|
| 131 |
output_text = gr.Markdown(label="Recommendations")
|
| 132 |
+
|
| 133 |
submit_btn.click(
|
| 134 |
fn=recommend_games,
|
| 135 |
+
inputs=[input_text, max_age_slider, max_price_slider, min_metacritic_slider],
|
| 136 |
outputs=output_text
|
| 137 |
)
|
| 138 |
|
|
|
|
| 139 |
# Launch the app
|
| 140 |
if __name__ == "__main__":
|
| 141 |
demo.launch()
|