Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ from collections import defaultdict
|
|
| 7 |
|
| 8 |
with open('best_svd.pkl', 'rb') as f:
|
| 9 |
best_svd = pickle.load(f)
|
| 10 |
-
with open('best_nmf.pkl', 'rb') as f:
|
| 11 |
-
best_nmf = pickle.load(f)
|
| 12 |
with open('model_metadata.pkl', 'rb') as f:
|
| 13 |
metadata = pickle.load(f)
|
| 14 |
|
|
@@ -16,7 +14,7 @@ movies = metadata['movies_df']
|
|
| 16 |
ratings_filtered = metadata['ratings_filtered_df']
|
| 17 |
popular_movies = metadata['popular_movies']
|
| 18 |
|
| 19 |
-
def recommend_movies_gradio(user_id,
|
| 20 |
try:
|
| 21 |
user_id = int(user_id)
|
| 22 |
n_recommendations = int(n_recommendations)
|
|
@@ -37,34 +35,14 @@ def recommend_movies_gradio(user_id, model_choice, n_recommendations):
|
|
| 37 |
all_movies = ratings_filtered['movieId'].unique()
|
| 38 |
unseen_movies = [m for m in all_movies if m not in user_ratings]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
predictions = []
|
| 50 |
-
for movie_id, preds in ensemble_predictions.items():
|
| 51 |
-
predictions.append({
|
| 52 |
-
'movieId': movie_id,
|
| 53 |
-
'score': np.mean(preds)
|
| 54 |
-
})
|
| 55 |
-
else:
|
| 56 |
-
if model_choice == "SVD":
|
| 57 |
-
model = best_svd
|
| 58 |
-
else:
|
| 59 |
-
model = best_nmf
|
| 60 |
-
|
| 61 |
-
predictions = []
|
| 62 |
-
for movie_id in unseen_movies:
|
| 63 |
-
pred = model.predict(user_id, movie_id)
|
| 64 |
-
predictions.append({
|
| 65 |
-
'movieId': movie_id,
|
| 66 |
-
'score': pred.est
|
| 67 |
-
})
|
| 68 |
|
| 69 |
predictions_df = pd.DataFrame(predictions)
|
| 70 |
top_n = predictions_df.nlargest(n_recommendations, 'score')
|
|
@@ -80,27 +58,22 @@ iface = gr.Interface(
|
|
| 80 |
fn=recommend_movies_gradio,
|
| 81 |
inputs=[
|
| 82 |
gr.Textbox(label="User ID", placeholder="Enter user ID (e.g., 1, 100, 500)"),
|
| 83 |
-
gr.Dropdown(
|
| 84 |
-
choices=["Ensemble (SVD + NMF)", "SVD", "NMF"],
|
| 85 |
-
label="Model Selection",
|
| 86 |
-
value="Ensemble (SVD + NMF)"
|
| 87 |
-
),
|
| 88 |
gr.Slider(minimum=5, maximum=50, value=10, step=5, label="Number of Recommendations")
|
| 89 |
],
|
| 90 |
outputs=gr.Textbox(label="Recommendations", lines=20),
|
| 91 |
-
title="
|
| 92 |
description="""
|
| 93 |
-
Get personalized movie recommendations
|
| 94 |
|
| 95 |
-
**
|
| 96 |
-
- **
|
| 97 |
-
- **
|
| 98 |
-
- **
|
| 99 |
""",
|
| 100 |
examples=[
|
| 101 |
-
["1",
|
| 102 |
-
["100",
|
| 103 |
-
["500",
|
| 104 |
]
|
| 105 |
)
|
| 106 |
|
|
|
|
| 7 |
|
| 8 |
with open('best_svd.pkl', 'rb') as f:
|
| 9 |
best_svd = pickle.load(f)
|
|
|
|
|
|
|
| 10 |
with open('model_metadata.pkl', 'rb') as f:
|
| 11 |
metadata = pickle.load(f)
|
| 12 |
|
|
|
|
| 14 |
ratings_filtered = metadata['ratings_filtered_df']
|
| 15 |
popular_movies = metadata['popular_movies']
|
| 16 |
|
| 17 |
+
def recommend_movies_gradio(user_id, n_recommendations):
|
| 18 |
try:
|
| 19 |
user_id = int(user_id)
|
| 20 |
n_recommendations = int(n_recommendations)
|
|
|
|
| 35 |
all_movies = ratings_filtered['movieId'].unique()
|
| 36 |
unseen_movies = [m for m in all_movies if m not in user_ratings]
|
| 37 |
|
| 38 |
+
# Use SVD model (best performing model - RMSE: 0.9338, Precision@10: 0.7968)
|
| 39 |
+
predictions = []
|
| 40 |
+
for movie_id in unseen_movies:
|
| 41 |
+
pred = best_svd.predict(user_id, movie_id)
|
| 42 |
+
predictions.append({
|
| 43 |
+
'movieId': movie_id,
|
| 44 |
+
'score': pred.est
|
| 45 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
predictions_df = pd.DataFrame(predictions)
|
| 48 |
top_n = predictions_df.nlargest(n_recommendations, 'score')
|
|
|
|
| 58 |
fn=recommend_movies_gradio,
|
| 59 |
inputs=[
|
| 60 |
gr.Textbox(label="User ID", placeholder="Enter user ID (e.g., 1, 100, 500)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
gr.Slider(minimum=5, maximum=50, value=10, step=5, label="Number of Recommendations")
|
| 62 |
],
|
| 63 |
outputs=gr.Textbox(label="Recommendations", lines=20),
|
| 64 |
+
title="🎬 Movie Recommendation System - MovieLens",
|
| 65 |
description="""
|
| 66 |
+
Get personalized movie recommendations using SVD (Singular Value Decomposition).
|
| 67 |
|
| 68 |
+
**Model Performance:**
|
| 69 |
+
- **RMSE**: 0.9338 (best prediction accuracy)
|
| 70 |
+
- **Precision@10**: 0.7968 (79.68% relevant recommendations)
|
| 71 |
+
- **NDCG@10**: 0.8514 (85.14% ranking quality)
|
| 72 |
""",
|
| 73 |
examples=[
|
| 74 |
+
["1", 10],
|
| 75 |
+
["100", 15],
|
| 76 |
+
["500", 20]
|
| 77 |
]
|
| 78 |
)
|
| 79 |
|