Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pickle | |
import requests | |
import pandas as pd | |
def recommend(movies, movie_name): | |
movie_index = movies[movies['title'] == movie_name].index[0] | |
distances = similarity[movie_index] | |
rec_movies_list = sorted(list(enumerate(distances)), | |
reverse=True, | |
key = lambda x: x[1])[1:6] | |
rec_movie_names = [] | |
rec_movie_posters = [] | |
for i in rec_movies_list: | |
# st.write(movies.iloc[i[0]].title) | |
rec_movie_names.append(movies.iloc[i[0]].title) | |
rec_movie_posters.append(fetch_poster(movies.iloc[i[0]].movie_id)) | |
return rec_movie_names, rec_movie_posters | |
def fetch_poster(movie_id): | |
url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key=95516cdf5c8762bc6bb90eef4d45be3c" | |
data = requests.get(url) | |
data = data.json() | |
# st.write(data) | |
poster_path = data['poster_path'] | |
full_path = "https://image.tmdb.org/t/p/w500/" + poster_path | |
return full_path | |
# Read the movies dict file and convert it into a dataframe | |
movies_dict = pickle.load(open("./model/movies_dict.pkl", "rb")) | |
movies = pd.DataFrame(movies_dict) | |
# Similarity matrix of the movies with each other | |
similarity = pickle.load(open("./model/similarity.pkl", "rb")) | |
movies_list = movies['title'].values | |
# print(movies_list[:5]) | |
# StreamLit application | |
st.set_page_config(layout='wide', | |
page_title="Movie_RecSys") | |
st.header('Movie Recommender System') | |
st.markdown("#") | |
selected_movie = st.selectbox( | |
"Type or select a movie from the dropdown", | |
movies_list | |
) | |
st.markdown("###") | |
if st.button('Recommend'): | |
rec_movie_names, rec_movie_posters = recommend(movies, selected_movie) | |
# st.text(rec_movie_names) | |
# st.text(rec_movie_posters) | |
st.markdown("#") | |
col1, col2, col3, col4, col5 = st.columns(5, gap = "small") | |
with col1: | |
st.image(rec_movie_posters[0]) | |
st.markdown(rec_movie_names[0]) | |
with col2: | |
st.image(rec_movie_posters[1]) | |
st.markdown(rec_movie_names[1]) | |
with col3: | |
st.image(rec_movie_posters[2]) | |
st.markdown(rec_movie_names[2]) | |
with col4: | |
st.image(rec_movie_posters[3]) | |
st.markdown(rec_movie_names[3]) | |
with col5: | |
st.image(rec_movie_posters[4]) | |
st.markdown(rec_movie_names[4]) |