Spaces:
Runtime error
Runtime error
Radar Added
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ list_of_all_titles = data['name'].tolist()
|
|
| 33 |
def recommend_games(user_game_name_input):
|
| 34 |
find_close_match = difflib.get_close_matches(user_game_name_input, list_of_all_titles, n=1)
|
| 35 |
if not find_close_match:
|
| 36 |
-
return "No close match found. Please try another game name.", None
|
| 37 |
|
| 38 |
closest_match = find_close_match[0]
|
| 39 |
index_of_the_game = data.loc[data['name'] == closest_match].index[0]
|
|
@@ -47,12 +47,14 @@ def recommend_games(user_game_name_input):
|
|
| 47 |
|
| 48 |
recommendations = []
|
| 49 |
chart_data = []
|
|
|
|
| 50 |
for i, (index, score) in enumerate(sorted_similar_games[1:21]):
|
| 51 |
if score < 0.3:
|
| 52 |
continue
|
| 53 |
-
|
| 54 |
game_name = data.iloc[index]['name']
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
os_icons = []
|
| 57 |
if 'windows' in platforms:
|
| 58 |
os_icons.append("๐ช")
|
|
@@ -60,17 +62,21 @@ def recommend_games(user_game_name_input):
|
|
| 60 |
os_icons.append("๐")
|
| 61 |
if 'linux' in platforms or 'steam' in platforms:
|
| 62 |
os_icons.append("๐ง")
|
| 63 |
-
|
| 64 |
os_display = ' '.join(os_icons) if os_icons else "โ"
|
| 65 |
-
|
| 66 |
-
recommendations.append(f"{i+1}. {game_name} {os_display} (Similarity: {
|
| 67 |
-
chart_data.append({'Game': game_name, 'Similarity':
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if len(recommendations) >= 10:
|
| 70 |
break
|
| 71 |
|
| 72 |
chart_df = pd.DataFrame(chart_data)
|
| 73 |
-
return "\n".join(recommendations), chart_df
|
| 74 |
|
| 75 |
# Precision@5
|
| 76 |
def evaluate_precision(user_game_name_input):
|
|
@@ -90,9 +96,32 @@ def evaluate_precision(user_game_name_input):
|
|
| 90 |
hits = sum(1 for genre in top_5 if genre == original_genre)
|
| 91 |
return round(hits / 5, 2)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Combined Gradio function
|
| 94 |
def recommend_and_visualize(user_input):
|
| 95 |
-
recommendations, chart_df = recommend_games(user_input)
|
| 96 |
precision = evaluate_precision(user_input)
|
| 97 |
chart = None
|
| 98 |
|
|
@@ -100,15 +129,17 @@ def recommend_and_visualize(user_input):
|
|
| 100 |
chart = px.bar(chart_df, x="Game", y="Similarity", title="Top Game Recommendations",
|
| 101 |
labels={"Similarity": "Similarity (%)"}, height=400)
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# Gradio UI
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
gr.Markdown("## ๐ฎ Steam Game Recommender")
|
| 108 |
gr.Markdown("Enter the name of a game you like and get recommendations based on similarity!")
|
| 109 |
-
|
| 110 |
-
gr.Markdown("### ๐ป Platform Legend")
|
| 111 |
-
gr.Markdown("- ๐ช = Windows \n- ๐ = macOS \n- ๐ง = Linux/SteamOS \n- โ = Unknown")
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
input_box = gr.Textbox(label="Your Favorite Game", placeholder="e.g., Portal 2")
|
|
@@ -119,6 +150,18 @@ with gr.Blocks() as demo:
|
|
| 119 |
|
| 120 |
run_button = gr.Button("Recommend")
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
demo.launch()
|
|
|
|
| 33 |
def recommend_games(user_game_name_input):
|
| 34 |
find_close_match = difflib.get_close_matches(user_game_name_input, list_of_all_titles, n=1)
|
| 35 |
if not find_close_match:
|
| 36 |
+
return "No close match found. Please try another game name.", None, [], {}
|
| 37 |
|
| 38 |
closest_match = find_close_match[0]
|
| 39 |
index_of_the_game = data.loc[data['name'] == closest_match].index[0]
|
|
|
|
| 47 |
|
| 48 |
recommendations = []
|
| 49 |
chart_data = []
|
| 50 |
+
radar_data = {}
|
| 51 |
for i, (index, score) in enumerate(sorted_similar_games[1:21]):
|
| 52 |
if score < 0.3:
|
| 53 |
continue
|
|
|
|
| 54 |
game_name = data.iloc[index]['name']
|
| 55 |
+
|
| 56 |
+
# Get OS support info
|
| 57 |
+
platforms = data.iloc[index].get('platforms', '').lower()
|
| 58 |
os_icons = []
|
| 59 |
if 'windows' in platforms:
|
| 60 |
os_icons.append("๐ช")
|
|
|
|
| 62 |
os_icons.append("๐")
|
| 63 |
if 'linux' in platforms or 'steam' in platforms:
|
| 64 |
os_icons.append("๐ง")
|
| 65 |
+
|
| 66 |
os_display = ' '.join(os_icons) if os_icons else "โ"
|
| 67 |
+
|
| 68 |
+
recommendations.append(f"{i+1}. {game_name} {os_display} (Similarity: {score*100:.1f}%)")
|
| 69 |
+
chart_data.append({'Game': game_name, 'Similarity': score * 100})
|
| 70 |
+
radar_data[game_name] = {
|
| 71 |
+
"genres": data.iloc[index]['genres'],
|
| 72 |
+
"categories": data.iloc[index]['categories']
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
if len(recommendations) >= 10:
|
| 76 |
break
|
| 77 |
|
| 78 |
chart_df = pd.DataFrame(chart_data)
|
| 79 |
+
return "\n".join(recommendations), chart_df, list(radar_data.keys()), radar_data
|
| 80 |
|
| 81 |
# Precision@5
|
| 82 |
def evaluate_precision(user_game_name_input):
|
|
|
|
| 96 |
hits = sum(1 for genre in top_5 if genre == original_genre)
|
| 97 |
return round(hits / 5, 2)
|
| 98 |
|
| 99 |
+
# Radar plot function
|
| 100 |
+
def plot_game_features(game_name, radar_data):
|
| 101 |
+
if game_name not in radar_data:
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
genres = radar_data[game_name]["genres"].split(';')
|
| 105 |
+
categories = radar_data[game_name]["categories"].split(';')
|
| 106 |
+
features = list(set([g.strip() for g in genres + categories if g.strip()]))
|
| 107 |
+
|
| 108 |
+
if not features:
|
| 109 |
+
return None
|
| 110 |
+
|
| 111 |
+
values = [1] * len(features)
|
| 112 |
+
radar_df = pd.DataFrame(dict(
|
| 113 |
+
Feature=features,
|
| 114 |
+
Presence=values
|
| 115 |
+
))
|
| 116 |
+
|
| 117 |
+
fig = px.line_polar(radar_df, r='Presence', theta='Feature', line_close=True,
|
| 118 |
+
title=f"Feature Radar: {game_name}", range_r=[0, 1])
|
| 119 |
+
fig.update_traces(fill='toself')
|
| 120 |
+
return fig
|
| 121 |
+
|
| 122 |
# Combined Gradio function
|
| 123 |
def recommend_and_visualize(user_input):
|
| 124 |
+
recommendations, chart_df, game_names, radar_data = recommend_games(user_input)
|
| 125 |
precision = evaluate_precision(user_input)
|
| 126 |
chart = None
|
| 127 |
|
|
|
|
| 129 |
chart = px.bar(chart_df, x="Game", y="Similarity", title="Top Game Recommendations",
|
| 130 |
labels={"Similarity": "Similarity (%)"}, height=400)
|
| 131 |
|
| 132 |
+
legend = "\n\n๐ **Platform Legend**\n๐ช = Windows\n๐ = macOS\n๐ง = Linux\nโ = Unknown"
|
| 133 |
+
return recommendations + f"\n\nPrecision@5 (approx): {precision}" + legend, chart, game_names, radar_data
|
| 134 |
+
|
| 135 |
+
# Trigger radar chart
|
| 136 |
+
def show_selected_game_radar(game_name, radar_data):
|
| 137 |
+
return plot_game_features(game_name, radar_data)
|
| 138 |
|
| 139 |
# Gradio UI
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
gr.Markdown("## ๐ฎ Steam Game Recommender")
|
| 142 |
gr.Markdown("Enter the name of a game you like and get recommendations based on similarity!")
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
input_box = gr.Textbox(label="Your Favorite Game", placeholder="e.g., Portal 2")
|
|
|
|
| 150 |
|
| 151 |
run_button = gr.Button("Recommend")
|
| 152 |
|
| 153 |
+
with gr.Row():
|
| 154 |
+
dropdown = gr.Dropdown(label="Inspect a Recommended Game", choices=[], interactive=True)
|
| 155 |
+
radar_chart = gr.Plot(label="Genre/Category Radar")
|
| 156 |
+
|
| 157 |
+
radar_data_state = gr.State()
|
| 158 |
+
|
| 159 |
+
run_button.click(fn=recommend_and_visualize,
|
| 160 |
+
inputs=input_box,
|
| 161 |
+
outputs=[output_text, output_chart, dropdown, radar_data_state])
|
| 162 |
+
|
| 163 |
+
dropdown.change(fn=show_selected_game_radar,
|
| 164 |
+
inputs=[dropdown, radar_data_state],
|
| 165 |
+
outputs=radar_chart)
|
| 166 |
|
| 167 |
demo.launch()
|