Reaper200's picture
Create app.py
7474794 verified
import streamlit as st
import pickle
import pandas as pd
import requests
# Function to fetch movie poster from API
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']
# Function to recommend movies based on selected movie
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
# Load data (movies and similarity matrix)
movies_dict = pickle.load(open('movie_dict2.pkl', 'rb'))
movies = pd.DataFrame(movies_dict)
similarity = pickle.load(open('similarity.pkl', 'rb'))
# App title
st.title('🎬 Movie Recommender System')
# Movie selection section
st.subheader("Select a movie to get recommendations:")
selected_movie_name = st.selectbox('Choose a movie:', movies['title'].values)
# Recommendation button and display
if st.button('Recommend'):
recommended_names, recommended_posters = recommend(selected_movie_name)
# Displaying recommendations in a more visually appealing way
st.subheader(f"Movies recommended based on '{selected_movie_name}':")
cols = st.columns(5) # Dividing the page into 5 columns
for idx, col in enumerate(cols):
with col:
st.text(recommended_names[idx])
st.image(recommended_posters[idx])
# Adding a clickable link that redirects to Google search for the movie
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)