%%writefile movie_recommendation_app.py import pickle import streamlit as st from sklearn.metrics.pairwise import cosine_similarity from PIL import Image @st.cache def get_recommendation(title): idx = df1.index[df1['title'] == title][0] poster = f'https://image.tmdb.org/t/p/w500/{df1.loc[idx, "poster_path"]}' # Get the pairwsie similarity scores of all movies with that movie sim_scores = list(enumerate( cosine_similarity( tfidf_matrix, tfidf_matrix[idx]))) # Sort the movies based on the similarity scores sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True) # Get the scores of the 10 most similar movies sim_scores = sim_scores[1:13] # Get the movie indices movie_indices = [i[0] for i in sim_scores] # Return the top 10 most similar movies result = df1.iloc[movie_indices] recommended_movie_names = [] recommended_movie_posters = [] recommended_movie_overview = [] for i, j in enumerate(result.poster_path): recommended_movie_names.append(result.iloc[i].title) recommended_movie_posters.append(f'https://image.tmdb.org/t/p/w500/{j}') recommended_movie_overview.append(result.iloc[i].overview) return poster, recommended_movie_names, recommended_movie_posters, recommended_movie_overview image = Image.open('Movie recommender system.jpg') st.image(image) st.markdown('You might have wondered sometime or at some point that how do platforms like Netflix or AmazonPrime Video are able to recommend us TV shows or movies, what kind of an algorithm do these websites use to recommend us movies. Well as complicated or difficult as it might seem this is simply just a mixture of some machine learning algorithms with some Natural Language Processing. ') st.markdown('There are two main techniques used in recommendation system, known as content-based filtering and collaborative filtering') st.markdown('For this project I have used Content Based Recommendation System, It uses attributes such as genre, director, description, actors, etc. for movies, to make suggestions for the users. The intuition behind this sort of recommendation system is that if a user liked a particular movie or show, he/she might like a movie or a show similar to it.') df1 = pickle.load(open('movie_list.pkl ', 'rb')) tfidf_matrix = pickle.load(open('tfidf_matrix.pkl ', 'rb')) movies_list = df1['title'].values selected_movie = st.selectbox('Type and Choose The Movie',movies_list) if st.button('Show Recommendation'): poster,recommended_movie_names,recommended_movie_posters,recommended_movie_overview = get_recommendation(selected_movie) st.image(poster,width=160) col1, col2, col3, col4 = st.columns(4) with col1: st.image(recommended_movie_posters[0]) st.markdown(recommended_movie_names[0]) with st.expander("OverView"): st.write(recommended_movie_overview[0]) st.image(recommended_movie_posters[4]) st.markdown(recommended_movie_names[4]) with st.expander("OverView"): st.write(recommended_movie_overview[4]) st.image(recommended_movie_posters[8]) st.markdown(recommended_movie_names[8]) with st.expander("OverView"): st.write(recommended_movie_overview[8]) with col2: st.image(recommended_movie_posters[1]) st.markdown(recommended_movie_names[1]) with st.expander("OverView"): st.write(recommended_movie_overview[1]) st.image(recommended_movie_posters[5]) st.markdown(recommended_movie_names[5]) with st.expander("OverView"): st.write(recommended_movie_overview[5]) st.image(recommended_movie_posters[9]) st.markdown(recommended_movie_names[9]) with st.expander("OverView"): st.write(recommended_movie_overview[9]) with col3: st.image(recommended_movie_posters[2]) st.markdown(recommended_movie_names[2]) with st.expander("OverView"): st.write(recommended_movie_overview[2]) st.image(recommended_movie_posters[6]) st.markdown(recommended_movie_names[6]) with st.expander("OverView"): st.write(recommended_movie_overview[6]) st.image(recommended_movie_posters[10]) st.markdown(recommended_movie_names[10]) with st.expander("OverView"): st.write(recommended_movie_overview[10]) with col4: st.image(recommended_movie_posters[3]) st.markdown(recommended_movie_names[3]) with st.expander("OverView"): st.write(recommended_movie_overview[3]) st.image(recommended_movie_posters[7]) st.markdown(recommended_movie_names[7]) with st.expander("OverView"): st.write(recommended_movie_overview[7]) st.image(recommended_movie_posters[11]) st.markdown(recommended_movie_names[11]) with st.expander("OverView"): st.write(recommended_movie_overview[11])