File size: 2,168 Bytes
7474794
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)