Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,66 @@
|
|
1 |
-
import pickle
|
2 |
-
import streamlit as st
|
3 |
-
import requests
|
4 |
-
|
5 |
-
def fetch_poster(movie_id):
|
6 |
-
url = "https://api.themoviedb.org/3/movie/{}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US".format(movie_id)
|
7 |
-
data = requests.get(url)
|
8 |
-
data = data.json()
|
9 |
-
print("API Response for movie ID {}: {}".format(movie_id, data)) # Print API response for debugging
|
10 |
-
if 'poster_path' in data:
|
11 |
-
poster_path = data['poster_path']
|
12 |
-
full_path = "https://image.tmdb.org/t/p/w500/" + poster_path
|
13 |
-
return full_path
|
14 |
-
else:
|
15 |
-
# Handle the case when 'poster_path' is not found
|
16 |
-
print("Poster path not found for movie ID:", movie_id) # Print error message for debugging
|
17 |
-
return "Placeholder_for_missing_poster"
|
18 |
-
|
19 |
-
def recommend(movie):
|
20 |
-
index = movies[movies['title'] == movie].index[0]
|
21 |
-
distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
|
22 |
-
recommended_movie_names = []
|
23 |
-
recommended_movie_posters = []
|
24 |
-
for i in distances[1:6]:
|
25 |
-
# fetch the movie poster
|
26 |
-
movie_title = movies.iloc[i[0]]['title'] # Fetch the movie title
|
27 |
-
print("Fetching poster for movie:", movie_title) # Print movie title for debugging
|
28 |
-
# Use the movie title to fetch the movie ID from the API
|
29 |
-
movie_id = fetch_movie_id(movie_title)
|
30 |
-
if movie_id:
|
31 |
-
recommended_movie_posters.append(fetch_poster(movie_id))
|
32 |
-
recommended_movie_names.append(movie_title) # Append movie title instead of 'title' column from DataFrame
|
33 |
-
|
34 |
-
return recommended_movie_names, recommended_movie_posters
|
35 |
-
|
36 |
-
# Function to fetch movie ID based on the title (modify this according to your API)
|
37 |
-
def fetch_movie_id(movie_title):
|
38 |
-
# Implement the logic to fetch movie ID using the movie title
|
39 |
-
# For demonstration, I'm assuming a simple dictionary mapping of titles to IDs
|
40 |
-
movie_id_mapping = {
|
41 |
-
"Movie Title 1": "id1",
|
42 |
-
"Movie Title 2": "id2",
|
43 |
-
# Add more mappings as needed
|
44 |
-
}
|
45 |
-
return movie_id_mapping.get(movie_title, None)
|
46 |
-
|
47 |
-
st.header('Movie Recommender System')
|
48 |
-
movies = pickle.load(open('movie_list.pkl','rb'))
|
49 |
-
similarity = pickle.load(open('
|
50 |
-
|
51 |
-
movie_list = movies['title'].values
|
52 |
-
selected_movie = st.selectbox(
|
53 |
-
"Type or select a movie from the dropdown",
|
54 |
-
movie_list
|
55 |
-
)
|
56 |
-
|
57 |
-
if st.button('Show Recommendation'):
|
58 |
-
recommended_movie_names, recommended_movie_posters = recommend(selected_movie)
|
59 |
-
if not recommended_movie_names:
|
60 |
-
st.write("No recommendations found.")
|
61 |
-
else:
|
62 |
-
col1, col2, col3, col4, col5 = st.columns(5)
|
63 |
-
for name, poster in zip(recommended_movie_names, recommended_movie_posters):
|
64 |
-
with col1:
|
65 |
-
st.text(name)
|
66 |
-
st.image(poster)
|
|
|
1 |
+
import pickle
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
|
5 |
+
def fetch_poster(movie_id):
|
6 |
+
url = "https://api.themoviedb.org/3/movie/{}?api_key=8265bd1679663a7ea12ac168da84d2e8&language=en-US".format(movie_id)
|
7 |
+
data = requests.get(url)
|
8 |
+
data = data.json()
|
9 |
+
print("API Response for movie ID {}: {}".format(movie_id, data)) # Print API response for debugging
|
10 |
+
if 'poster_path' in data:
|
11 |
+
poster_path = data['poster_path']
|
12 |
+
full_path = "https://image.tmdb.org/t/p/w500/" + poster_path
|
13 |
+
return full_path
|
14 |
+
else:
|
15 |
+
# Handle the case when 'poster_path' is not found
|
16 |
+
print("Poster path not found for movie ID:", movie_id) # Print error message for debugging
|
17 |
+
return "Placeholder_for_missing_poster"
|
18 |
+
|
19 |
+
def recommend(movie):
|
20 |
+
index = movies[movies['title'] == movie].index[0]
|
21 |
+
distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
|
22 |
+
recommended_movie_names = []
|
23 |
+
recommended_movie_posters = []
|
24 |
+
for i in distances[1:6]:
|
25 |
+
# fetch the movie poster
|
26 |
+
movie_title = movies.iloc[i[0]]['title'] # Fetch the movie title
|
27 |
+
print("Fetching poster for movie:", movie_title) # Print movie title for debugging
|
28 |
+
# Use the movie title to fetch the movie ID from the API
|
29 |
+
movie_id = fetch_movie_id(movie_title)
|
30 |
+
if movie_id:
|
31 |
+
recommended_movie_posters.append(fetch_poster(movie_id))
|
32 |
+
recommended_movie_names.append(movie_title) # Append movie title instead of 'title' column from DataFrame
|
33 |
+
|
34 |
+
return recommended_movie_names, recommended_movie_posters
|
35 |
+
|
36 |
+
# Function to fetch movie ID based on the title (modify this according to your API)
|
37 |
+
def fetch_movie_id(movie_title):
|
38 |
+
# Implement the logic to fetch movie ID using the movie title
|
39 |
+
# For demonstration, I'm assuming a simple dictionary mapping of titles to IDs
|
40 |
+
movie_id_mapping = {
|
41 |
+
"Movie Title 1": "id1",
|
42 |
+
"Movie Title 2": "id2",
|
43 |
+
# Add more mappings as needed
|
44 |
+
}
|
45 |
+
return movie_id_mapping.get(movie_title, None)
|
46 |
+
|
47 |
+
st.header('Movie Recommender System')
|
48 |
+
movies = pickle.load(open('movie_list.pkl','rb'))
|
49 |
+
similarity = pickle.load(open('similarity.pkl','rb'))
|
50 |
+
|
51 |
+
movie_list = movies['title'].values
|
52 |
+
selected_movie = st.selectbox(
|
53 |
+
"Type or select a movie from the dropdown",
|
54 |
+
movie_list
|
55 |
+
)
|
56 |
+
|
57 |
+
if st.button('Show Recommendation'):
|
58 |
+
recommended_movie_names, recommended_movie_posters = recommend(selected_movie)
|
59 |
+
if not recommended_movie_names:
|
60 |
+
st.write("No recommendations found.")
|
61 |
+
else:
|
62 |
+
col1, col2, col3, col4, col5 = st.columns(5)
|
63 |
+
for name, poster in zip(recommended_movie_names, recommended_movie_posters):
|
64 |
+
with col1:
|
65 |
+
st.text(name)
|
66 |
+
st.image(poster)
|