-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
163 lines (123 loc) · 5.6 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import streamlit as st
import pickle,csv
import pandas as pd
import os , papermill as pm
# Set the app title and favicon
st.set_page_config(page_title='Book Recommendation System', page_icon='📚', layout='wide')
# Run .ipynb file if model doesn't contain the final_data & cosine_sim_desc
@st.cache_resource()
def model_generate(path):
final_data = pd.read_csv(path)
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel
from sklearnex import patch_sklearn
patch_sklearn()
# Create a TF-IDF Vectorizer for the 'desc' column
tfidf_vectorizer = TfidfVectorizer(stop_words='english', max_features=7500)
# To check Output from above code:
# print(f"Final Data Null Values: {final_data['Desc'].isnull().sum()}")
# print(f"Lenght of Final Data: {len(final_data)}")
# print(f"TfidfVectorizer: {tfidf_vectorizer}")
# Apply the TF-IDF vectorizer to the 'desc' column
tfidf_matrix_desc = tfidf_vectorizer.fit_transform(final_data['Title'] +" " + final_data['Genre'])
# print(f"tfidf_matrix_desc: {tfidf_matrix_desc}") # To check Output from above code
# Convert the data type to float32
tfidf_matrix_desc = tfidf_matrix_desc.astype(np.float32)
# print(f"tfidf_matrix_desc: {tfidf_matrix_desc}") # To check Output from above code
# Compute the cosine similarity matrix for book descriptions
cosine_sim_desc = linear_kernel(tfidf_matrix_desc, tfidf_matrix_desc)
# print(f"cosine_sim_desc: {cosine_sim_desc}") # To check Output from above code
# Save the cosine_sim_desc matrix to a pickle file
pickle.dump(cosine_sim_desc, open('model/cosine_sim_desc.pkl', 'wb'), protocol=4)
# Execute the IPython Notebook
if not os.path.exists('model/final_data.csv'):
warn = st.warning('Models not found! Running the notebook to create models...')
pm.execute_notebook(
'recommendation_data_clean.ipynb',
'output.ipynb',
)
warn.empty()
if not os.path.exists('model/cosine_sim_desc.pkl'):
warn = st.warning('Models not found! Running the notebook to create models...')
model_generate('model/final_data.csv')
warn.empty()
# Function to load the pickled model
@st.cache_resource()
def load_models():
cosine_sim_desc = pickle.load(open('model/cosine_sim_desc.pkl', 'rb'))
final_data = pd.read_csv('model/final_data.csv')
# final_data = pickle.load(open('model/final_data.pkl', 'rb'))
return cosine_sim_desc, final_data
cosine_sim_desc, final_data = load_models()
# Get the list of book titles from the final_data DataFrame using pandas
options = final_data['Title'].values.tolist()
# print(options[:5]) # Output check
# Create the Streamlit app
def main():
# Set the app title
st.title('Book Recommendation System')
# Add a dropdown to the main content
selected_option = st.selectbox('Select an option', pd.Series(options).sort_values().unique())
# Display the selected option
st.write('You selected:', selected_option)
def get_recommendations(book_title, cosine_sim_desc):
# Check if the final_data DataFrame is empty
if not final_data.empty:
# Get the index of the book title
idx = final_data[final_data['Title'] == book_title].index
# print(f"idx: {idx}") output check
if len(idx) > 0:
idx = idx[0]
sim_scores = list(enumerate(cosine_sim_desc[idx]))
# print(f"sim_scores: {sim_scores}") # output check
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:6]
# print(f"sim_scores top 10: {sim_scores}") # output check
book_indices = [i[0] for i in sim_scores]
# print(f"book_indices: {book_indices}") # output check
return final_data[['Title', 'Image', 'Author','Pages']].iloc[book_indices] # return book title, image and author
else:
return "Book not found"
else:
return "No data available"
# Display book recommendations
st.subheader('Recommended Books')
# Display book with image fetch from image url
book = get_recommendations(selected_option, cosine_sim_desc)
# align books in a row
col1, col2, col3, col4, col5 = st.columns(5,gap='large')
with col1:
st.image(book.iloc[0, 1],caption=book.iloc[0,0], width=150)
# st.write(book.iloc[0, 0])
st.write(book.iloc[0, 2])
st.write("Pages: ",book.iloc[0, 3])
with col2:
st.image(book.iloc[1, 1],caption=book.iloc[1,0], width=150)
# st.write(book.iloc[1, 0])
st.write(book.iloc[1, 2])
st.write("Pages: ",book.iloc[1, 3])
with col3:
st.image(book.iloc[2, 1], caption=book.iloc[2,0],width=150)
# st.write(book.iloc[2, 0])
st.write(book.iloc[2, 2])
st.write("Pages: ",book.iloc[2, 3])
with col4:
st.image(book.iloc[3, 1],caption=book.iloc[3,0], width=150)
# st.write(book.iloc[3, 0])
st.write(book.iloc[3, 2])
st.write("Pages: ",book.iloc[3, 3])
with col5:
st.image(book.iloc[4, 1], caption=book.iloc[4,0],width=150)
# st.write(book.iloc[4, 0])
st.write(book.iloc[4, 2])
st.write("Pages: ", book.iloc[4, 3])
# Books Recommended in a column
# for i in range(5):
# st.image(book.iloc[i, 1], width=150)
# st.write(book.iloc[i, 0])
# st.write(book.iloc[i, 2])
# st.write(book.iloc[i, 3])
# st.write('______')
if __name__ == '__main__':
main()