|
import streamlit as st |
|
import pickle |
|
import pandas as pd |
|
import requests |
|
|
|
|
|
def fetch_poster(movie_id): |
|
response = requests.get( |
|
f'https://api.themoviedb.org/3/movie/{movie_id}?api_key=9b955595d7ffef24254513d6a66503fe&language=en-US' |
|
) |
|
data = response.json() |
|
return "http://image.tmdb.org/t/p/w500" + data['poster_path'] |
|
|
|
|
|
def recommend(movie): |
|
movie_index = movies[movies['title'] == movie].index[0] |
|
distances = similarity[movie_index] |
|
movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:6] |
|
|
|
recommended_movies = [] |
|
recommended_posters = [] |
|
|
|
for i in movie_list: |
|
movie_id = movies.iloc[i[0]].movie_id |
|
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('movie_dict2.pkl', 'rb')) |
|
movies = pd.DataFrame(movies_dict) |
|
similarity = pickle.load(open('similarity.pkl', 'rb')) |
|
|
|
|
|
st.title('🎬 Movie Recommender System') |
|
|
|
|
|
st.subheader("Select a movie to get recommendations:") |
|
selected_movie_name = st.selectbox('Choose a movie:', movies['title'].values) |
|
|
|
|
|
if st.button('Recommend'): |
|
recommended_names, recommended_posters = recommend(selected_movie_name) |
|
|
|
|
|
st.subheader(f"Movies recommended based on '{selected_movie_name}':") |
|
cols = st.columns(5) |
|
for idx, col in enumerate(cols): |
|
with col: |
|
st.text(recommended_names[idx]) |
|
st.image(recommended_posters[idx]) |
|
|
|
search_url = f"https://www.google.com/search?q={recommended_names[idx].replace(' ', '+')}+movie" |
|
st.markdown(f"[Search '{recommended_names[idx]}' on Google]({search_url})", unsafe_allow_html=True) |