Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,54 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import requests
|
| 4 |
-
import pickle
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
import pickle
|
| 5 |
+
import os
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Load the processed data and similarity matrix
|
| 13 |
+
with open('movie_data.pkl', 'rb') as file:
|
| 14 |
+
movies, cosine_sim = pickle.load(file)
|
| 15 |
+
|
| 16 |
+
# Function to get movie recommendations
|
| 17 |
+
def get_recommendations(title, cosine_sim=cosine_sim):
|
| 18 |
+
idx = movies[movies['title'] == title].index[0]
|
| 19 |
+
sim_scores = list(enumerate(cosine_sim[idx]))
|
| 20 |
+
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
| 21 |
+
sim_scores = sim_scores[1:11] # Get top 10 similar movies
|
| 22 |
+
movie_indices = [i[0] for i in sim_scores]
|
| 23 |
+
return movies[['title', 'movie_id']].iloc[movie_indices]
|
| 24 |
+
|
| 25 |
+
# Fetch movie poster from TMDB API
|
| 26 |
+
def fetch_poster(movie_id):
|
| 27 |
+
api_key = os.getenv("API_KEY") # Replace with your TMDB API key
|
| 28 |
+
url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}'
|
| 29 |
+
response = requests.get(url)
|
| 30 |
+
data = response.json()
|
| 31 |
+
poster_path = data['poster_path']
|
| 32 |
+
full_path = f"https://image.tmdb.org/t/p/w500{poster_path}"
|
| 33 |
+
return full_path
|
| 34 |
+
|
| 35 |
+
# Streamlit UI
|
| 36 |
+
st.title("Movie Recommendation System")
|
| 37 |
+
|
| 38 |
+
selected_movie = st.selectbox("Select a movie:", movies['title'].values)
|
| 39 |
+
|
| 40 |
+
if st.button('Recommend'):
|
| 41 |
+
recommendations = get_recommendations(selected_movie)
|
| 42 |
+
st.write("Top 10 recommended movies:")
|
| 43 |
+
|
| 44 |
+
# Create a 2x5 grid layout
|
| 45 |
+
for i in range(0, 10, 5): # Loop over rows (2 rows, 5 movies each)
|
| 46 |
+
cols = st.columns(5) # Create 5 columns for each row
|
| 47 |
+
for col, j in zip(cols, range(i, i+5)):
|
| 48 |
+
if j < len(recommendations):
|
| 49 |
+
movie_title = recommendations.iloc[j]['title']
|
| 50 |
+
movie_id = recommendations.iloc[j]['movie_id']
|
| 51 |
+
poster_url = fetch_poster(movie_id)
|
| 52 |
+
with col:
|
| 53 |
+
st.image(poster_url, width=130)
|
| 54 |
+
st.write(movie_title)
|