-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
380 lines (295 loc) · 10.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import streamlit as st
import requests
import pickle
import numpy as np
from PIL import Image
# Load necessary data using pickle
book_list = pickle.load(open('pivot.pkl','rb')) # Load pivot.pkl file using pickle to get the list of books
book_list = book_list.index # Get the index of the book list
similarity_score = pickle.load(open('similarity.pkl','rb')) # Load similarity.pkl file using pickle to get the similarity scores
whole_book = pickle.load(open('whole_book_data.pkl','rb')) # Load whole_book_data.pkl file using pickle to get the whole book data
top50 = pickle.load(open('top50.pkl','rb')) # Load top50.pkl file using pickle to get the top 50 book data
top50_img = top50['Image-URL-M'].reset_index() # Get the image URL for the top 50 books
top50_img = top50_img['Image-URL-M']
top50_tit = top50['Book-Title'].reset_index() # Get the book title for the top 50 books
top50_tit = top50_tit['Book-Title']
top50_auth = top50['Book-Author'].reset_index() # Get the book author for the top 50 books
top50_auth = top50_auth['Book-Author']
# Open the image for the app
image = Image.open('myimage.jpg')
# Create a title and sidebar for the app
st.title('Book Shelf')
st.sidebar.write('Built By -')
st.sidebar.title('Rishabh Vyas')
st.sidebar.image(image,caption='Machine Learning Engineer',width=160)
st.sidebar.write('E-mail - [email protected]')
# Create two tabs for the app - Book Recommender and Top Books
tab1,tab2= st.tabs(['Book Recommender','Top Books'])
# Define a function to recommend books based on the input book
def recomend(book_name):
# Get the index of the input book in the book list
index_num = np.where(book_list == book_name)[0][0]
# Get the similarity scores for the input book
similarity = similarity_score[index_num]
# Sort the similarity scores and get the top 6 books
sorted_score = sorted(list(enumerate(similarity)),key = lambda x:x[1],reverse=True)[1:7]
# Create empty lists for the recommended book's title, author, and image
title =[]
author = []
images =[]
# For each book in the top 6 recommended books, get the title, author, and image
for i in sorted_score:
title.append(book_list[i[0]])
author.append(list(whole_book[whole_book['Book-Title']==book_list[i[0]]].drop_duplicates('Book-Title')['Book-Author'].values))
images.append(list(whole_book[whole_book['Book-Title']==book_list[i[0]]].drop_duplicates('Book-Title')['Image-URL-M'].values))
# Return the recommended books' title, author, and image
return title,author,images
with tab1:
st.header('Book Recommender')
select_books = st.selectbox(
'Selecte the book from the list or type name',
book_list,index=0)
if st.button('Recommend'):
st.header('These are the 5 books you would love to read')
tit,auth,img = recomend(select_books)
col1,col2,col3,col4,col5,col6 = st.columns(6,gap="small")
with col1:
st.image(img[0][0])
st.write(tit[0])
st.caption('Author:'+auth[0][0])
with col2:
st.image(img[1][0])
st.write(tit[1])
st.caption('Author:'+auth[1][0])
with col3:
st.image(img[2][0])
st.write(tit[2])
st.caption('Author:'+auth[2][0])
with col4:
st.image(img[3][0])
st.write(tit[3])
st.caption('Author:'+auth[3][0])
with col5:
st.image(img[4][0])
st.write(tit[4])
st.caption('Author:'+auth[4][0])
with col6:
st.image(img[5][0])
st.write(tit[5])
st.caption('Author:'+auth[5][0])
with tab2:
st.header('Top 50 Books')
col1, col2, col3, col4, col5 = st.columns(5, gap="small")
with col1:
st.image(top50_img[0])
st.write(top50_tit[0])
st.caption('Author:' + top50_auth[0])
with col2:
st.image(top50_img[1])
st.write(top50_tit[1])
st.caption('Author:' + top50_auth[1])
with col3:
st.image(top50_img[2])
st.write(top50_tit[2])
st.caption('Author:' + top50_auth[2])
with col4:
st.image(top50_img[3])
st.write(top50_tit[3])
st.caption('Author:' + top50_auth[3])
with col5:
st.image(top50_img[4])
st.write(top50_tit[4])
st.caption('Author:' + top50_auth[4])
col6, col7, col8, col9, col10 = st.columns(5, gap="small")
with col6:
st.image(top50_img[5])
st.write(top50_tit[5])
st.caption('Author:' + top50_auth[5])
with col7:
st.image(top50_img[6])
st.write(top50_tit[6])
st.caption('Author:' + top50_auth[6])
with col8:
st.image(top50_img[7])
st.write(top50_tit[7])
st.caption('Author:' + top50_auth[7])
with col9:
st.image(top50_img[8])
st.write(top50_tit[8])
st.caption('Author:' + top50_auth[8])
with col10:
st.image(top50_img[9])
st.write(top50_tit[9])
st.caption('Author:' + top50_auth[9])
# ------------------Second row--------------------#
col11, col12, col13, col14, col15 = st.columns(5, gap="small")
with col11:
st.image(top50_img[10])
st.write(top50_tit[10])
st.caption('Author:' + top50_auth[10])
with col12:
st.image(top50_img[11])
st.write(top50_tit[11])
st.caption('Author:' + top50_auth[11])
with col13:
st.image(top50_img[12])
st.write(top50_tit[12])
st.caption('Author:' + top50_auth[12])
with col14:
st.image(top50_img[13])
st.write(top50_tit[13])
st.caption('Author:' + top50_auth[13])
with col15:
st.image(top50_img[14])
st.write(top50_tit[14])
st.caption('Author:' + top50_auth[14])
col16, col17, col18, col19, col20 = st.columns(5, gap="small")
with col16:
st.image(top50_img[15])
st.write(top50_tit[15])
st.caption('Author:' + top50_auth[15])
with col17:
st.image(top50_img[16])
st.write(top50_tit[16])
st.caption('Author:' + top50_auth[16])
with col18:
st.image(top50_img[17])
st.write(top50_tit[17])
st.caption('Author:' + top50_auth[17])
with col19:
st.image(top50_img[18])
st.write(top50_tit[18])
st.caption('Author:' + top50_auth[18])
with col20:
st.image(top50_img[19])
st.write(top50_tit[19])
st.caption('Author:' + top50_auth[19])
# -----------Third row---------------#
col21, col22, col23, col24, col25 = st.columns(5, gap="small")
with col21:
st.image(top50_img[20])
st.write(top50_tit[20])
st.caption('Author:' + top50_auth[20])
with col22:
st.image(top50_img[21])
st.write(top50_tit[21])
st.caption('Author:' + top50_auth[21])
with col23:
st.image(top50_img[22])
st.write(top50_tit[22])
st.caption('Author:' + top50_auth[22])
with col24:
st.image(top50_img[23])
st.write(top50_tit[23])
st.caption('Author:' + top50_auth[23])
with col25:
st.image(top50_img[24])
st.write(top50_tit[24])
st.caption('Author:' + top50_auth[24])
col26, col27, col28, col29, col30 = st.columns(5, gap="small")
with col26:
st.image(top50_img[25])
st.write(top50_tit[25])
st.caption('Author:' + top50_auth[25])
with col27:
st.image(top50_img[26])
st.write(top50_tit[26])
st.caption('Author:' + top50_auth[26])
with col28:
st.image(top50_img[27])
st.write(top50_tit[27])
st.caption('Author:' + top50_auth[27])
with col29:
st.image(top50_img[28])
st.write(top50_tit[28])
st.caption('Author:' + top50_auth[28])
with col30:
st.image(top50_img[29])
st.write(top50_tit[29])
st.caption('Author:' + top50_auth[29])
# ---------Fourth Row------------#
col31, col32, col33, col34, col35 = st.columns(5, gap="small")
with col31:
st.image(top50_img[30])
st.write(top50_tit[30])
st.caption('Author:' + top50_auth[30])
with col32:
st.image(top50_img[31])
st.write(top50_tit[31])
st.caption('Author:' + top50_auth[31])
with col33:
st.image(top50_img[32])
st.write(top50_tit[32])
st.caption('Author:' + top50_auth[32])
with col34:
st.image(top50_img[33])
st.write(top50_tit[33])
st.caption('Author:' + top50_auth[33])
with col35:
st.image(top50_img[34])
st.write(top50_tit[34])
st.caption('Author:' + top50_auth[34])
col36, col37, col38, col39, col40 = st.columns(5, gap="small")
with col36:
st.image(top50_img[35])
st.write(top50_tit[35])
st.caption('Author:' + top50_auth[35])
with col37:
st.image(top50_img[36])
st.write(top50_tit[36])
st.caption('Author:' + top50_auth[36])
with col38:
st.image(top50_img[37])
st.write(top50_tit[37])
st.caption('Author:' + top50_auth[37])
with col39:
st.image(top50_img[38])
st.write(top50_tit[38])
st.caption('Author:' + top50_auth[38])
with col40:
st.image(top50_img[39])
st.write(top50_tit[39])
st.caption('Author:' + top50_auth[39])
# -------------Fifth Row-----------#
col41, col42, col43, col44, col45 = st.columns(5, gap="small")
with col41:
st.image(top50_img[40])
st.write(top50_tit[40])
st.caption('Author:' + top50_auth[40])
with col42:
st.image(top50_img[41])
st.write(top50_tit[41])
st.caption('Author:' + top50_auth[41])
with col43:
st.image(top50_img[42])
st.write(top50_tit[42])
st.caption('Author:' + top50_auth[42])
with col44:
st.image(top50_img[43])
st.write(top50_tit[43])
st.caption('Author:' + top50_auth[43])
with col45:
st.image(top50_img[44])
st.write(top50_tit[44])
st.caption('Author:' + top50_auth[44])
col46, col47, col48, col49, col50 = st.columns(5, gap="small")
with col46:
st.image(top50_img[45])
st.write(top50_tit[45])
st.caption('Author:' + top50_auth[45])
with col47:
st.image(top50_img[46])
st.write(top50_tit[46])
st.caption('Author:' + top50_auth[46])
with col48:
st.image(top50_img[47])
st.write(top50_tit[47])
st.caption('Author:' + top50_auth[47])
with col49:
st.image(top50_img[48])
st.write(top50_tit[48])
st.caption('Author:' + top50_auth[48])
with col50:
st.image(top50_img[49])
st.write(top50_tit[49])
st.caption('Author:' + top50_auth[49])