Abs6187 commited on
Commit
8c68bab
·
verified ·
1 Parent(s): ebb8988

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
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 from TMDB API
20
- def fetch_poster(movie_id):
21
- api_key = "d52d86933103ae578bbf057ec39d012e" # Your TMDB API key
22
- url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}'
23
- response = requests.get(url)
24
 
25
- # Check if the request was successful
26
- if response.status_code != 200:
27
- return "https://via.placeholder.com/500x750?text=No+Image+Available" # Placeholder for errors
28
-
29
- data = response.json()
30
-
31
- # Safely access 'poster_path'
32
- poster_path = data.get('poster_path')
33
- if poster_path:
34
- return f"https://image.tmdb.org/t/p/w500{poster_path}"
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Return placeholder if 'poster_path' is missing
37
- return "https://via.placeholder.com/500x750?text=No+Image+Available"
 
 
 
 
 
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
- poster_url = fetch_poster(movie_id)
 
56
  with col:
57
- st.image(poster_url, width=130)
58
- st.write(movie_title)
 
 
 
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] + "...")