Gary0417 commited on
Commit
e5452dc
1 Parent(s): 973646d

Update docstrings and type hinting

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -9,7 +9,6 @@ from sentence_transformers import SentenceTransformer
9
  from sklearn.metrics.pairwise import cosine_similarity
10
 
11
 
12
-
13
  def load_model() -> SentenceTransformer:
14
  """
15
  Loads a pre-trained SentenceTransformer model.
@@ -25,7 +24,9 @@ def load_model() -> SentenceTransformer:
25
  return model
26
 
27
 
28
- def encode_and_calculate_similarity(model: SentenceTransformer, df_merged: pd.DataFrame) -> np.ndarray:
 
 
29
  """
30
  Encodes sentences using the provided SentenceTransformer model and calculates cosine similarity.
31
 
@@ -59,7 +60,7 @@ def svd(df_ratings: pd.DataFrame) -> SVD:
59
 
60
  def get_sorted_similar_movies(
61
  title: str, cos_sim: np.ndarray, df_merged: pd.DataFrame
62
- ) -> list[int]:
63
  """
64
  Get a sorted DataFrame of movies based on their similarity scores to a given movie.
65
 
@@ -151,19 +152,25 @@ def predict_user_rating(
151
  return final_qualified_movies
152
 
153
 
154
- def get_movie_recommendations_hybrid(title: str, user_id: int) -> pd.DataFrame:
 
 
155
  """
156
  Get movie recommendations based on a given title and user ID.
157
 
158
  :param title: The title of the movie to find similar movies for.
159
  :param userId: The ID of the user.
160
- :return: A Pandas DataFrame containing the recommended movies
 
 
161
  """
162
  # Get recommended movie indices based on the given title
163
  sorted_similar_movies = get_sorted_similar_movies(title, cos_sim, df_merged)
164
 
165
- # Filter out bad movies and select the top 50 qualified movies
166
- qualified_movies = get_qualified_movies(df_qualified, sorted_similar_movies).head(50)
 
 
167
 
168
  # Predict user ratings for qualified movies and select the top recommended movies
169
  recommended_movies = predict_user_rating(
@@ -183,8 +190,14 @@ def get_movie_recommendations_hybrid(title: str, user_id: int) -> pd.DataFrame:
183
  "Predicted User Rating",
184
  ]
185
 
186
- recommendation_criteria = recommended_movies[["ID", "Title", "Predicted User Rating", "Similarity Score", "Weighted Rating"]]
187
- recommended_movies.drop(["Predicted User Rating", "Similarity Score", "Weighted Rating"], axis = 1, inplace=True)
 
 
 
 
 
 
188
  return recommended_movies, recommendation_criteria
189
 
190
 
@@ -222,7 +235,7 @@ if __name__ == "__main__":
222
  recommend_button.click(
223
  get_movie_recommendations_hybrid,
224
  inputs=[title, user_id],
225
- outputs=[recommended_movies, recommendation_criteria]
226
  )
227
  examples = gr.Examples(
228
  examples=[
 
9
  from sklearn.metrics.pairwise import cosine_similarity
10
 
11
 
 
12
  def load_model() -> SentenceTransformer:
13
  """
14
  Loads a pre-trained SentenceTransformer model.
 
24
  return model
25
 
26
 
27
+ def encode_and_calculate_similarity(
28
+ model: SentenceTransformer, df_merged: pd.DataFrame
29
+ ) -> np.ndarray:
30
  """
31
  Encodes sentences using the provided SentenceTransformer model and calculates cosine similarity.
32
 
 
60
 
61
  def get_sorted_similar_movies(
62
  title: str, cos_sim: np.ndarray, df_merged: pd.DataFrame
63
+ ) -> pd.DataFrame:
64
  """
65
  Get a sorted DataFrame of movies based on their similarity scores to a given movie.
66
 
 
152
  return final_qualified_movies
153
 
154
 
155
+ def get_movie_recommendations_hybrid(
156
+ title: str, user_id: int
157
+ ) -> tuple[pd.DataFrame, pd.DataFrame]:
158
  """
159
  Get movie recommendations based on a given title and user ID.
160
 
161
  :param title: The title of the movie to find similar movies for.
162
  :param userId: The ID of the user.
163
+ :return: A tuple of two Pandas DataFrames.
164
+ The first DataFrame contains the recommended movies.
165
+ The second DataFrame contains the recommendation criteria (ID, Title, Predicted User Rating, Similarity Score, Weighted Rating).
166
  """
167
  # Get recommended movie indices based on the given title
168
  sorted_similar_movies = get_sorted_similar_movies(title, cos_sim, df_merged)
169
 
170
+ # Filter out bad movies and select the top 50 qualified movies
171
+ qualified_movies = get_qualified_movies(df_qualified, sorted_similar_movies).head(
172
+ 50
173
+ )
174
 
175
  # Predict user ratings for qualified movies and select the top recommended movies
176
  recommended_movies = predict_user_rating(
 
190
  "Predicted User Rating",
191
  ]
192
 
193
+ recommendation_criteria = recommended_movies[
194
+ ["ID", "Title", "Predicted User Rating", "Similarity Score", "Weighted Rating"]
195
+ ]
196
+ recommended_movies.drop(
197
+ ["Predicted User Rating", "Similarity Score", "Weighted Rating"],
198
+ axis=1,
199
+ inplace=True,
200
+ )
201
  return recommended_movies, recommendation_criteria
202
 
203
 
 
235
  recommend_button.click(
236
  get_movie_recommendations_hybrid,
237
  inputs=[title, user_id],
238
+ outputs=[recommended_movies, recommendation_criteria],
239
  )
240
  examples = gr.Examples(
241
  examples=[