sujoy0011 commited on
Commit
67bc893
·
verified ·
1 Parent(s): 7c953c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -48
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
- # Load the processed data and similarity matrix
7
- with open('movie_data.pkl', 'rb') as file:
8
- movies, cosine_sim = pickle.load(file)
9
-
10
- # Function to get movie recommendations
11
- def get_recommendations(title, cosine_sim=cosine_sim):
12
- idx = movies[movies['title'] == title].index[0]
13
- sim_scores = list(enumerate(cosine_sim[idx]))
14
- sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
15
- sim_scores = sim_scores[1:11] # Get top 10 similar movies
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 = '7b995d3c6fd91a2284b4ad8cb390c7b8' # Replace with 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
- data = response.json()
25
- poster_path = data['poster_path']
26
- full_path = f"https://image.tmdb.org/t/p/w500{poster_path}"
27
- return full_path
28
-
29
- # Streamlit UI
30
- st.title("Movie Recommendation System")
31
-
32
- selected_movie = st.selectbox("Select a movie:", movies['title'].values)
33
-
34
- if st.button('Recommend'):
35
- recommendations = get_recommendations(selected_movie)
36
- st.write("Top 10 recommended movies:")
37
-
38
- # Create a 2x5 grid layout
39
- for i in range(0, 10, 5): # Loop over rows (2 rows, 5 movies each)
40
- cols = st.columns(5) # Create 5 columns for each row
41
- for col, j in zip(cols, range(i, i+5)):
42
- if j < len(recommendations):
43
- movie_title = recommendations.iloc[j]['title']
44
- movie_id = recommendations.iloc[j]['movie_id']
45
- poster_url = fetch_poster(movie_id)
46
- with col:
47
- st.image(poster_url, width=130)
48
- st.write(movie_title)
 
 
 
 
 
 
 
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)