-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
253 lines (168 loc) · 7.2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from copyreg import pickle
from urllib import request
import streamlit as st
import pickle
import pandas as pd
import numpy as np
from api_ import get_poster_and_safe_mode
from binary_search import binary_search_
st.set_page_config(
page_title="Movie Recommendation System",
page_icon="🎬",
layout="wide",
menu_items={
'Report a bug': "https://github.com/adarshpalaskar1/Movie-Recommendation-System/issues",
'About': "This webapp uses ML algorithms to recommend movies based on your user profile and 2 hundred thousand ratings on 10000 different movies. It uses the TMDB api and the Movie Lens Dataset."
}
)
st.title('Movie Recommender System')
st.subheader('Enter 5 different movies and rate them to create your Profile:')
movie_dict = pickle.load(open('movies.pkl', 'rb'))
movies = pd.DataFrame(movie_dict)
ratings_dict = pickle.load(open('ratings.pkl', 'rb'))
ratings = pd.DataFrame(ratings_dict)
links_dict = pickle.load(open('links.pkl', 'rb'))
links = pd.DataFrame(links_dict)
#movie 1
option1 = st.selectbox(
'1. What is your favourite Movie?',
movies['title'].values)
st.write('You selected:', option1)
rating1 = st.slider('1. How would you rate this movie', 0, 5, 3)
#movie 2
option2 = st.selectbox(
'2. What movie do you hate the most?',
(movies['title'].values))
st.write('You selected:', option2)
rating2 = st.slider('2. How would you rate this movie', 0, 5, 3)
#movie 3
option3 = st.selectbox(
'3. Which movie has most of the Genres you like?',
(movies['title'].values))
st.write('You selected:', option3)
rating3 = st.slider('3. How would you rate this movie', 0, 5, 2)
#movie 4
option4 = st.selectbox(
'4. Which movie has most of the Genres you hate?',
(movies['title'].values))
st.write('You selected:', option4)
rating4 = st.slider('4. How would you rate this movie', 0, 5, 3)
#movie 5
option5 = st.selectbox(
'5. What movie would you recommend to your friends?',
(movies['title'].values))
st.write('You selected:', option5)
rating5 = st.slider('5. How would you rate this movie', 0, 5, 4)
# Copying the movie dataframe into a new one since we won't need to use the genre information in our first case.
moviesWithGenres_df = movies.copy()
# For every row in the dataframe, iterate through the list of genres and place a 1 into the corresponding column
for index, row in movies.iterrows():
for genre in row['genres']:
moviesWithGenres_df.at[index, genre] = 1
# Filling in the NaN values with 0 to show that a movie doesn't have that column's genre
moviesWithGenres_df = moviesWithGenres_df.fillna(0)
child_safe_mode = st.checkbox('Enable child safe mode')
if st.button('Recommend movies for me'):
userInput = [
{'title': option1, 'rating': rating1},
{'title': option2, 'rating': rating2},
{'title': option3, 'rating': rating3},
{'title': option4, 'rating': rating4},
{'title': option5, 'rating': rating5}
]
inputMovies = pd.DataFrame(userInput)
length_set = len({option1, option2, option3, option4, option5})
if(length_set < 5):
st.subheader("Please choose 5 different movies")
else:
st.header('Movies you may like: ')
# Filtering out the movies by title
inputId = movies[movies['title'].isin(inputMovies['title'].tolist())]
# Then merging it so we can get the movieId. It's implicitly merging it by title.
inputMovies = pd.merge(inputId, inputMovies)
# Dropping information we won't use from the input dataframe
inputMovies = inputMovies.drop('genres', 1).drop('year', 1)
# Filtering out the movies from the input
userMovies = moviesWithGenres_df[moviesWithGenres_df['movieId'].isin(inputMovies['movieId'].tolist())]
# Resetting the index to avoid future issues
userMovies = userMovies.reset_index(drop=True)
# Dropping unnecessary issues due to save memory and to avoid issues
userGenreTable = userMovies.drop('movieId', 1).drop('title', 1).drop('genres', 1).drop('year', 1)
# Dot produt to get weights
userProfile = (userGenreTable.transpose()).dot(inputMovies['rating'])
# Removing genres to implement Child Safe Mode
if(child_safe_mode):
userProfile['Crime'] = 0
userProfile['Romance'] = 0
userProfile['War'] = 0
# Now let's get the genres of every movie in our original dataframe
genreTable = moviesWithGenres_df.set_index(moviesWithGenres_df['movieId'])
# And drop the unnecessary information
genreTable = genreTable.drop('movieId', 1).drop('title', 1).drop('genres', 1).drop('year', 1)
recommendationTable_df = ((genreTable*userProfile).sum(axis=1))/(userProfile.sum())
recommendationTable_df = recommendationTable_df.sort_values(ascending=False)
# The final recommendation table
final_table = movies.loc[movies['movieId'].isin(recommendationTable_df.head(50).keys())]
tmdbid = 0
recommended_movies = []
posters = []
child_safe = []
for movieid in final_table['movieId']:
idx = binary_search_(links['movieId'], movieid, 0, 9742)
tmdbid = links.loc[idx][1]
poster , is_safe = get_poster_and_safe_mode(int(tmdbid))
if(poster == False):
continue
if(is_safe == False and child_safe_mode == True):
continue
child_safe.append(is_safe)
posters.append(poster)
recommended_movies.append(final_table.loc[idx][1])
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.text(recommended_movies[0])
st.image(posters[0])
with col2:
st.text(recommended_movies[1])
st.image(posters[1])
with col3:
st.text(recommended_movies[2])
st.image(posters[2])
with col4:
st.text(recommended_movies[3])
st.image(posters[3])
with col5:
st.text(recommended_movies[4])
st.image(posters[4])
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.text(recommended_movies[5])
st.image(posters[5])
with col2:
st.text(recommended_movies[6])
st.image(posters[6])
with col3:
st.text(recommended_movies[7])
st.image(posters[7])
with col4:
st.text(recommended_movies[8])
st.image(posters[8])
with col5:
st.text(recommended_movies[9])
st.image(posters[9])
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.text(recommended_movies[10])
st.image(posters[10])
with col2:
st.text(recommended_movies[11])
st.image(posters[11])
with col3:
st.text(recommended_movies[12])
st.image(posters[12])
with col4:
st.text(recommended_movies[13])
st.image(posters[13])
with col5:
st.text(recommended_movies[14])
st.image(posters[14])