LvMAC commited on
Commit
788d67e
·
verified ·
1 Parent(s): c4d9527

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -19,23 +19,22 @@ def recommend_movies_gradio(user_id, n_recommendations):
19
  user_id = int(user_id)
20
  n_recommendations = int(n_recommendations)
21
  except:
22
- return "Error: Please enter valid numbers for User ID and N"
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', 'genres']],
27
  on='movieId'
28
  )
29
- result = popular_recs[['title_clean', 'year', 'genres', 'weighted_rating']].rename(
30
- columns={'title_clean': 'Title', 'year': 'Year', 'genres': 'Genres', 'weighted_rating': 'Score'}
31
- )
32
- return f"User {user_id} not found. Showing popular movies:\n\n" + result.to_string(index=False)
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
- # 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)
@@ -46,13 +45,13 @@ def recommend_movies_gradio(user_id, n_recommendations):
46
 
47
  predictions_df = pd.DataFrame(predictions)
48
  top_n = predictions_df.nlargest(n_recommendations, 'score')
 
49
 
50
- top_n = top_n.merge(movies[['movieId', 'title_clean', 'year', 'genres']], on='movieId')
51
- result = top_n[['title_clean', 'year', 'genres', 'score']].rename(
52
- columns={'title_clean': 'Title', 'year': 'Year', 'genres': 'Genres', 'score': 'Predicted Rating'}
53
- )
54
 
55
- return result.to_string(index=False)
56
 
57
  iface = gr.Interface(
58
  fn=recommend_movies_gradio,
 
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)
 
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,