Spaces:
Build error
Build error
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| import pickle | |
| # Load the processed data and similarity matrix | |
| with open('movie_data.pkl', 'rb') as file: | |
| movies, cosine_sim = pickle.load(file) | |
| # Function to get movie recommendations | |
| def get_recommendations(title, cosine_sim=cosine_sim): | |
| idx = movies[movies['title'] == title].index[0] | |
| sim_scores = list(enumerate(cosine_sim[idx])) | |
| sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True) | |
| sim_scores = sim_scores[1:11] # Get top 10 similar movies | |
| movie_indices = [i[0] for i in sim_scores] | |
| return movies[['title', 'movie_id']].iloc[movie_indices] | |
| # Fetch movie poster from TMDB API | |
| def fetch_poster(movie_id): | |
| api_key = "d52d86933103ae578bbf057ec39d012e" # Your TMDB API key | |
| url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}' | |
| response = requests.get(url) | |
| # Check if the request was successful | |
| if response.status_code != 200: | |
| return "https://via.placeholder.com/500x750?text=No+Image+Available" # Placeholder for errors | |
| data = response.json() | |
| # Safely access 'poster_path' | |
| poster_path = data.get('poster_path') | |
| if poster_path: | |
| return f"https://image.tmdb.org/t/p/w500{poster_path}" | |
| # Return placeholder if 'poster_path' is missing | |
| return "https://via.placeholder.com/500x750?text=No+Image+Available" | |
| # Streamlit UI | |
| st.title("Movie Recommendation System") | |
| selected_movie = st.selectbox("Select a movie:", movies['title'].values) | |
| if st.button('Recommend'): | |
| recommendations = get_recommendations(selected_movie) | |
| st.write("Top 10 recommended movies:") | |
| # Create a 2x5 grid layout | |
| for i in range(0, 10, 5): # Loop over rows (2 rows, 5 movies each) | |
| cols = st.columns(5) # Create 5 columns for each row | |
| for col, j in zip(cols, range(i, i+5)): | |
| if j < len(recommendations): | |
| movie_title = recommendations.iloc[j]['title'] | |
| movie_id = recommendations.iloc[j]['movie_id'] | |
| poster_url = fetch_poster(movie_id) | |
| with col: | |
| st.image(poster_url, width=130) | |
| st.write(movie_title) | |