-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (46 loc) · 1.97 KB
/
app.py
1
2
3
4
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
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import pickle
import pandas
from utils import fetch_poster, recommend, improved_recommendations
st.header('Movie Recommender System Using Machine Learning')
# Load movies_df
with open('src/movies_df.pkl', 'rb') as f:
movies = pickle.load(f)
# Load cosine_sim
with open('src/cosine_sim.pkl', 'rb') as f:
similarity = pickle.load(f)
movie_list = movies['title'].values
selected_movie = st.selectbox(
"Type or select a movie from the dropdown",
movie_list
)
if st.button('Show Recommendation'):
recommended_movie_names, recommended_movie_posters = recommend(selected_movie,movies,similarity)
num_cols = 5
num_recommendations = 10
col_list = st.columns(num_cols)
for i in range(num_recommendations):
col_index = i % num_cols
with col_list[col_index]:
st.text(recommended_movie_names[i])
st.image(recommended_movie_posters[i])
if st.button('Show Improved Recommendations'):
recommendations = improved_recommendations(selected_movie,movies,similarity)
if recommendations is not None:
recommended_movie_ids = recommendations['id'].tolist()
recommended_movie_names = recommendations['title'].tolist()
recommended_movie_posters = []
for movie_id in recommended_movie_ids:
recommended_movie_posters.append(fetch_poster(movie_id))
num_recommendations = len(recommended_movie_ids)
num_cols = 5
# Calculate the number of rows needed
num_rows = (num_recommendations + num_cols - 1) // num_cols
for row in range(num_rows):
col_list = st.columns(num_cols)
start_index = row * num_cols
end_index = min((row + 1) * num_cols, num_recommendations)
for i in range(start_index, end_index):
with col_list[i - start_index]:
st.text(recommended_movie_names[i])
st.image(recommended_movie_posters[i])