AI / app.py
Sourabh-Kumar04's picture
Upload app.py
f5d671b verified
import streamlit as st
import pickle
import pandas as pd
import requests
def fetch_poster(movie_id):
response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key=44f0fa1913413a1261cfb9fda276f78a&language=en-US'.format(movie_id))
data = response.json()
return "https://image.tmdb.org/t/p/w500" + data['poster_path']
def recommend(movie):
movie_index = movies[movies['title'] == movie].index[0]
distance = similarity[movie_index]
movie_list = sorted(list(enumerate(distance)),reverse=True, key=lambda x:x[1])[1:6]
recommended_movie = []
recommended_movie_poster = []
for i in movie_list:
movie_id = movies.iloc[i[0]].movie_id
recommended_movie.append(movies.iloc[i[0]].title)
# fetch poster from tmdb website
recommended_movie_poster.append(fetch_poster(movie_id))
return recommended_movie, recommended_movie_poster
# Load Data
movies_dict = pickle.load(open('movie_dict.pkl', 'rb'))
movies = pd.DataFrame(movies_dict)
similarity = pickle.load(open('similarity.pkl', 'rb'))
# Streamlit UI
st.title('Movie Recommendation System')
selected_movie_name = st.selectbox(
'Select a movie',
movies['title'].values
)
if st.button('Recommend'):
names,posters = recommend(selected_movie_name)
col1, col2, col3, col4, col5 = st.columns(5)
cols = [col1, col2, col3, col4, col5]
for i in range(len(names)):
with cols[i]:
st.text(names[i])
st.image(posters[i])
# with col1:
# st.text(names[0])
# st.image(posters[0])
# with col2:
# st.text(names[1])
# st.image(posters[1])
# with col3:
# st.text(names[2])
# st.image(posters[2])
# with col4:
# st.text(names[3])
# st.image(posters[3])
# with col5:
# st.text(names[4])
# st.image(posters[4])