Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import pickle
|
| 5 |
-
import numpy as np
|
| 6 |
-
from collections import defaultdict
|
| 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 |
-
|
| 13 |
-
movies = metadata['movies_df']
|
| 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)
|
| 21 |
-
except:
|
| 22 |
-
return "Error: Please enter valid numbers"
|
| 23 |
-
|
| 24 |
-
if user_id not in ratings_filtered['userId'].values:
|
| 25 |
-
popular_recs = popular_movies.head(n_recommendations).merge(
|
| 26 |
-
movies[['movieId', 'title_clean', 'year']],
|
| 27 |
-
on='movieId'
|
| 28 |
-
)
|
| 29 |
-
output = f"User {user_id} not found. Showing popular movies:\n\n"
|
| 30 |
-
for i, row in enumerate(popular_recs.itertuples(), 1):
|
| 31 |
-
output += f"{i}. {row.title_clean} ({row.year})\n"
|
| 32 |
-
return output
|
| 33 |
-
|
| 34 |
-
user_ratings = ratings_filtered[ratings_filtered['userId'] == user_id]['movieId'].values
|
| 35 |
-
all_movies = ratings_filtered['movieId'].unique()
|
| 36 |
-
unseen_movies = [m for m in all_movies if m not in user_ratings]
|
| 37 |
-
|
| 38 |
-
predictions = []
|
| 39 |
-
for movie_id in unseen_movies:
|
| 40 |
-
pred = best_svd.predict(user_id, movie_id)
|
| 41 |
-
predictions.append({
|
| 42 |
-
'movieId': movie_id,
|
| 43 |
-
'score': pred.est
|
| 44 |
-
})
|
| 45 |
-
|
| 46 |
-
predictions_df = pd.DataFrame(predictions)
|
| 47 |
-
top_n = predictions_df.nlargest(n_recommendations, 'score')
|
| 48 |
-
top_n = top_n.merge(movies[['movieId', 'title_clean', 'year']], on='movieId')
|
| 49 |
-
|
| 50 |
-
output = f"Recommendations for User {user_id}:\n\n"
|
| 51 |
-
for i, row in enumerate(top_n.itertuples(), 1):
|
| 52 |
-
output += f"{i}. {row.title_clean} ({row.year}) - Rating: {row.score:.2f}\n"
|
| 53 |
-
|
| 54 |
-
return output
|
| 55 |
-
|
| 56 |
-
iface = gr.Interface(
|
| 57 |
-
fn=recommend_movies_gradio,
|
| 58 |
-
inputs=[
|
| 59 |
-
gr.Textbox(label="User ID", placeholder="Enter user ID (e.g., 1, 100, 500)"),
|
| 60 |
-
gr.Slider(minimum=5, maximum=50, value=10, step=5, label="Number of Recommendations")
|
| 61 |
-
],
|
| 62 |
-
outputs=gr.Textbox(label="Recommendations", lines=20),
|
| 63 |
-
title="🎬 Movie Recommendation System - MovieLens",
|
| 64 |
-
description="""
|
| 65 |
-
Get personalized movie recommendations using SVD (Singular Value Decomposition).
|
| 66 |
-
|
| 67 |
-
**Model Performance:**
|
| 68 |
-
- **RMSE**: 0.9338 (best prediction accuracy)
|
| 69 |
-
- **Precision@10**: 0.7968 (79.68% relevant recommendations)
|
| 70 |
-
- **NDCG@10**: 0.8514 (85.14% ranking quality)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
["
|
| 75 |
-
["
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
from collections import defaultdict
|
| 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 |
+
|
| 13 |
+
movies = metadata['movies_df']
|
| 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)
|
| 21 |
+
except:
|
| 22 |
+
return "Error: Please enter valid numbers"
|
| 23 |
+
|
| 24 |
+
if user_id not in ratings_filtered['userId'].values:
|
| 25 |
+
popular_recs = popular_movies.head(n_recommendations).merge(
|
| 26 |
+
movies[['movieId', 'title_clean', 'year']],
|
| 27 |
+
on='movieId'
|
| 28 |
+
)
|
| 29 |
+
output = f"User {user_id} not found. Showing popular movies:\n\n"
|
| 30 |
+
for i, row in enumerate(popular_recs.itertuples(), 1):
|
| 31 |
+
output += f"{i}. {row.title_clean} ({row.year})\n"
|
| 32 |
+
return output
|
| 33 |
+
|
| 34 |
+
user_ratings = ratings_filtered[ratings_filtered['userId'] == user_id]['movieId'].values
|
| 35 |
+
all_movies = ratings_filtered['movieId'].unique()
|
| 36 |
+
unseen_movies = [m for m in all_movies if m not in user_ratings]
|
| 37 |
+
|
| 38 |
+
predictions = []
|
| 39 |
+
for movie_id in unseen_movies:
|
| 40 |
+
pred = best_svd.predict(user_id, movie_id)
|
| 41 |
+
predictions.append({
|
| 42 |
+
'movieId': movie_id,
|
| 43 |
+
'score': pred.est
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
predictions_df = pd.DataFrame(predictions)
|
| 47 |
+
top_n = predictions_df.nlargest(n_recommendations, 'score')
|
| 48 |
+
top_n = top_n.merge(movies[['movieId', 'title_clean', 'year']], on='movieId')
|
| 49 |
+
|
| 50 |
+
output = f"Recommendations for User {user_id}:\n\n"
|
| 51 |
+
for i, row in enumerate(top_n.itertuples(), 1):
|
| 52 |
+
output += f"{i}. {row.title_clean} ({row.year}) - Rating: {row.score:.2f}\n"
|
| 53 |
+
|
| 54 |
+
return output
|
| 55 |
+
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=recommend_movies_gradio,
|
| 58 |
+
inputs=[
|
| 59 |
+
gr.Textbox(label="User ID", placeholder="Enter user ID (e.g., 1, 100, 500)"),
|
| 60 |
+
gr.Slider(minimum=5, maximum=50, value=10, step=5, label="Number of Recommendations")
|
| 61 |
+
],
|
| 62 |
+
outputs=gr.Textbox(label="Recommendations", lines=20),
|
| 63 |
+
title="🎬 Movie Recommendation System - MovieLens",
|
| 64 |
+
description="""
|
| 65 |
+
Get personalized movie recommendations using SVD (Singular Value Decomposition).
|
| 66 |
+
|
| 67 |
+
**Model Performance:**
|
| 68 |
+
- **RMSE**: 0.9338 (best prediction accuracy)
|
| 69 |
+
- **Precision@10**: 0.7968 (79.68% relevant recommendations)
|
| 70 |
+
- **NDCG@10**: 0.8514 (85.14% ranking quality)
|
| 71 |
+
- **Recall@10**: 0.2245 (22.46% of ALL relevant items in just 10 recommendations is solid performance)
|
| 72 |
+
""",
|
| 73 |
+
examples=[
|
| 74 |
+
["1", 10],
|
| 75 |
+
["100", 15],
|
| 76 |
+
["500", 20]
|
| 77 |
+
]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
iface.launch()
|