Update app.py
Browse files
app.py
CHANGED
@@ -16,25 +16,39 @@ def get_recommendations(title, cosine_sim=cosine_sim):
|
|
16 |
movie_indices = [i[0] for i in sim_scores]
|
17 |
return movies[['title', 'movie_id']].iloc[movie_indices]
|
18 |
|
19 |
-
# Fetch movie poster
|
20 |
-
def
|
21 |
-
api_key = "
|
22 |
-
url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}'
|
23 |
-
response = requests.get(url)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Streamlit UI
|
40 |
st.title("Movie Recommendation System")
|
@@ -52,7 +66,10 @@ if st.button('Recommend'):
|
|
52 |
if j < len(recommendations):
|
53 |
movie_title = recommendations.iloc[j]['title']
|
54 |
movie_id = recommendations.iloc[j]['movie_id']
|
55 |
-
|
|
|
56 |
with col:
|
57 |
-
st.image(
|
58 |
-
|
|
|
|
|
|
16 |
movie_indices = [i[0] for i in sim_scores]
|
17 |
return movies[['title', 'movie_id']].iloc[movie_indices]
|
18 |
|
19 |
+
# Fetch movie poster and additional details using append_to_response
|
20 |
+
def fetch_movie_data(movie_id):
|
21 |
+
api_key = "5c1c27e14a6a61a873db79d82528056f" # Updated API key
|
22 |
+
url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}&append_to_response=videos,images'
|
|
|
23 |
|
24 |
+
try:
|
25 |
+
response = requests.get(url)
|
26 |
+
response.raise_for_status() # Raise exception for 4xx/5xx errors
|
27 |
+
data = response.json()
|
28 |
+
|
29 |
+
# Get poster path with fallback
|
30 |
+
poster_path = data.get('poster_path', '')
|
31 |
+
poster_url = f"https://image.tmdb.org/t/p/w500{poster_path}" if poster_path else \
|
32 |
+
"https://via.placeholder.com/500x750?text=No+Image+Available"
|
33 |
+
|
34 |
+
# Extract additional information
|
35 |
+
trailer = next((v for v in data.get('videos', {}).get('results', [])
|
36 |
+
if v['type'] == 'Trailer'), None)
|
37 |
+
|
38 |
+
return {
|
39 |
+
'poster': poster_url,
|
40 |
+
'trailer_key': trailer['key'] if trailer else None,
|
41 |
+
'backdrop': data.get('images', {}).get('backdrops', [{}])[0].get('file_path', ''),
|
42 |
+
'overview': data.get('overview', 'No description available')
|
43 |
+
}
|
44 |
|
45 |
+
except requests.exceptions.RequestException:
|
46 |
+
return {
|
47 |
+
'poster': "https://via.placeholder.com/500x750?text=No+Image+Available",
|
48 |
+
'trailer_key': None,
|
49 |
+
'backdrop': '',
|
50 |
+
'overview': 'No description available'
|
51 |
+
}
|
52 |
|
53 |
# Streamlit UI
|
54 |
st.title("Movie Recommendation System")
|
|
|
66 |
if j < len(recommendations):
|
67 |
movie_title = recommendations.iloc[j]['title']
|
68 |
movie_id = recommendations.iloc[j]['movie_id']
|
69 |
+
movie_data = fetch_movie_data(movie_id)
|
70 |
+
|
71 |
with col:
|
72 |
+
st.image(movie_data['poster'], width=130, caption=movie_title)
|
73 |
+
if movie_data['trailer_key']:
|
74 |
+
st.markdown(f"[Watch Trailer](https://www.youtube.com/watch?v={movie_data['trailer_key']})")
|
75 |
+
st.write(movie_data['overview'][:100] + "...")
|