-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (48 loc) · 2.07 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
import streamlit as st
import pypickle as pickle
import pandas as pd
import requests
import time
def local_css(file_name):
with open(file_name) as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
def fetch_poster(movie_id):
response=requests.get('https://api.themoviedb.org/3/movie/{}?api_key=f19da43b6e95d951f63c85c751fee805&&language=en-US'.format(movie_id))
data=response.json()
return "https://image.tmdb.org/t/p/original/"+data['poster_path']
def recommend(movie):
movie_index=movies[movies['title']==movie].index[0]
distances=similarity[movie_index]
movies_list=sorted(list(enumerate(similarity[movie_index])),reverse=True,key=lambda x:x[1])[1:10]
recommended_movies=[]
recommended_posters=[]
for i in movies_list:
movie_id=movies.iloc[i[0]].movie_id
#Fetch poster from API
recommended_movies.append(movies.iloc[i[0]].title)
recommended_posters.append(fetch_poster(movie_id))
return recommended_movies,recommended_posters
movies_dict=pickle.load(open('movies_dict.pkl','rb'))
movies=pd.DataFrame(movies_dict)
local_css("styles.css")
similarity=pickle.load(open('similarity.pkl','rb'))
st.title('Movie Recommender System')
selected_movie_name = st.selectbox(
'Which movie did you watch recently?',
(movies['title'].values))
if st.button('Recommend'):
names, posters = recommend(selected_movie_name)
# Create a container for the movies
st.markdown('<div class="movie-container">', unsafe_allow_html=True)
for i in range(len(names)):
movie_search_url = f"https://www.google.com/search?q={names[i]}"
movie_html = f'''
<div class="movie">
<a href="{movie_search_url}" target="_blank">
<p>{names[i]}</p>
<img src="{posters[i]}" class="movie-poster" />
</a>
</div>
'''
st.markdown(movie_html, unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)