-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
357 lines (269 loc) · 10.6 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# <<--importing libraries which are neede-->
import streamlit as st
import pandas as pd
import pickle
import requests
# <--Function to fetch poster from movie_id-->
def fetch_poster(movie_id):
def get_name(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
poster_path = data['poster_path']
return poster_path
full_path = "https://image.tmdb.org/t/p/w500/" + get_name(movie_id)
return full_path
# <--function to recommend movies with posters-->
def recommend(movie):
index = movies[movies['title'] == movie].index[0]
distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
recommended_movie_names = []
recommended_movie_posters = []
for i in distances[1:17]:
# fetch the movie poster
movie_id = movies.iloc[i[0]].movie_id
recommended_movie_posters.append(fetch_poster(movie_id))
recommended_movie_names.append(movies.iloc[i[0]].title)
return recommended_movie_names, recommended_movie_posters
# <--Header and selectbox to select movie from which our app is going to recommend movies similar to it-->
st.header('🎬MOVIE RECOMMENDATION ENGINE🎬')
movies_dict = pickle.load(open('movie_dict.pkl', 'rb'))
movies = pd.DataFrame(movies_dict)
similarity = pickle.load(open('similarity.pkl', 'rb'))
movie_list = movies['title'].values
selected_movie = st.selectbox(
"Type or select a movie from the dropdown",
movie_list,
key="na_upper"
)
# <--Displaying recommended movies after clicking the button show recommendation-->
if st.button('Show Recommendation'):
recommended_movie_names, recommended_movie_posters = recommend(selected_movie)
# first row of recommended movies
col1, col2, col3, col4 = st.columns(4)
with col1:
st.text(recommended_movie_names[0])
st.image(recommended_movie_posters[0])
with col2:
st.text(recommended_movie_names[1])
st.image(recommended_movie_posters[1])
with col3:
st.text(recommended_movie_names[2])
st.image(recommended_movie_posters[2])
with col4:
st.text(recommended_movie_names[3])
st.image(recommended_movie_posters[3])
# second row of recommended movies
col5, col6, col7, col8 = st.columns(4)
with col5:
st.text(recommended_movie_names[4])
st.image(recommended_movie_posters[4])
with col6:
st.text(recommended_movie_names[5])
st.image(recommended_movie_posters[5])
with col7:
st.text(recommended_movie_names[6])
st.image(recommended_movie_posters[6])
with col8:
st.text(recommended_movie_names[7])
st.image(recommended_movie_posters[7])
# third row of recommended movies
col9, col10, col11, col12 = st.columns(4)
with col9:
st.text(recommended_movie_names[8])
st.image(recommended_movie_posters[8])
with col10:
st.text(recommended_movie_names[9])
st.image(recommended_movie_posters[9])
with col11:
st.text(recommended_movie_names[10])
st.image(recommended_movie_posters[10])
with col12:
st.text(recommended_movie_names[11])
st.image(recommended_movie_posters[11])
# forth row of recommended movies
col13, col14, col15, col16 = st.columns(4)
with col13:
st.text(recommended_movie_names[12])
st.image(recommended_movie_posters[12])
with col14:
st.text(recommended_movie_names[13])
st.image(recommended_movie_posters[13])
with col15:
st.text(recommended_movie_names[14])
st.image(recommended_movie_posters[14])
with col16:
st.text(recommended_movie_names[15])
st.image(recommended_movie_posters[15])
# Displaying Trending movies all over world
st.subheader('TOP RATED MOVIES ON TMDB')
col17, col18, col19, col20 = st.columns(4)
with col17:
st.text("The Godfather")
st.image(fetch_poster(238))
with col18:
st.text("The Shawshank Redemption")
st.image(fetch_poster(278))
with col19:
st.text("The Godfather Part II")
st.image(fetch_poster(240))
with col20:
st.text("Schindler's List ")
st.image(fetch_poster(424))
col21, col22, col23, col24 = st.columns(4)
with col21:
st.text("Dilwale Dulhania Le Jayenge")
st.image(fetch_poster(19404))
with col22:
st.text("12 Angry Men")
st.image(fetch_poster(389))
with col23:
st.text("Spirited Away ")
st.image(fetch_poster(129))
with col24:
st.text("Parasite")
st.image(fetch_poster(496243))
col25, col26, col27, col28 = st.columns(4)
with col25:
st.text("Your Name")
st.image(fetch_poster(372058))
with col26:
st.text("The Dark Knight")
st.image(fetch_poster(155))
with col27:
st.text("The Green Mile")
st.image(fetch_poster(497))
with col28:
st.text("Pulp Fiction")
st.image(fetch_poster(680))
# <--Note of thanks-->
st.info(" 😃 Thank u for visiting movie recommandation engine 😃 ")
st.info(" 📑For more information about above recommended movies check sidebar 📑")
# <--SIDEBAR-->
st.sidebar.header(" 🎥 MORE INFORMATION 🎥 ")
movies_dict = pickle.load(open('movie_dict.pkl', 'rb'))
movies = pd.DataFrame(movies_dict)
similarity = pickle.load(open('similarity.pkl', 'rb'))
st.sidebar.info(
" Wanted to know more information like tagline, overview, runtime, budget and revenue, release date, genre, production companies and more about particular movie that are recommended on Home page ")
movie_list1 = movies['title'].values
slider_selectbox = st.sidebar.selectbox(
"Please Select the movie from below and please click on know more ",
movie_list1,
key="na_lower"
)
# <--fetching all the details needed to display information about the selected movie on slider-->
# fetching movie_id from movie name
id_ = movies[movies['title'] == slider_selectbox]
index = id_.index[0]
id = id_.movie_id[index]
# fetching movie_poster from movie_id
def fetch_poster(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
poster_path = data['poster_path']
full_path = "https://image.tmdb.org/t/p/w500/" + poster_path
return full_path
recommended_movie_posters = []
recommended_movie_posters.append(fetch_poster(id))
# fetching tagline from movie_id
def fetch_tagline(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
tagline = data['tagline']
return tagline
# fetching overview from movie_id
def fetch_overview(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
overview = data['overview']
return overview
# fetching runtime from movie_id
def fetch_runtime(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
runtime = data['runtime']
return runtime
# fetch budget and revenue from movie_id
def fetch_budget(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
budget = data['budget']
revenue = data['revenue']
return budget, revenue
# fetch release date from movie_id
def fetch_release_date(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
date = data['release_date']
return date
# fetching genres from movie_id
def fetch_genre(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
genre = data['genres']
return genre
genres = []
x = len(fetch_genre(id))
for i in range(0, x):
genres.append(fetch_genre(id)[i]['name'])
# fetch production campanies from movie_id
def fetch_production_companies(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
campanies = data['production_companies']
return campanies
production_companies = []
x = len(fetch_production_companies(id))
for i in range(0, x):
production_companies.append(fetch_production_companies(id)[i]['name'])
# fetching homepage from movie_id
def fetch_homepage(movie_id):
url = "https://api.themoviedb.org/3/movie/{}?api_key=12b2f32cf2fae53b46c341e1cf6944a9&language=en-US".format(
movie_id)
data = requests.get(url)
data = data.json()
homepage = data['homepage']
return homepage
# <--Displaying information about the selected movie on slider-->
if st.sidebar.button('know more'):
st.image(recommended_movie_posters[0])
st.subheader("MOVIE NAME")
st.error(slider_selectbox)
st.subheader("MOVIE TAGLINE")
st.error(fetch_tagline(id))
st.subheader("OVERVIEW ")
st.error(fetch_overview(id))
st.subheader("RUNTIME")
st.error(fetch_runtime(id))
st.write("(In Minutes)")
st.subheader("BUDGET AND REVENUE")
st.error(fetch_budget(id))
st.write("(In Rupees)")
st.subheader("RELEASE DATE")
st.error(fetch_release_date(id))
st.subheader("GENRE")
st.error(genres)
st.subheader("PRODUCTION COMPANIES")
st.error(production_companies)
st.subheader("HOMEPAGE")
st.error(
"Follow the link to get more details like Cast, Crew, Movie streaming platform ,Vedios, Movie gallery ,Music and explore more about the movie")
st.write(fetch_homepage(id))