-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
70 lines (53 loc) · 1.89 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
60
61
62
63
64
65
66
67
68
69
70
import pickle
import streamlit as st
import numpy as np
st.header('Book Recommender System | Collaborative Filtering')
model = pickle.load(open('Artifacts/Model.pkl','rb'))
book_names = pickle.load(open('Artifacts/Book_Names.pkl','rb'))
final_rating = pickle.load(open('Artifacts/Final_Rating.pkl','rb'))
book_pivot = pickle.load(open('Artifacts/Book_Pivot.pkl','rb'))
def fetch_poster(suggestion):
book_name = []
ids_index = []
poster_url = []
for book_id in suggestion:
book_name.append(book_pivot.index[book_id])
for name in book_name[0]:
ids = np.where(final_rating['Title'] == name)[0][0]
ids_index.append(ids)
for idx in ids_index:
url = final_rating.iloc[idx]['URL']
poster_url.append(url)
return poster_url
def recommend_book(book_name):
books_list = []
book_id = np.where(book_pivot.index == book_name)[0][0]
distance, suggestion = model.kneighbors(book_pivot.iloc[book_id,:].values.reshape(1,-1), n_neighbors=6 )
poster_url = fetch_poster(suggestion)
for i in range(len(suggestion)):
books = book_pivot.index[suggestion[i]]
for j in books:
books_list.append(j)
return books_list , poster_url
selected_books = st.selectbox(
"Type or select a book from the dropdown",
book_names
)
if st.button('Show Recommendation'):
recommended_books,poster_url = recommend_book(selected_books)
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
st.text(recommended_books[1])
st.image(poster_url[1])
with col2:
st.text(recommended_books[2])
st.image(poster_url[2])
with col3:
st.text(recommended_books[3])
st.image(poster_url[3])
with col4:
st.text(recommended_books[4])
st.image(poster_url[4])
with col5:
st.text(recommended_books[5])
st.image(poster_url[5])