Insightly/Movie_Recommender
0
1import pickle2import streamlit as st3import requests4 5# Set page title and sidebar properties6st.set_page_config(page_title="Insightly")7 8st.markdown(9 """10 <style>11 .image-container {12 margin-bottom: 60px;13 }14 .sidebar-link {15 display: flex;16 justify-content: left;17 font-size: 28px;18 margin-top: 10px; /* Adjust margin-top value to control space on the top */19 margin-left: 20px; /* Adjust margin-left value to add space from the left */20 }21 .vertical-space {22 height: 20px;23 }24 .movie-title {25 font-size: 18px;26 font-weight: bold;27 }28 .row-padding {29 padding-bottom: 40px;30 }31 </style>32 """,33 unsafe_allow_html=True,34)35 36# Sidebar contents37with st.sidebar:38 st.image("https://i.ibb.co/bX6GdqG/insightly-wbg.png", use_column_width=True)39 st.markdown("<p class='sidebar-link'>๐ <a href='https://insightly-csv-bot.hf.space/'> CSV Bot</a></p>", unsafe_allow_html=True)40 st.markdown("<p class='sidebar-link'>๐ <a href='https://chandrakalagowda-demo2.hf.space/'> PDF Bot </a></p>", unsafe_allow_html=True)41 st.markdown("<p class='sidebar-link'>๐ธ <a href='https://insightly-frame-capturer.hf.space/'> Frame Capturer</a></p>", unsafe_allow_html=True)42 st.markdown("<p class='sidebar-link'>๐ผ๏ธ <a href='https://insightly-image-reader.hf.space/'> Image Reader</a></p>", unsafe_allow_html=True)43 st.markdown("<div class='vertical-space'></div>", unsafe_allow_html=True)44 45 46 47def fetch_poster(movie_id):48 url = "https://api.themoviedb.org/3/movie/{}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US".format(movie_id)49 data = requests.get(url)50 data = data.json()51 poster_path = data['poster_path']52 full_path = "https://image.tmdb.org/t/p/w500/" + poster_path53 return full_path54 55def recommend(movie):56 index = movies[movies['title'] == movie].index[0]57 distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])58 recommended_movie_names = []59 recommended_movie_posters = []60 for i in distances[1:6]:61 # fetch the movie poster62 movie_id = movies.iloc[i[0]].movie_id63 recommended_movie_posters.append(fetch_poster(movie_id))64 recommended_movie_names.append(movies.iloc[i[0]].title)65 66 return recommended_movie_names,recommended_movie_posters67 68 69st.title('Movie Recommender ๐ฌ')70 71# Provide the correct absolute paths to the pickled data72movie_list_path = "https://drive.google.com/file/d/1OmueIayrvczEQKRWIOfEkKH5zLWGxCn1/view?usp=drive_link"73similarity_path = "https://drive.google.com/file/d/1RI6XgtbaNxlBqZM0cznOZXN88tQWo98a/view?usp=drive_link"74 75movies = pickle.load(open(movie_list_path, 'rb'))76similarity = pickle.load(open(similarity_path, 'rb'))77 78 79movies = pickle.load(open(movie_list_path, 'rb'))80similarity = pickle.load(open(similarity_path, 'rb'))81 82movie_list = movies['title'].values83selected_movie = st.selectbox(84 "Type or select a movie from the dropdown",85 movie_list86)87 88if st.button('Show Recommendation'):89 recommended_movie_names, recommended_movie_posters = recommend(selected_movie)90 91 # Create columns based on the number of recommended movies92 num_recommendations = len(recommended_movie_names)93 num_columns = 394 num_rows = (num_recommendations + num_columns - 1) // num_columns # Calculate the number of rows required95 96 # Create a list of columns97 cols = [st.columns(num_columns) for _ in range(num_rows)]98 99 # Loop through recommended movies and posters and display them in the columns100 for i, movie_name in enumerate(recommended_movie_names):101 col_index = i // num_columns102 row_index = i % num_columns103 cols[col_index][row_index].markdown(f"<span class='movie-title'>{movie_name}</span>", unsafe_allow_html=True)104 cols[col_index][row_index].image(recommended_movie_posters[i])105 106 # Add padding between the rows107 st.markdown("<br>", unsafe_allow_html=True)108 st.write('<div class="row-padding"></div>', unsafe_allow_html=True)109 110 111 