-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
118 lines (91 loc) · 3.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import sys
import pickle
import streamlit as st
import numpy as np
from books_recommender.logger.log import logging
from books_recommender.config.configuration import AppConfiguration
from books_recommender.pipeline.training_pipeline import TrainingPipeline
from books_recommender.exception.exception_handler import AppException
class Recommendation:
def __init__(self,app_config = AppConfiguration()):
try:
self.recommendation_config= app_config.get_recommendation_config()
except Exception as e:
raise AppException(e, sys) from e
def fetch_poster(self,suggestion):
try:
book_name = []
ids_index = []
poster_url = []
book_pivot = pickle.load(open(self.recommendation_config.book_pivot_serialized_objects,'rb'))
final_rating = pickle.load(open(self.recommendation_config.final_rating_serialized_objects,'rb'))
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]['image_url']
poster_url.append(url)
return poster_url
except Exception as e:
raise AppException(e, sys) from e
def recommend_book(self,book_name):
try:
books_list = []
model = pickle.load(open(self.recommendation_config.trained_model_path,'rb'))
book_pivot = pickle.load(open(self.recommendation_config.book_pivot_serialized_objects,'rb'))
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 = self.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
except Exception as e:
raise AppException(e, sys) from e
def train_engine(self):
try:
obj = TrainingPipeline()
obj.start_training_pipeline()
st.text("Training Completed!")
logging.info(f"Recommended successfully!")
except Exception as e:
raise AppException(e, sys) from e
def recommendations_engine(self,selected_books):
try:
recommended_books,poster_url = self.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])
except Exception as e:
raise AppException(e, sys) from e
if __name__ == "__main__":
st.header('ML Based Books Recommender System')
st.text("This is a collaborative filtering based recommendation system!")
obj = Recommendation()
#Training
if st.button('Train Recommender System'):
obj.train_engine()
book_names = pickle.load(open(os.path.join('templates','book_names.pkl') ,'rb'))
selected_books = st.selectbox(
"Type or select a book from the dropdown",
book_names)
#recommendation
if st.button('Show Recommendation'):
obj.recommendations_engine(selected_books)