Spaces:
Sleeping
Sleeping
scholar-2001
commited on
Commit
•
9f71e21
1
Parent(s):
9c1a8da
First
Browse files- app.py +57 -0
- df_popularity.pkl +3 -0
- mapping.pkl +3 -0
- requirements.txt +1 -0
- tfidf.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
from sklearn.metrics.pairwise import linear_kernel
|
6 |
+
def fetch_poster(movie_id):
|
7 |
+
url = "https://api.themoviedb.org/3/movie/{}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US".format(movie_id)
|
8 |
+
data = requests.get(url)
|
9 |
+
data = data.json()
|
10 |
+
poster_path = data['poster_path']
|
11 |
+
full_path = "https://image.tmdb.org/t/p/w500/" + poster_path
|
12 |
+
return full_path
|
13 |
+
|
14 |
+
def get_popular_recommendations(title, linear_sim, df):
|
15 |
+
indices = pd.Series(df.index, index=df['title']).drop_duplicates()
|
16 |
+
idx = indices[title]
|
17 |
+
|
18 |
+
sim_scores = list(enumerate(linear_sim[idx]))
|
19 |
+
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
20 |
+
top_movies_indices = [i[0] for i in sim_scores[1:31]]
|
21 |
+
top_movies = df[['title','popularity_score']].iloc[top_movies_indices]
|
22 |
+
top_movies = list(top_movies.sort_values('popularity_score',ascending = False).head(5)['title'])
|
23 |
+
top_movies_posters = [fetch_poster(mapping[title]) for title in top_movies ]
|
24 |
+
return top_movies, top_movies_posters
|
25 |
+
|
26 |
+
st.header('Movie Recommender System')
|
27 |
+
movies = pickle.load(open('df_popularity.pkl','rb'))
|
28 |
+
df_popularity = pd.DataFrame(movies)
|
29 |
+
tfidf_matrix = pickle.load(open('tfidf.pkl','rb'))
|
30 |
+
linear_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
|
31 |
+
mapping = pickle.load(open('mapping.pkl','rb'))
|
32 |
+
movie_list = df_popularity['title'].values
|
33 |
+
selected_movie = st.selectbox(
|
34 |
+
"Type or select a movie from the dropdown",
|
35 |
+
movie_list
|
36 |
+
)
|
37 |
+
|
38 |
+
if st.button('Show Recommendation'):
|
39 |
+
recommended_movie_names,recommended_movie_posters = get_popular_recommendations(selected_movie ,linear_sim ,df_popularity)
|
40 |
+
col1, col2, col3, col4, col5 = st.columns(5)
|
41 |
+
with col1:
|
42 |
+
st.text(recommended_movie_names[0])
|
43 |
+
st.image(recommended_movie_posters[0])
|
44 |
+
with col2:
|
45 |
+
st.text(recommended_movie_names[1])
|
46 |
+
st.image(recommended_movie_posters[1])
|
47 |
+
|
48 |
+
with col3:
|
49 |
+
st.text(recommended_movie_names[2])
|
50 |
+
st.image(recommended_movie_posters[2])
|
51 |
+
with col4:
|
52 |
+
st.text(recommended_movie_names[3])
|
53 |
+
st.image(recommended_movie_posters[3])
|
54 |
+
with col5:
|
55 |
+
st.text(recommended_movie_names[4])
|
56 |
+
st.image(recommended_movie_posters[4])
|
57 |
+
|
df_popularity.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:069a76c21ea30c73af03256510a6b43170b3cf324cd3c41dbeef80bd77534b04
|
3 |
+
size 10316460
|
mapping.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3258fd12a04f6322a3a7df5f7c4cf69b1e5245c0a95f1391bc4df39d89657f69
|
3 |
+
size 1020592
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
scikit-learn
|
tfidf.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6a4abd058c42eff9247e280c6e356c1af0a97ff2ea4cc812b0ecff4d34d24dfb
|
3 |
+
size 4574514
|