Harsh12 commited on
Commit
7f1da0d
1 Parent(s): 1f4691d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import pandas as pd
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ from sklearn.feature_extraction.text import CountVectorizer
6
+ from imdb import IMDb
7
+
8
+
9
+ similarity = pickle.load(open('cosine_sim.pkl', 'rb'))
10
+ movie_dict = pickle.load(open('movie_dict.pkl', 'rb'))
11
+ movies = pd.DataFrame(movie_dict)
12
+
13
+ programme_list=movies['title'].to_list()
14
+
15
+ imdb = IMDb()
16
+ def get_movie_id(movie_title):
17
+ """Get the IMDb ID of the movie using the IMDbPY library."""
18
+ try:
19
+
20
+ movies = imdb.search_movie(movie_title)
21
+ movie_id = movies[0].getID() # get the ID of the first search result
22
+ return movie_id
23
+
24
+ except Exception as e:
25
+ st.error("Error: Failed to retrieve IMDb ID for the selected movie. Please try again with a different movie.")
26
+ st.stop()
27
+
28
+
29
+
30
+ def get_poster_url(imdb_id):
31
+ """Get the URL of the poster image of the movie using the IMDbPY library."""
32
+ try:
33
+
34
+ movie = imdb.get_movie(imdb_id)
35
+ poster_url = movie['full-size cover url']
36
+ return poster_url
37
+
38
+ except Exception as e:
39
+ st.error("Error: Failed to retrieve poster URL for the selected movie. Please try again with a different movie.")
40
+ st.stop()
41
+
42
+
43
+
44
+ def recommend(movie):
45
+ index = programme_list.index(movie)
46
+ 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.
47
+
48
+ #position 0 is the movie itself, thus exclude
49
+ 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.
50
+ recommend_index = [i[0] for i in sim_score]
51
+ rec_movie = movies['title'].iloc[recommend_index]
52
+ rec_movie_ids = [get_movie_id(title) for title in rec_movie]
53
+ return rec_movie, rec_movie_ids
54
+
55
+ st.set_page_config(page_title='Movie Recommender System', page_icon=':clapper:', layout='wide')
56
+ st.title('Movie Recommender System')
57
+
58
+
59
+ selected_movie_name = st.selectbox('Please select a Movie',
60
+ sorted(movies['title'].values))
61
+
62
+ if st.button('Recommend Me'):
63
+ try:
64
+
65
+ recommendations, rec_movie_ids = recommend(selected_movie_name)
66
+ # st.write(recommendations, rec_movie_ids)
67
+ # st.write(recommendations[6195])
68
+ final_movie_names = []
69
+ for i, rec_id in zip(recommendations, rec_movie_ids):
70
+ final_movie_names.append(i)
71
+ # st.write(i)
72
+ # poster_url = get_poster_url(rec_id)
73
+ # st.image(poster_url)
74
+
75
+
76
+ col1, col2, col3, col4, col5 = st.columns(5)
77
+ cols = [col1, col2, col3, col4, col5]
78
+ with col1:
79
+ st.text(final_movie_names[0])
80
+ poster_url = get_poster_url(rec_movie_ids[0])
81
+ st.image(poster_url)
82
+ with col2:
83
+ st.text(final_movie_names[1])
84
+ poster_url = get_poster_url(rec_movie_ids[1])
85
+ st.image(poster_url)
86
+ with col3:
87
+ st.text(final_movie_names[2])
88
+ poster_url = get_poster_url(rec_movie_ids[2])
89
+ st.image(poster_url)
90
+ with col4:
91
+ st.text(final_movie_names[3])
92
+ poster_url = get_poster_url(rec_movie_ids[3])
93
+ st.image(poster_url)
94
+ with col5:
95
+ st.text(final_movie_names[4])
96
+ poster_url = get_poster_url(rec_movie_ids[4])
97
+ st.image(poster_url)
98
+ except Exception as e:
99
+ st.write('An error occurred while generating recommendations:', e)
100
+