File size: 5,564 Bytes
fce58be
 
 
583d3ee
fce58be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583d3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fce58be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583d3ee
 
 
 
 
 
fce58be
 
 
 
 
 
 
c94fcf8
 
 
 
 
 
fce58be
 
c94fcf8
 
f4c17e9
fce58be
 
 
 
 
 
 
 
 
 
 
 
c94fcf8
583d3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94fcf8
 
583d3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94fcf8
583d3ee
 
fce58be
 
 
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
import streamlit as st
import pickle
import requests
import pandas as pd

footer="""<style>
a:link , a:visited{
color: black;
background-color: transparent;
}

a:hover,  a:active {
color: red;
background-color: transparent;
}

.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: white;
color: black;
text-align: center;
}
</style>
<div class="footer">
<p>Developed with <span style ='color:red'>❤</span> by <a href="https://shrikrishnaparab.tech/" target="_blank">Shrikrishna Parab</a></p>
</div>
"""

def fetch_poster(movie_id):
    url = "https://api.themoviedb.org/3/movie/{}?api_key=8265bd1679663a7ea12ac168da84d2e8&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

def get_popular(qualified):
    top_5 = qualified.head(5)
    return top_5


def top_genre_based_movies(genre, percentile=0.95):
    df = genre_df[genre_df['genres'].str.contains(genre)]
    vote_counts = df['vote_count'].astype('int')
    vote_averages = df['vote_average'].astype('int')
    C = vote_averages.mean()
    m = vote_counts.quantile(percentile)
    qualified = df[(df['vote_count'] >= m)][['movie_id', 'title', 'vote_count', 'vote_average', 'genres']]
    qualified['vote_count'] = qualified['vote_count'].astype('int')
    qualified['vote_average'] = qualified['vote_average'].astype('int')
    qualified['wr'] = qualified.apply(
        lambda x: (x['vote_count'] / (x['vote_count'] + m) * x['vote_average']) + (m / (m + x['vote_count']) * C),
        axis=1)
    qualified = qualified.sort_values('wr', ascending=False).head(250)
    return qualified

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:6]:
        # 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


st.title("Movie Recommender System")

movies = pickle.load(open('movie_list.pkl','rb'))
similarity = pickle.load(open('similarity.pkl','rb'))
all_movies = pickle.load(open('movies_df.pkl','rb'))
top_popular = pickle.load(open('top_popular.pkl','rb'))

s = all_movies.apply(lambda x: pd.Series(x['genres']),axis=1).stack().reset_index(level=1, drop=True)
s.name = 'genres'
genre_df = all_movies.drop('genres', axis=1).join(s)

movie_list = movies['title'].values
option_selected = st.selectbox(
    'Type or Select Movie Name from Dropdown',
    movie_list
)

genre_list = ['Action','Romance','Adventure','Science Fiction','Comedy']
genre_selected = st.selectbox(
    'Type or Select Genre from Dropdown',
    genre_list
)

if st.button('Show Recommendation'):
    recommended_movie_names, recommended_movie_posters = recommend(option_selected)
    top_popular_movies = get_popular(top_popular)
    st.header("Movies Based on Content: Similar Movies")
    col1, col2, col3, col4, col5 = st.columns(5)
    with col1:
        st.image(recommended_movie_posters[0], caption=recommended_movie_names[0])
    with col2:
        st.image(recommended_movie_posters[1], caption=recommended_movie_names[1])

    with col3:
        st.image(recommended_movie_posters[2], caption=recommended_movie_names[2])
    with col4:
        st.image(recommended_movie_posters[3], caption=recommended_movie_names[3])
    with col5:
        st.image(recommended_movie_posters[4], caption=recommended_movie_names[4])

    st.header("Movies Based on Popularity: Top Popular")
    popular = []
    for row in top_popular_movies.loc[:,['title','movie_id']].values:
        popular.append(row)
    col6, col7, col8, col9, col10 = st.columns(5)
    with col6:
        full_path = fetch_poster(popular[0][1])
        st.image(full_path, caption=popular[0][0])
    with col7:
        full_path = fetch_poster(popular[1][1])
        st.image(full_path, caption=popular[1][0])
    with col8:
        full_path = fetch_poster(popular[2][1])
        st.image(full_path, caption=popular[2][0])
    with col9:
        full_path = fetch_poster(popular[3][1])
        st.image(full_path, caption=popular[3][0])
    with col10:
        full_path = fetch_poster(popular[4][1])
        st.image(full_path, caption=popular[4][0])


    st.header("Movies Based on Genre: Top "+str(genre_selected)+" Movies")
    top_gener_based = top_genre_based_movies(genre_selected).head(5)
    genre_popular = []
    for row in top_gener_based.loc[:, ['title', 'movie_id']].values:
        genre_popular.append(row)
    col11, col12, col13, col14, col15 = st.columns(5)
    with col11:
        full_path = fetch_poster(genre_popular[0][1])
        st.image(full_path, caption=genre_popular[0][0])
    with col12:
        full_path = fetch_poster(genre_popular[1][1])
        st.image(full_path, caption=genre_popular[1][0])
    with col13:
        full_path = fetch_poster(genre_popular[2][1])
        st.image(full_path, caption=genre_popular[2][0])
    with col14:
        full_path = fetch_poster(genre_popular[3][1])
        st.image(full_path, caption=genre_popular[3][0])
    with col15:
        full_path = fetch_poster(genre_popular[4][1])
        st.image(full_path, caption=genre_popular[4][0])

    




st.markdown(footer,unsafe_allow_html=True)