Shrikrishna commited on
Commit
e34a46c
·
1 Parent(s): b56f349

Upload 6 files

Browse files
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import requests
4
+ import pandas as pd
5
+
6
+ footer="""<style>
7
+ a:link , a:visited{
8
+ color: black;
9
+ background-color: transparent;
10
+ }
11
+
12
+ a:hover, a:active {
13
+ color: red;
14
+ background-color: transparent;
15
+ }
16
+
17
+ .footer {
18
+ position: fixed;
19
+ left: 0;
20
+ bottom: 0;
21
+ width: 100%;
22
+ background-color: white;
23
+ color: black;
24
+ text-align: center;
25
+ }
26
+ </style>
27
+ <div class="footer">
28
+ <p>Developed with <span style ='color:red'>❤</span> by <a href="https://shrikrishnaparab.tech/" target="_blank">Shrikrishna Parab</a></p>
29
+ </div>
30
+ """
31
+
32
+ def fetch_poster(movie_id):
33
+ url = "https://api.themoviedb.org/3/movie/{}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US".format(movie_id)
34
+ data = requests.get(url)
35
+ data = data.json()
36
+ poster_path = data['poster_path']
37
+ #homepage_path = data['homepage']
38
+ full_path = "https://image.tmdb.org/t/p/w500/" + poster_path
39
+ return full_path
40
+
41
+ def get_popular(qualified):
42
+ #print(qualified.head())
43
+ top_5 = qualified.head(5)
44
+ return top_5
45
+
46
+
47
+ def top_genre_based_movies(genre, percentile=0.95):
48
+ df = genre_df[genre_df['genres'].str.contains(genre)]
49
+ vote_counts = df['vote_count'].astype('int')
50
+ vote_averages = df['vote_average'].astype('int')
51
+ C = vote_averages.mean()
52
+ m = vote_counts.quantile(percentile)
53
+ qualified = df[(df['vote_count'] >= m)][['movie_id', 'title', 'vote_count', 'vote_average', 'genres']]
54
+ qualified['vote_count'] = qualified['vote_count'].astype('int')
55
+ qualified['vote_average'] = qualified['vote_average'].astype('int')
56
+ qualified['wr'] = qualified.apply(
57
+ lambda x: (x['vote_count'] / (x['vote_count'] + m) * x['vote_average']) + (m / (m + x['vote_count']) * C),
58
+ axis=1)
59
+ qualified = qualified.sort_values('wr', ascending=False).head(250)
60
+ return qualified
61
+
62
+ def recommend(movie):
63
+ index = movies[movies['title'] == movie].index[0]
64
+ distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
65
+ recommended_movie_names = []
66
+ recommended_movie_posters = []
67
+ homepage_path_list = []
68
+ for i in distances[1:6]:
69
+ # fetch the movie poster
70
+ movie_id = movies.iloc[i[0]].movie_id
71
+ full_path= fetch_poster(movie_id)
72
+ #homepage_path_list.append(homepage_path)
73
+ recommended_movie_posters.append(full_path)
74
+ recommended_movie_names.append(movies.iloc[i[0]].title)
75
+
76
+ return recommended_movie_names,recommended_movie_posters
77
+
78
+
79
+ st.title("Movie Recommender System")
80
+
81
+ movies = pickle.load(open('movie_list_hi.pkl','rb'))
82
+ similarity = pickle.load(open('similarity_hi.pkl','rb'))
83
+ all_movies = pickle.load(open('movies_df_hi.pkl','rb'))
84
+ top_popular = pickle.load(open('top_popular_hi.pkl','rb'))
85
+
86
+ s = all_movies.apply(lambda x: pd.Series(x['genres']),axis=1).stack().reset_index(level=1, drop=True)
87
+ s.name = 'genres'
88
+ genre_df = all_movies.drop('genres', axis=1).join(s)
89
+
90
+ movie_list = movies['title'].values
91
+ option_selected = st.selectbox(
92
+ 'Type or Select Movie Name from Dropdown',
93
+ movie_list
94
+ )
95
+
96
+ genre_list = ['Action','Romance','Adventure','Science Fiction','Comedy']
97
+ genre_selected = st.selectbox(
98
+ 'Type or Select Genre from Dropdown',
99
+ genre_list
100
+ )
101
+
102
+ if st.button('Show Recommendation'):
103
+ recommended_movie_names, recommended_movie_posters = recommend(option_selected)
104
+ top_popular_movies = get_popular(top_popular)
105
+ st.header("Movies Based on Content: Similar Movies")
106
+ col1, col2, col3, col4, col5 = st.columns(5)
107
+ with col1:
108
+ st.image(recommended_movie_posters[0], caption=recommended_movie_names[0])
109
+ with col2:
110
+ st.image(recommended_movie_posters[1], caption=recommended_movie_names[1])
111
+
112
+ with col3:
113
+ st.image(recommended_movie_posters[2], caption=recommended_movie_names[2])
114
+ with col4:
115
+ st.image(recommended_movie_posters[3], caption=recommended_movie_names[3])
116
+ with col5:
117
+ st.image(recommended_movie_posters[4], caption=recommended_movie_names[4])
118
+
119
+ st.header("Movies Based on Popularity: Top Popular")
120
+ popular = []
121
+ for row in top_popular_movies.loc[:,['title','movie_id']].values:
122
+ popular.append(row)
123
+ col6, col7, col8, col9, col10 = st.columns(5)
124
+ with col6:
125
+ full_path = fetch_poster(popular[0][1])
126
+ st.image(full_path, caption=popular[0][0])
127
+ with col7:
128
+ full_path = fetch_poster(popular[1][1])
129
+ st.image(full_path, caption=popular[1][0])
130
+ with col8:
131
+ full_path = fetch_poster(popular[2][1])
132
+ st.image(full_path, caption=popular[2][0])
133
+ with col9:
134
+ full_path = fetch_poster(popular[3][1])
135
+ st.image(full_path, caption=popular[3][0])
136
+ with col10:
137
+ full_path = fetch_poster(popular[4][1])
138
+ st.image(full_path, caption=popular[4][0])
139
+
140
+
141
+ st.header("Movies Based on Genre: Top "+str(genre_selected)+" Movies")
142
+ top_gener_based = top_genre_based_movies(genre_selected).head(5)
143
+ genre_popular = []
144
+ for row in top_gener_based.loc[:, ['title', 'movie_id']].values:
145
+ genre_popular.append(row)
146
+ col11, col12, col13, col14, col15 = st.columns(5)
147
+ with col11:
148
+ full_path = fetch_poster(genre_popular[0][1])
149
+ st.image(full_path, caption=genre_popular[0][0])
150
+ with col12:
151
+ full_path = fetch_poster(genre_popular[1][1])
152
+ st.image(full_path, caption=genre_popular[1][0])
153
+ with col13:
154
+ full_path = fetch_poster(genre_popular[2][1])
155
+ st.image(full_path, caption=genre_popular[2][0])
156
+ with col14:
157
+ full_path = fetch_poster(genre_popular[3][1])
158
+ st.image(full_path, caption=genre_popular[3][0])
159
+ with col15:
160
+ full_path = fetch_poster(genre_popular[4][1])
161
+ st.image(full_path, caption=genre_popular[4][0])
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+ st.markdown(footer,unsafe_allow_html=True)
movie_list_hi.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2891c693f283e45aad254aafd947ff13c5364fc8a3501eebac465fd7bf33553b
3
+ size 504725
movies_df_hi.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:efcfb5764e355eff6344530bc0e501d5f7222c98ea4c56ad8723bc9fd0d2f767
3
+ size 11674864
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ sklearn
similarity_hi.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9373b0aceaef2615c22975d0d4d6a5d1e00ff4e8bf68bdce103650af309d66f8
3
+ size 13025568
top_popular_hi.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:528bd83c7c17adad2edecb2f0f5c896bd7b6011170d96aecb7157c424aa97370
3
+ size 7328