File size: 3,529 Bytes
7f1da0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
import pickle
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer
from imdb import IMDb


similarity = pickle.load(open('cosine_sim.pkl', 'rb'))
movie_dict = pickle.load(open('movie_dict.pkl', 'rb'))
movies = pd.DataFrame(movie_dict)

programme_list=movies['title'].to_list()

imdb = IMDb()
def get_movie_id(movie_title):
    """Get the IMDb ID of the movie using the IMDbPY library."""
    try:

        movies = imdb.search_movie(movie_title)
        movie_id = movies[0].getID()  # get the ID of the first search result
        return movie_id

    except Exception as e:
        st.error("Error: Failed to retrieve IMDb ID for the selected movie. Please try again with a different movie.")
        st.stop()



def get_poster_url(imdb_id):
    """Get the URL of the poster image of the movie using the IMDbPY library."""
    try:

        movie = imdb.get_movie(imdb_id)
        poster_url = movie['full-size cover url']
        return poster_url

    except Exception as e:
        st.error("Error: Failed to retrieve poster URL for the selected movie. Please try again with a different movie.")
        st.stop()



def recommend(movie):
    index = programme_list.index(movie)         
    sim_score = list(enumerate(similarity[index])) #creates a list of tuples containing the similarity score and index between the input title and all other programmes in the dataset.
    
    #position 0 is the movie itself, thus exclude
    sim_score = sorted(sim_score, key= lambda x: x[1], reverse=True)[1:6]  #sorts the list of tuples by similarity score in descending order.
    recommend_index = [i[0] for i in sim_score]
    rec_movie = movies['title'].iloc[recommend_index]
    rec_movie_ids = [get_movie_id(title) for title in rec_movie]
    return rec_movie, rec_movie_ids
    
st.set_page_config(page_title='Movie Recommender System', page_icon=':clapper:', layout='wide')
st.title('Movie Recommender System')


selected_movie_name = st.selectbox('Please select a Movie',
sorted(movies['title'].values))

if st.button('Recommend Me'):
    try:

        recommendations, rec_movie_ids = recommend(selected_movie_name)
        # st.write(recommendations, rec_movie_ids)
        # st.write(recommendations[6195])
        final_movie_names = []
        for i, rec_id in zip(recommendations, rec_movie_ids):
            final_movie_names.append(i)
            # st.write(i)
            # poster_url = get_poster_url(rec_id)
            # st.image(poster_url)


        col1, col2, col3, col4, col5 = st.columns(5)
        cols = [col1, col2, col3, col4, col5]
        with col1:
            st.text(final_movie_names[0])
            poster_url = get_poster_url(rec_movie_ids[0])
            st.image(poster_url)
        with col2:
            st.text(final_movie_names[1])
            poster_url = get_poster_url(rec_movie_ids[1])
            st.image(poster_url)
        with col3:
            st.text(final_movie_names[2])
            poster_url = get_poster_url(rec_movie_ids[2])
            st.image(poster_url)
        with col4:
            st.text(final_movie_names[3])
            poster_url = get_poster_url(rec_movie_ids[3])
            st.image(poster_url)
        with col5:
            st.text(final_movie_names[4])
            poster_url = get_poster_url(rec_movie_ids[4])
            st.image(poster_url)
    except Exception as e:
        st.write('An error occurred while generating recommendations:', e)