Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Load the data - when deploying, adjust the path to where your dataset will be stored
|
| 9 |
def load_data():
|
|
@@ -56,7 +60,32 @@ def get_recommendations(game_name, data, feature_vectors):
|
|
| 56 |
|
| 57 |
# Get additional information
|
| 58 |
about = data.loc[index, 'about_the_game'] if 'about_the_game' in data.columns else "No description available"
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Get platform information
|
| 62 |
platforms = []
|
|
@@ -68,23 +97,59 @@ def get_recommendations(game_name, data, feature_vectors):
|
|
| 68 |
platforms.append("Linux")
|
| 69 |
platforms_str = ", ".join(platforms) if platforms else "Unknown"
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Format the result
|
| 72 |
-
result = f"**{
|
|
|
|
| 73 |
result += f"**Platforms:** {platforms_str}\n\n"
|
| 74 |
|
| 75 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if about and about != "":
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
-
result += "**About the Game:** No description available\n
|
| 81 |
-
|
| 82 |
-
result += "---\n\n"
|
| 83 |
|
| 84 |
results.append((result, image_url))
|
| 85 |
|
| 86 |
return results
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Gradio interface function
|
| 89 |
def recommend_games(game_name):
|
| 90 |
data = load_data()
|
|
@@ -103,45 +168,99 @@ def recommend_games(game_name):
|
|
| 103 |
|
| 104 |
for result, image_url in recommendations:
|
| 105 |
result_texts.append(result)
|
|
|
|
| 106 |
if image_url and str(image_url) != 'nan':
|
|
|
|
|
|
|
| 107 |
result_images.append(image_url)
|
| 108 |
else:
|
| 109 |
# Use a placeholder image if no image URL is available
|
| 110 |
result_images.append(None)
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
# Create the Gradio interface
|
| 120 |
with gr.Blocks(title="Steam Game Recommender") as demo:
|
| 121 |
gr.Markdown("# Steam Game Recommender")
|
| 122 |
gr.Markdown("Enter your favorite game to get recommendations for similar games.")
|
| 123 |
|
| 124 |
with gr.Row():
|
| 125 |
-
input_text = gr.Textbox(label="Enter your favorite game:")
|
| 126 |
-
submit_btn = gr.Button("Get Recommendations")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
submit_btn.click(
|
| 142 |
-
fn=
|
| 143 |
inputs=input_text,
|
| 144 |
-
outputs=[
|
| 145 |
)
|
| 146 |
|
| 147 |
# Launch the app
|
|
|
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
+
import re
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
import requests
|
| 11 |
|
| 12 |
# Load the data - when deploying, adjust the path to where your dataset will be stored
|
| 13 |
def load_data():
|
|
|
|
| 60 |
|
| 61 |
# Get additional information
|
| 62 |
about = data.loc[index, 'about_the_game'] if 'about_the_game' in data.columns else "No description available"
|
| 63 |
+
|
| 64 |
+
# Try to get an image - first check screenshots, then header_image
|
| 65 |
+
image_url = None
|
| 66 |
+
if 'screenshots' in data.columns and pd.notna(data.loc[index, 'screenshots']):
|
| 67 |
+
# Try to extract the first screenshot URL
|
| 68 |
+
screenshots = data.loc[index, 'screenshots']
|
| 69 |
+
if isinstance(screenshots, str):
|
| 70 |
+
# Handle potential JSON format
|
| 71 |
+
if screenshots.startswith('[') and ']' in screenshots:
|
| 72 |
+
try:
|
| 73 |
+
import json
|
| 74 |
+
screenshot_list = json.loads(screenshots)
|
| 75 |
+
if screenshot_list and isinstance(screenshot_list, list) and len(screenshot_list) > 0:
|
| 76 |
+
if isinstance(screenshot_list[0], dict) and 'path_full' in screenshot_list[0]:
|
| 77 |
+
image_url = screenshot_list[0]['path_full']
|
| 78 |
+
elif isinstance(screenshot_list[0], str):
|
| 79 |
+
image_url = screenshot_list[0]
|
| 80 |
+
except:
|
| 81 |
+
# If JSON parsing fails, try regex
|
| 82 |
+
url_match = re.search(r'https?://[^\s,\'"]+\.(jpg|jpeg|png|gif)', screenshots)
|
| 83 |
+
if url_match:
|
| 84 |
+
image_url = url_match.group(0)
|
| 85 |
+
|
| 86 |
+
# If no screenshot, try header image
|
| 87 |
+
if (image_url is None or image_url == '') and 'header_image' in data.columns:
|
| 88 |
+
image_url = data.loc[index, 'header_image'] if pd.notna(data.loc[index, 'header_image']) else None
|
| 89 |
|
| 90 |
# Get platform information
|
| 91 |
platforms = []
|
|
|
|
| 97 |
platforms.append("Linux")
|
| 98 |
platforms_str = ", ".join(platforms) if platforms else "Unknown"
|
| 99 |
|
| 100 |
+
# Get price information
|
| 101 |
+
price = data.loc[index, 'price'] if 'price' in data.columns else None
|
| 102 |
+
price_str = f"${price}" if pd.notna(price) and price != '' else "Price not available"
|
| 103 |
+
|
| 104 |
# Format the result
|
| 105 |
+
result = f"**{name}**\n\n"
|
| 106 |
+
result += f"**Price:** {price_str}\n"
|
| 107 |
result += f"**Platforms:** {platforms_str}\n\n"
|
| 108 |
|
| 109 |
+
# Add genres if available
|
| 110 |
+
if 'genres' in data.columns and pd.notna(data.loc[index, 'genres']):
|
| 111 |
+
genres = data.loc[index, 'genres']
|
| 112 |
+
if genres and genres != '':
|
| 113 |
+
# Clean up genres format
|
| 114 |
+
if isinstance(genres, str):
|
| 115 |
+
# Handle potential JSON format
|
| 116 |
+
if genres.startswith('[') and ']' in genres:
|
| 117 |
+
try:
|
| 118 |
+
import json
|
| 119 |
+
genres_list = json.loads(genres)
|
| 120 |
+
if isinstance(genres_list, list):
|
| 121 |
+
genres = ", ".join(genres_list)
|
| 122 |
+
except:
|
| 123 |
+
pass
|
| 124 |
+
result += f"**Genres:** {genres}\n\n"
|
| 125 |
+
|
| 126 |
+
# Truncate and clean the about text
|
| 127 |
if about and about != "":
|
| 128 |
+
# Remove HTML tags
|
| 129 |
+
about_clean = re.sub(r'<.*?>', '', about)
|
| 130 |
+
about_truncated = about_clean[:300] + "..." if len(about_clean) > 300 else about_clean
|
| 131 |
+
result += f"**About the Game:** {about_truncated}\n"
|
| 132 |
else:
|
| 133 |
+
result += "**About the Game:** No description available\n"
|
|
|
|
|
|
|
| 134 |
|
| 135 |
results.append((result, image_url))
|
| 136 |
|
| 137 |
return results
|
| 138 |
|
| 139 |
+
# Function to safely load image from URL
|
| 140 |
+
def load_image_safely(url):
|
| 141 |
+
if not url or str(url).lower() == 'nan':
|
| 142 |
+
return None
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
response = requests.get(url, timeout=5)
|
| 146 |
+
if response.status_code == 200:
|
| 147 |
+
return Image.open(BytesIO(response.content))
|
| 148 |
+
else:
|
| 149 |
+
return None
|
| 150 |
+
except:
|
| 151 |
+
return None
|
| 152 |
+
|
| 153 |
# Gradio interface function
|
| 154 |
def recommend_games(game_name):
|
| 155 |
data = load_data()
|
|
|
|
| 168 |
|
| 169 |
for result, image_url in recommendations:
|
| 170 |
result_texts.append(result)
|
| 171 |
+
# Add similarity score if available
|
| 172 |
if image_url and str(image_url) != 'nan':
|
| 173 |
+
# For Hugging Face Spaces, use the URL directly
|
| 174 |
+
# The image loading will happen through the browser
|
| 175 |
result_images.append(image_url)
|
| 176 |
else:
|
| 177 |
# Use a placeholder image if no image URL is available
|
| 178 |
result_images.append(None)
|
| 179 |
|
| 180 |
+
# Return list of recommendations with their info
|
| 181 |
+
return result_texts, result_images
|
| 182 |
+
|
| 183 |
+
# Create the Gradio interface with individual game cards
|
| 184 |
+
def create_recommendation_ui(game_name):
|
| 185 |
+
data = load_data()
|
| 186 |
+
if data is None:
|
| 187 |
+
return [gr.Markdown("Failed to load data. Please check the data file.")]
|
| 188 |
+
|
| 189 |
+
feature_vectors = prepare_features(data)
|
| 190 |
+
recommendations = get_recommendations(game_name, data, feature_vectors)
|
| 191 |
+
|
| 192 |
+
if isinstance(recommendations, str):
|
| 193 |
+
return [gr.Markdown(recommendations)]
|
| 194 |
+
|
| 195 |
+
result_texts, result_images = recommendations
|
| 196 |
+
|
| 197 |
+
# Create output components dynamically
|
| 198 |
+
output_components = []
|
| 199 |
|
| 200 |
+
for i, (text, img_url) in enumerate(zip(result_texts, result_images)):
|
| 201 |
+
with gr.Group():
|
| 202 |
+
with gr.Row():
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
if img_url and str(img_url) != 'nan':
|
| 205 |
+
output_components.append(gr.Image(value=img_url, label=f"Game {i+1}"))
|
| 206 |
+
else:
|
| 207 |
+
output_components.append(gr.Markdown("*No image available*"))
|
| 208 |
+
with gr.Column(scale=2):
|
| 209 |
+
output_components.append(gr.Markdown(text))
|
| 210 |
+
output_components.append(gr.Markdown("---"))
|
| 211 |
+
|
| 212 |
+
return output_components
|
| 213 |
|
|
|
|
| 214 |
with gr.Blocks(title="Steam Game Recommender") as demo:
|
| 215 |
gr.Markdown("# Steam Game Recommender")
|
| 216 |
gr.Markdown("Enter your favorite game to get recommendations for similar games.")
|
| 217 |
|
| 218 |
with gr.Row():
|
| 219 |
+
input_text = gr.Textbox(label="Enter your favorite game:", placeholder="e.g., Half-Life 2")
|
| 220 |
+
submit_btn = gr.Button("Get Recommendations", variant="primary")
|
| 221 |
|
| 222 |
+
output_container = gr.Group(visible=False)
|
| 223 |
+
with output_container:
|
| 224 |
+
gr.Markdown("## Your Recommendations")
|
| 225 |
+
recommendation_outputs = []
|
| 226 |
+
for i in range(9): # For 9 recommendations
|
| 227 |
+
with gr.Group():
|
| 228 |
+
with gr.Row():
|
| 229 |
+
with gr.Column(scale=1):
|
| 230 |
+
recommendation_outputs.append(gr.Image(label=f"Game {i+1}"))
|
| 231 |
+
with gr.Column(scale=2):
|
| 232 |
+
recommendation_outputs.append(gr.Markdown())
|
| 233 |
+
recommendation_outputs.append(gr.Markdown("---"))
|
| 234 |
|
| 235 |
+
def process_recommendations(game_name):
|
| 236 |
+
data = load_data()
|
| 237 |
+
if data is None:
|
| 238 |
+
return [gr.update(visible=True), gr.update(value="Failed to load data. Please check the data file.")]
|
| 239 |
+
|
| 240 |
+
feature_vectors = prepare_features(data)
|
| 241 |
+
recommendations = get_recommendations(game_name, data, feature_vectors)
|
| 242 |
+
|
| 243 |
+
if isinstance(recommendations, str):
|
| 244 |
+
return [gr.update(visible=True), gr.update(value=recommendations)]
|
| 245 |
+
|
| 246 |
+
result_texts, result_images = recommendations
|
| 247 |
+
updates = [gr.update(visible=True)]
|
| 248 |
+
|
| 249 |
+
for i, (text, img_url) in enumerate(zip(result_texts, result_images)):
|
| 250 |
+
updates.append(gr.update(value=img_url if img_url and str(img_url) != 'nan' else None))
|
| 251 |
+
updates.append(gr.update(value=text))
|
| 252 |
+
updates.append(gr.update())
|
| 253 |
+
|
| 254 |
+
# Fill any remaining slots with empty updates
|
| 255 |
+
while len(updates) < len(recommendation_outputs) + 1:
|
| 256 |
+
updates.append(gr.update(visible=False))
|
| 257 |
+
|
| 258 |
+
return updates
|
| 259 |
|
| 260 |
submit_btn.click(
|
| 261 |
+
fn=process_recommendations,
|
| 262 |
inputs=input_text,
|
| 263 |
+
outputs=[output_container] + recommendation_outputs
|
| 264 |
)
|
| 265 |
|
| 266 |
# Launch the app
|