Spaces:
Sleeping
Sleeping
File size: 21,094 Bytes
842bb52 4f11e18 842bb52 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | import gradio as gr
import pandas as pd
import numpy as np
import pickle
import os
from comprehensive_recommendation_system import ComprehensiveRecommendationSystem
import warnings
warnings.filterwarnings('ignore')
rec_system = None
def load_recommendation_system():
"""Load the trained recommendation system"""
global rec_system
if rec_system is None:
print("Loading comprehensive recommendation system...")
rec_system = ComprehensiveRecommendationSystem()
rec_system.load_data("u.data", "u.item")
if os.path.exists("comprehensive_recommendation_model.pkl"):
print("Loading pre-trained model...")
rec_system.load_model("comprehensive_recommendation_model.pkl")
else:
print("Training models...")
rec_system.train_all_models()
rec_system.save_model("comprehensive_recommendation_model.pkl")
print("Recommendation system loaded successfully!")
return rec_system
def get_available_users():
"""Get list of available users"""
if rec_system is None:
return []
return sorted(rec_system.ratings['user_id'].unique().tolist())
def get_user_stats():
"""Get user statistics"""
if rec_system is None:
return "System not loaded"
total_users = rec_system.ratings['user_id'].nunique()
total_movies = rec_system.movies['movie_id'].nunique()
total_ratings = len(rec_system.ratings)
avg_rating = rec_system.ratings['rating'].mean()
return f"""
**Dataset Statistics:**
- **Users:** {total_users:,}
- **Movies:** {total_movies:,}
- **Ratings:** {total_ratings:,}
- **Average Rating:** {avg_rating:.2f}/5.0
- **Sparsity:** 93.70%
"""
def get_user_info(user_id):
"""Get detailed information about a user"""
if rec_system is None:
return "System not loaded"
if user_id is None:
return "Please select a user"
user_ratings = rec_system.ratings[rec_system.ratings['user_id'] == user_id]
if len(user_ratings) == 0:
return f"User {user_id} not found in dataset"
# Get user's rating statistics
avg_rating = user_ratings['rating'].mean()
num_ratings = len(user_ratings)
rating_range = f"{user_ratings['rating'].min()} - {user_ratings['rating'].max()}"
# Get rating distribution
rating_dist = user_ratings['rating'].value_counts().sort_index()
# Get user's top-rated movies
top_movies = user_ratings.merge(
rec_system.movies[['movie_id', 'title']],
on='movie_id'
).sort_values('rating', ascending=False).head(10)
info = f"""
## π€ User {user_id} Profile
### π Rating Statistics
- **Total Ratings:** {num_ratings:,}
- **Average Rating:** {avg_rating:.2f}/5.0
- **Rating Range:** {rating_range}
### π Rating Distribution
"""
for rating, count in rating_dist.items():
percentage = (count / num_ratings) * 100
info += f"- **{rating} stars:** {count} ratings ({percentage:.1f}%)\n"
info += f"""
### π¬ Top 10 Rated Movies
"""
for i, (_, movie) in enumerate(top_movies.iterrows(), 1):
stars = "β" * int(movie['rating'])
info += f"{i:2d}. {movie['title']} {stars} ({movie['rating']})\n"
return info
def recommend_movies(user_id, num_recommendations, method):
"""Generate movie recommendations"""
if rec_system is None:
return "Error: Recommendation system not loaded"
if user_id is None:
return "Please select a user"
try:
recommendations = rec_system.recommend_movies(
user_id=int(user_id),
N=int(num_recommendations),
method=method
)
if len(recommendations) == 0:
return "No recommendations available for this user"
method_names = {
"user_based": "π₯ User-based Collaborative Filtering",
"item_based": "π¬ Item-based Collaborative Filtering",
"svd": "π SVD Matrix Factorization",
"neural": "π§ Neural Embeddings",
"ensemble": "π― Ensemble Method"
}
result = f"## π¬ Top {len(recommendations)} Movie Recommendations\n\n"
result += f"**Method:** {method_names.get(method, method.upper())}\n"
result += f"**User:** {user_id}\n\n"
for i, (_, rec) in enumerate(recommendations.iterrows(), 1):
rating_emoji = "β" * int(round(rec['predicted_rating']))
result += f"**{i:2d}.** {rec['title']}\n"
result += f" {rating_emoji} Predicted Rating: {rec['predicted_rating']:.3f}\n\n"
return result
except Exception as e:
return f"β Error generating recommendations: {str(e)}"
def compare_all_methods(user_id, num_recommendations):
"""Compare all recommendation methods"""
if rec_system is None:
return "Error: Recommendation system not loaded"
if user_id is None:
return "Please select a user"
methods = ["user_based", "item_based", "svd", "neural", "ensemble"]
results = {}
try:
for method in methods:
recommendations = rec_system.recommend_movies(
user_id=int(user_id),
N=int(num_recommendations),
method=method
)
results[method] = recommendations
result = f"## βοΈ Method Comparison for User {user_id}\n\n"
result += f"**Number of recommendations:** {num_recommendations}\n\n"
method_names = {
"user_based": "π₯ User-based CF",
"item_based": "π¬ Item-based CF",
"svd": "π SVD",
"neural": "π§ Neural",
"ensemble": "π― Ensemble"
}
for method, recs in results.items():
result += f"### {method_names[method]}\n"
if len(recs) == 0:
result += "No recommendations available\n\n"
else:
for i, (_, rec) in enumerate(recs.iterrows(), 1):
rating_emoji = "β" * int(round(rec['predicted_rating']))
result += f"{i:2d}. {rec['title']} {rating_emoji} ({rec['predicted_rating']:.3f})\n"
result += "\n"
return result
except Exception as e:
return f"β Error comparing methods: {str(e)}"
def evaluate_system_performance():
"""Evaluate the recommendation system performance"""
if rec_system is None:
return "Error: Recommendation system not loaded"
try:
all_users = rec_system.ratings['user_id'].unique()
test_users = np.random.choice(all_users, min(15, len(all_users)), replace=False)
methods = ["user_based", "item_based", "svd", "neural", "ensemble"]
results = {}
for method in methods:
eval_results = rec_system.evaluate_model(test_users, method=method, N=10)
results[method] = eval_results
result = "## π System Performance Evaluation\n\n"
result += f"**Evaluated on:** {len(test_users)} users\n"
result += f"**Recommendations per user:** 10\n\n"
result += "| Method | Precision@10 | Recall@10 | NDCG@10 |\n"
result += "|--------|--------------|-----------|----------|\n"
for method, eval_results in results.items():
method_emoji = {"user_based": "π₯", "item_based": "π¬", "svd": "π", "neural": "π§ ", "ensemble": "π―"}
emoji = method_emoji.get(method, "π")
result += f"| {emoji} {method:12} | {eval_results['precision']:.4f} | {eval_results['recall']:.4f} | {eval_results['ndcg']:.4f} |\n"
best_method = max(results.keys(), key=lambda x: results[x]['ndcg'])
best_emoji = {"user_based": "π₯", "item_based": "π¬", "svd": "π", "neural": "π§ ", "ensemble": "π―"}.get(best_method, "π")
result += f"\n### π Best Performing Method\n"
result += f"**{best_emoji} {best_method.upper()}** with NDCG@10: {results[best_method]['ndcg']:.4f}\n\n"
result += "### π Metric Explanations\n"
result += "- **Precision@10:** Fraction of recommended items that are relevant\n"
result += "- **Recall@10:** Fraction of relevant items that are recommended\n"
result += "- **NDCG@10:** Normalized Discounted Cumulative Gain (ranking quality)\n"
return result
except Exception as e:
return f"β Error evaluating system: {str(e)}"
def get_method_explanations():
"""Get detailed explanations of each recommendation method"""
return """
## π¬ Recommendation Methods Explained
### π₯ User-based Collaborative Filtering
**How it works:** Finds users with similar rating patterns and recommends movies they liked.
**Strengths:**
- Intuitive approach that mimics human behavior
- Good for finding diverse recommendations
- Effective when user base is large
**Best for:** Users who want to discover new genres and diverse content
---
### π¬ Item-based Collaborative Filtering
**How it works:** Finds movies with similar rating patterns and recommends items similar to those a user has liked.
**Strengths:**
- More stable than user-based approach
- Computationally efficient
- Good for popular items
**Best for:** Users who want recommendations similar to movies they already like
---
### π SVD Matrix Factorization
**How it works:** Decomposes the user-item matrix into latent factors representing user preferences and item characteristics.
**Strengths:**
- Handles sparsity well
- Captures implicit relationships
- Computationally efficient
**Best for:** Balanced performance with good generalization
---
### π§ Neural Embeddings
**How it works:** Uses deep learning to learn complex non-linear relationships between users and items.
**Strengths:**
- Can capture complex patterns
- Flexible architecture
- Good for non-linear relationships
**Best for:** Advanced pattern recognition and complex user behaviors
---
### π― Ensemble Method
**How it works:** Combines predictions from all four approaches by averaging their recommendation scores.
**Strengths:**
- Reduces individual method biases
- More robust predictions
- Better coverage of different patterns
**Best for:** Optimal overall performance and reliability
"""
def create_interface():
"""Create the enhanced Gradio interface"""
load_recommendation_system()
with gr.Blocks(
title="π¬ Comprehensive Movie Recommendation System",
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="purple",
neutral_hue="slate"
),
css="""
.main-header {
text-align: center;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 2.5em;
font-weight: bold;
margin-bottom: 20px;
}
.method-card {
border: 2px solid #333333;
border-radius: 12px;
padding: 20px;
margin: 15px 0;
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
color: white;
}
.stats-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
border-radius: 12px;
margin: 10px 0;
}
.footer-text {
color: white !important;
}
.footer-text h2, .footer-text h3 {
color: white !important;
}
.footer-text p, .footer-text li {
color: #e0e0e0 !important;
}
"""
) as app:
gr.Markdown(
"""
# π¬ Comprehensive Movie Recommendation System
**Advanced ML Approaches for Personalized Recommendations**
This system implements multiple state-of-the-art recommendation approaches including collaborative filtering,
matrix factorization, and neural embeddings using the MovieLens 100K dataset.
""",
elem_classes="main-header"
)
# Stats overview
with gr.Row():
with gr.Column():
stats_display = gr.Markdown(
get_user_stats(),
elem_classes="stats-card"
)
# Main tabs
with gr.Tabs():
# Recommendation Tab
with gr.Tab("π― Get Recommendations", id="recommendations"):
gr.Markdown("### Generate personalized movie recommendations using different ML approaches")
with gr.Row():
with gr.Column(scale=1):
user_dropdown = gr.Dropdown(
choices=get_available_users(),
label="π€ Select User ID",
value=get_available_users()[0] if get_available_users() else None,
info="Choose from 943 available users"
)
num_recommendations = gr.Slider(
minimum=5,
maximum=20,
value=10,
step=1,
label="π Number of Recommendations",
info="How many movies to recommend"
)
method_dropdown = gr.Dropdown(
choices=[
("π― Ensemble Method (Recommended)", "ensemble"),
("π₯ User-based Collaborative Filtering", "user_based"),
("π¬ Item-based Collaborative Filtering", "item_based"),
("π SVD Matrix Factorization", "svd"),
("π§ Neural Embeddings", "neural")
],
value="ensemble",
label="π¬ Recommendation Method",
info="Choose the ML approach for recommendations"
)
recommend_btn = gr.Button("π Get Recommendations", variant="primary", size="lg")
with gr.Column(scale=2):
recommendations_output = gr.Markdown(
"π Select a user and method to get personalized movie recommendations...",
label="π¬ Recommendations"
)
recommend_btn.click(
fn=recommend_movies,
inputs=[user_dropdown, num_recommendations, method_dropdown],
outputs=recommendations_output
)
# User Analysis Tab
with gr.Tab("π€ User Analysis", id="analysis"):
gr.Markdown("### Analyze user preferences, rating patterns, and movie tastes")
with gr.Row():
with gr.Column(scale=1):
user_analysis_dropdown = gr.Dropdown(
choices=get_available_users(),
label="π€ Select User ID",
value=get_available_users()[0] if get_available_users() else None,
info="Choose a user to analyze"
)
analyze_user_btn = gr.Button("π Analyze User", variant="primary", size="lg")
with gr.Column(scale=2):
user_info_output = gr.Markdown(
"π Select a user to analyze their preferences and rating patterns...",
label="π User Profile"
)
analyze_user_btn.click(
fn=get_user_info,
inputs=[user_analysis_dropdown],
outputs=user_info_output
)
# Method Comparison Tab
with gr.Tab("βοΈ Compare Methods", id="comparison"):
gr.Markdown("### Compare all recommendation methods side-by-side for a user")
with gr.Row():
with gr.Column(scale=1):
compare_user_dropdown = gr.Dropdown(
choices=get_available_users(),
label="π€ Select User ID",
value=get_available_users()[0] if get_available_users() else None,
info="Choose a user to compare methods for"
)
compare_num_recs = gr.Slider(
minimum=5,
maximum=15,
value=10,
step=1,
label="π Number of Recommendations",
info="How many recommendations per method"
)
compare_btn = gr.Button("βοΈ Compare All Methods", variant="primary", size="lg")
with gr.Column(scale=2):
comparison_output = gr.Markdown(
"π Select a user to compare all recommendation methods...",
label="π Method Comparison"
)
compare_btn.click(
fn=compare_all_methods,
inputs=[compare_user_dropdown, compare_num_recs],
outputs=comparison_output
)
with gr.Tab("π System Evaluation", id="evaluation"):
gr.Markdown("### Evaluate system performance with comprehensive metrics")
with gr.Row():
with gr.Column(scale=1):
evaluate_btn = gr.Button("π Evaluate System Performance", variant="primary", size="lg")
with gr.Column(scale=2):
evaluation_output = gr.Markdown(
"π Click 'Evaluate System Performance' to see detailed metrics...",
label="π Performance Results"
)
evaluate_btn.click(
fn=evaluate_system_performance,
inputs=[],
outputs=evaluation_output
)
# Method Explanations Tab
with gr.Tab("π¬ Method Explanations", id="explanations"):
gr.Markdown("### Learn about each recommendation method and when to use them")
explanations_output = gr.Markdown(
get_method_explanations(),
label="π Method Details"
)
gr.Markdown(
"""
---
## π About This System
This comprehensive recommendation system demonstrates multiple state-of-the-art approaches:
- **π₯ Collaborative Filtering**: Both user-based and item-based methods
- **π Matrix Factorization**: SVD for dimensionality reduction
- **π§ Neural Networks**: Deep learning embeddings for complex patterns
- **π― Ensemble Methods**: Combining multiple approaches for optimal performance
### π Evaluation Metrics
- **Precision@K**: Fraction of recommended items that are relevant
- **Recall@K**: Fraction of relevant items that are recommended
- **NDCG@K**: Normalized Discounted Cumulative Gain for ranking quality
### π Educational Value
Perfect for learning about recommendation systems, machine learning approaches,
and interactive web application development.
""",
elem_classes="method-card footer-text"
)
return app
if __name__ == "__main__":
app = create_interface()
app.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True
)
|