MohamedMotaz commited on
Commit
70a2b0f
1 Parent(s): 98edc51

add watch history

Browse files
Files changed (2) hide show
  1. Helpers.py +2 -1
  2. app.py +39 -79
Helpers.py CHANGED
@@ -49,7 +49,8 @@ def get_user_recommendation_XGBoost(all_moves,model, user_id, n=10):
49
  recommendations = user_unseen_movies.sort_values(by='Pred_rating', ascending=False).head(n)['title']
50
  return recommendations ,user_seen_movies
51
 
52
-
 
53
 
54
  def get_user_recommendation(DataBase, Matrix,user_id,l=10):
55
  user = Matrix[user_id]
 
49
  recommendations = user_unseen_movies.sort_values(by='Pred_rating', ascending=False).head(n)['title']
50
  return recommendations ,user_seen_movies
51
 
52
+ def seen_movies(dataBase,user_id):
53
+ return dataBase[dataBase['userId'] == user_id]['title'].values
54
 
55
  def get_user_recommendation(DataBase, Matrix,user_id,l=10):
56
  user = Matrix[user_id]
app.py CHANGED
@@ -3,8 +3,9 @@ import requests
3
  import pandas as pd
4
  import pickle
5
  import gdown
 
6
  import os
7
- from Helpers import get_user_recommendation , train_model , get_user_recommendation_XGBoost ,get_recommendation_item
8
 
9
 
10
  # Set page configuration
@@ -18,13 +19,11 @@ st.markdown(
18
  color: #FFFFFF;
19
  font-family: 'Arial', sans-serif;
20
  }
21
-
22
  .stApp {
23
  background: rgba(0, 0, 0, 0.7);
24
  border-radius: 15px;
25
  padding: 20px;
26
  }
27
-
28
  .title {
29
  font-size: 3em;
30
  text-align: center;
@@ -32,7 +31,6 @@ st.markdown(
32
  font-weight: bold;
33
  color: #FF0000;
34
  }
35
-
36
  .section-title {
37
  font-size: 2em;
38
  margin-top: 30px;
@@ -40,7 +38,6 @@ st.markdown(
40
  text-align: center;
41
  color: #FFD700;
42
  }
43
-
44
  .recommendation {
45
  border: 1px solid #FFD700;
46
  padding: 20px;
@@ -51,12 +48,10 @@ st.markdown(
51
  background-color: rgba(0, 0, 0, 0.8);
52
  overflow: hidden;
53
  }
54
-
55
  .recommendation:hover {
56
  transform: translateY(-10px);
57
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.5);
58
  }
59
-
60
  .recommendation img {
61
  width: 100%;
62
  height: 200px;
@@ -64,47 +59,38 @@ st.markdown(
64
  border-radius: 10px;
65
  margin-bottom: 10px;
66
  }
67
-
68
  .movie-details-container {
69
  display: flex;
70
  align-items: center;
71
  margin-bottom: 20px;
72
  }
73
-
74
  .movie-details-container .movie-poster {
75
  flex: 0 0 auto;
76
  width: 30%;
77
  margin-right: 20px;
78
  }
79
-
80
  .movie-details-container .movie-poster img {
81
  width: 100%;
82
  border-radius: 10px;
83
  }
84
-
85
  .movie-details-container .movie-details {
86
  flex: 1 1 auto;
87
  }
88
-
89
  .movie-details-container .movie-details p {
90
  margin: 5px 0;
91
  }
92
-
93
  a {
94
  color: #FFD700;
95
  text-decoration: none;
96
  }
97
-
98
  a:hover {
99
  text-decoration: underline;
100
  }
101
-
102
  .stSidebar .element-container {
103
  background: rgba(0, 0, 0, 0.7);
104
  border-radius: 15px;
105
  padding: 15px;
106
  }
107
-
108
  .stSidebar .stButton button {
109
  background-color: #FFD700;
110
  color: #000;
@@ -113,7 +99,6 @@ st.markdown(
113
  padding: 10px;
114
  transition: background-color 0.2s, transform 0.2s;
115
  }
116
-
117
  .stSidebar .stButton button:hover {
118
  background-color: #FFAA00;
119
  transform: scale(1.05);
@@ -159,43 +144,33 @@ if not os.path.exists(output_path):
159
 
160
  print("Download completed......")
161
 
 
 
 
 
 
 
 
 
 
 
162
 
163
 
164
- # Dummy data for user recommendations
165
- user_recommendations = {
166
- 1: ["Inception", "The Matrix", "Interstellar"],
167
- 2: ["The Amazing Spider-Man", "District 9", "Titanic"]
168
- }
169
 
170
  # Function to hash passwords
171
  def hash_password(password):
172
  pass
173
 
174
- # Dummy user database
175
- user_db = {
176
- 1: "password123",
177
- 2: "mypassword"
178
- }
179
 
180
  # Login function
181
- def login(username, password):
182
  if isinstance(username, int) and username > 0 and username < 610:
183
  return True
184
  return False
185
 
186
 
187
 
188
-
189
- # Function to fetch movie details from OMDb API
190
- # def fetch_movie_details(title, api_key="23f109b2"):
191
- # url = f"http://www.omdbapi.com/?t={title}&apikey={api_key}"
192
- # response = requests.get(url)
193
- # return response.json()
194
-
195
- # Display movie details
196
-
197
- import re
198
-
199
  def fetch_movie_details(title, api_key_omdb="23f109b2", api_key_tmdb="b8c96e534866701532768a313b978c8b"):
200
  # First, try the OMDb API
201
  title = title[:-7]
@@ -396,8 +371,7 @@ def recommend(movie, similarity_df, movies_df, ratings_df, links_df, k=5):
396
  # Main app
397
 
398
  def main():
399
-
400
- movies_df, ratings_df, links_df , DB_df = load_data()
401
  print("Data loaded successfully")
402
  print("Loading similarity matrix...")
403
  similarity_df = load_similarity_matrix(output_path)
@@ -407,29 +381,38 @@ def main():
407
  choice = st.sidebar.selectbox("Select an option", menu)
408
 
409
  if choice == "Login":
410
- st.title("Movie Recommendations")
411
- st.write("Welcome to the Movie Recommendation App!")
412
- st.write("Please login to get personalized movie recommendations. username between (1 and 800)")
413
- # model selection
414
- C = st.selectbox("Select the model", ["User Similarity Matrix", "XGBoost"])
 
 
 
415
 
416
  # Login form
417
  st.sidebar.header("Login")
418
  username = st.sidebar.text_input("Username")
419
  if username:
420
  username = int(username)
421
- # password = st.sidebar.text_input("Password", type="password")
422
  if st.sidebar.button("Login"):
423
- if login(username, 'password'):
424
  st.sidebar.success("Login successful!")
 
 
 
 
 
 
425
  if C == "User Similarity Matrix":
426
  user_matrix = load_similarity_matrix(user_matrix_path)
427
  recommendations = get_user_recommendation(DB_df, user_matrix, username)
428
  elif C == "XGBoost":
429
- model = train_model(DB_df,username)
430
- recommendations , user_seen_movies = get_user_recommendation_XGBoost(DB_df, model, username)
431
  else:
432
- recommendations = user_recommendations.get(username, [])
 
433
  st.write(f"Recommendations for user number {username}:")
434
  num_cols = 2
435
  cols = st.columns(num_cols)
@@ -456,7 +439,6 @@ def main():
456
  with cols[1]:
457
  st.title("Choosen Movie Details:")
458
  if selected_movie:
459
- # correct_Name = selected_movie[:-7]
460
  movie = fetch_movie_details(selected_movie)
461
  if movie['Response'] == 'True':
462
  display_movie_details(movie)
@@ -465,39 +447,14 @@ def main():
465
  if button:
466
  st.write("The rating bar here is token from our dataset and it's between 0 and 5.")
467
  if selected_movie:
468
- # recommendations = get_recommendation_item(DB_df, similarity_df, selected_movie , k)
469
  recommendations = recommend(selected_movie, similarity_df, movies_df, ratings_df, links_df, k)
470
  if recommendations:
471
  st.write(f"Similar movies to '{selected_movie}':")
472
  num_cols = 2
473
  cols = st.columns(num_cols)
474
-
475
- # movie_id = movies_df[movies_df['title'] == selected_movie]['movieId'].values[0]
476
- # movie_details = get_movie_details(movie_id, movies_df, ratings_df, links_df)
477
- # if movie_details:
478
- # st.markdown(f'<h2 class="section-title">{movie_details["title"]} Details:</h2>', unsafe_allow_html=True)
479
- # st.markdown(
480
- # f"""
481
- # <div class="movie-details-container">
482
- # <div class="movie-poster">
483
- # <img src="{movie_details['poster_url']}" alt="Movie Poster">
484
- # </div>
485
- # <div class="movie-details">
486
- # <p><b>Genres:</b> {', '.join(movie_details['genres'])}</p>
487
- # <p><b>Average Rating:</b> {movie_details['avg_rating']}</p>
488
- # <p><b>Number of Ratings:</b> {movie_details['num_ratings']}</p>
489
- # <p><b>IMDb :</b> <a href="https://www.imdb.com/title/tt{movie_details['imdb_id']:07d}/" target="_blank">movie link</a></p>
490
- # </div>
491
- # </div>
492
- # """,
493
- # unsafe_allow_html=True
494
- # )
495
-
496
-
497
-
498
  for i, movie in enumerate(recommendations):
499
- with cols[i % num_cols]:
500
- print_movie_details(movie)
501
  else:
502
  st.write("No recommendations found.")
503
  else:
@@ -505,3 +462,6 @@ def main():
505
 
506
  if __name__ == "__main__":
507
  main()
 
 
 
 
3
  import pandas as pd
4
  import pickle
5
  import gdown
6
+ import re
7
  import os
8
+ from Helpers import get_user_recommendation , train_model , get_user_recommendation_XGBoost , seen_movies
9
 
10
 
11
  # Set page configuration
 
19
  color: #FFFFFF;
20
  font-family: 'Arial', sans-serif;
21
  }
 
22
  .stApp {
23
  background: rgba(0, 0, 0, 0.7);
24
  border-radius: 15px;
25
  padding: 20px;
26
  }
 
27
  .title {
28
  font-size: 3em;
29
  text-align: center;
 
31
  font-weight: bold;
32
  color: #FF0000;
33
  }
 
34
  .section-title {
35
  font-size: 2em;
36
  margin-top: 30px;
 
38
  text-align: center;
39
  color: #FFD700;
40
  }
 
41
  .recommendation {
42
  border: 1px solid #FFD700;
43
  padding: 20px;
 
48
  background-color: rgba(0, 0, 0, 0.8);
49
  overflow: hidden;
50
  }
 
51
  .recommendation:hover {
52
  transform: translateY(-10px);
53
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.5);
54
  }
 
55
  .recommendation img {
56
  width: 100%;
57
  height: 200px;
 
59
  border-radius: 10px;
60
  margin-bottom: 10px;
61
  }
 
62
  .movie-details-container {
63
  display: flex;
64
  align-items: center;
65
  margin-bottom: 20px;
66
  }
 
67
  .movie-details-container .movie-poster {
68
  flex: 0 0 auto;
69
  width: 30%;
70
  margin-right: 20px;
71
  }
 
72
  .movie-details-container .movie-poster img {
73
  width: 100%;
74
  border-radius: 10px;
75
  }
 
76
  .movie-details-container .movie-details {
77
  flex: 1 1 auto;
78
  }
 
79
  .movie-details-container .movie-details p {
80
  margin: 5px 0;
81
  }
 
82
  a {
83
  color: #FFD700;
84
  text-decoration: none;
85
  }
 
86
  a:hover {
87
  text-decoration: underline;
88
  }
 
89
  .stSidebar .element-container {
90
  background: rgba(0, 0, 0, 0.7);
91
  border-radius: 15px;
92
  padding: 15px;
93
  }
 
94
  .stSidebar .stButton button {
95
  background-color: #FFD700;
96
  color: #000;
 
99
  padding: 10px;
100
  transition: background-color 0.2s, transform 0.2s;
101
  }
 
102
  .stSidebar .stButton button:hover {
103
  background-color: #FFAA00;
104
  transform: scale(1.05);
 
144
 
145
  print("Download completed......")
146
 
147
+ def display_user_history(history):
148
+ st.write("Your Watch History:")
149
+ container = st.container(height=300)
150
+ for movie in history:
151
+ with container:
152
+ container.write(movie)
153
+
154
+
155
+ def get_user_history(dataBase, user_id):
156
+ return dataBase[dataBase['userId'] == user_id]['title'].values
157
 
158
 
 
 
 
 
 
159
 
160
  # Function to hash passwords
161
  def hash_password(password):
162
  pass
163
 
164
+
 
 
 
 
165
 
166
  # Login function
167
+ def login(username, password = None):
168
  if isinstance(username, int) and username > 0 and username < 610:
169
  return True
170
  return False
171
 
172
 
173
 
 
 
 
 
 
 
 
 
 
 
 
174
  def fetch_movie_details(title, api_key_omdb="23f109b2", api_key_tmdb="b8c96e534866701532768a313b978c8b"):
175
  # First, try the OMDb API
176
  title = title[:-7]
 
371
  # Main app
372
 
373
  def main():
374
+ movies_df, ratings_df, links_df, DB_df = load_data()
 
375
  print("Data loaded successfully")
376
  print("Loading similarity matrix...")
377
  similarity_df = load_similarity_matrix(output_path)
 
381
  choice = st.sidebar.selectbox("Select an option", menu)
382
 
383
  if choice == "Login":
384
+ num_cols = 2
385
+ cols = st.columns(num_cols)
386
+ with cols[0]:
387
+ st.title("Movie Recommendations")
388
+ st.write("Welcome to the Movie Recommendation App!")
389
+ st.write("Please login to get personalized movie recommendations. username between (1 and 609)")
390
+ # model selection
391
+ C = st.selectbox("Select the model", ["User Similarity Matrix", "XGBoost"])
392
 
393
  # Login form
394
  st.sidebar.header("Login")
395
  username = st.sidebar.text_input("Username")
396
  if username:
397
  username = int(username)
 
398
  if st.sidebar.button("Login"):
399
+ if login(username):
400
  st.sidebar.success("Login successful!")
401
+
402
+ # Fetch user history
403
+ with cols[1 % num_cols]:
404
+ user_history = get_user_history(DB_df, username)
405
+ display_user_history(user_history)
406
+
407
  if C == "User Similarity Matrix":
408
  user_matrix = load_similarity_matrix(user_matrix_path)
409
  recommendations = get_user_recommendation(DB_df, user_matrix, username)
410
  elif C == "XGBoost":
411
+ model = train_model(DB_df, username)
412
+ recommendations, user_seen_movies = get_user_recommendation_XGBoost(DB_df, model, username)
413
  else:
414
+ pass
415
+
416
  st.write(f"Recommendations for user number {username}:")
417
  num_cols = 2
418
  cols = st.columns(num_cols)
 
439
  with cols[1]:
440
  st.title("Choosen Movie Details:")
441
  if selected_movie:
 
442
  movie = fetch_movie_details(selected_movie)
443
  if movie['Response'] == 'True':
444
  display_movie_details(movie)
 
447
  if button:
448
  st.write("The rating bar here is token from our dataset and it's between 0 and 5.")
449
  if selected_movie:
 
450
  recommendations = recommend(selected_movie, similarity_df, movies_df, ratings_df, links_df, k)
451
  if recommendations:
452
  st.write(f"Similar movies to '{selected_movie}':")
453
  num_cols = 2
454
  cols = st.columns(num_cols)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
455
  for i, movie in enumerate(recommendations):
456
+ with cols[i % num_cols]:
457
+ print_movie_details(movie)
458
  else:
459
  st.write("No recommendations found.")
460
  else:
 
462
 
463
  if __name__ == "__main__":
464
  main()
465
+
466
+
467
+