conlan commited on
Commit
6f53ea2
·
1 Parent(s): 3fa3306

Add models and gitignore

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .DS_Store
app.py CHANGED
@@ -2,79 +2,78 @@ import pickle
2
  import streamlit as st
3
  import numpy as np
4
 
5
-
6
  st.header("Book Recommender System")
7
 
8
- # model = pickle.load(open("artifacts/model.pkl", "rb"))
9
- # book_names = pickle.load(open("artifacts/book_names.pkl", "rb"))
10
- # final_ratings = pickle.load(open("artifacts/final_ratings.pkl", "rb"))
11
- # book_pivot = pickle.load(open("artifacts/book_pivot.pkl", "rb"))
12
 
13
- # def fetch_poster(suggestion):
14
- # bookNames = []
15
- # idsIndex = []
16
- # posterUrl = []
17
 
18
- # for bookId in suggestion[0]:
19
- # name = book_pivot.index[bookId]
20
 
21
- # bookNames.append(book_pivot.index[bookId])
22
 
23
- # for name in bookNames:
24
- # ids = np.where(final_ratings['title'] == name)[0][0]
25
- # idsIndex.append(ids)
26
 
27
- # for idx in idsIndex:
28
- # row = final_ratings.iloc[idx]
29
- # url = row['img_url']
30
- # posterUrl.append(url)
31
 
32
- # return posterUrl
33
 
34
- # def recommend_book(bookName):
35
- # bookList = []
36
 
37
- # book_id = np.where(book_pivot.index == bookName)[0][0]
38
 
39
- # distance, suggestion = model.kneighbors(book_pivot.iloc[book_id,:].values.reshape(1, -1), n_neighbors=5)
40
 
41
- # poster_url = fetch_poster(suggestion)
42
 
43
- # for i in range(len(suggestion)):
44
- # books = book_pivot.index[suggestion[i]]
45
 
46
- # for j in books:
47
- # bookList.append(j)
48
 
49
- # return bookList, poster_url
50
 
51
- # selected_books = st.selectbox(
52
- # "Select a book",
53
- # book_names
54
- # )
55
 
56
- # if st.button("Show Recommendations"):
57
- # recommendations, posterUrls = recommend_book(selected_books)
58
 
59
- # st.subheader("Recommendations")
60
 
61
- # col1, col2, col3, col4 = st.columns(4)
62
 
63
- # for url in posterUrls:
64
- # print(url)
65
 
66
- # with col1:
67
- # st.text(recommendations[1])
68
- # st.image(posterUrls[1])
69
 
70
- # with col2:
71
- # st.text(recommendations[2])
72
- # st.image(posterUrls[2])
73
 
74
- # with col3:
75
- # st.text(recommendations[3])
76
- # st.image(posterUrls[3])
77
 
78
- # with col4:
79
- # st.text(recommendations[4])
80
- # st.image(posterUrls[4])
 
2
  import streamlit as st
3
  import numpy as np
4
 
 
5
  st.header("Book Recommender System")
6
 
7
+ model = pickle.load(open("artifacts/model.pkl", "rb"))
8
+ book_names = pickle.load(open("artifacts/book_names.pkl", "rb"))
9
+ final_ratings = pickle.load(open("artifacts/final_ratings.pkl", "rb"))
10
+ book_pivot = pickle.load(open("artifacts/book_pivot.pkl", "rb"))
11
 
12
+ def fetch_poster(suggestion):
13
+ bookNames = []
14
+ idsIndex = []
15
+ posterUrl = []
16
 
17
+ for bookId in suggestion[0]:
18
+ name = book_pivot.index[bookId]
19
 
20
+ bookNames.append(book_pivot.index[bookId])
21
 
22
+ for name in bookNames:
23
+ ids = np.where(final_ratings['title'] == name)[0][0]
24
+ idsIndex.append(ids)
25
 
26
+ for idx in idsIndex:
27
+ row = final_ratings.iloc[idx]
28
+ url = row['img_url']
29
+ posterUrl.append(url)
30
 
31
+ return posterUrl
32
 
33
+ def recommend_book(bookName):
34
+ bookList = []
35
 
36
+ book_id = np.where(book_pivot.index == bookName)[0][0]
37
 
38
+ distance, suggestion = model.kneighbors(book_pivot.iloc[book_id,:].values.reshape(1, -1), n_neighbors=5)
39
 
40
+ poster_url = fetch_poster(suggestion)
41
 
42
+ for i in range(len(suggestion)):
43
+ books = book_pivot.index[suggestion[i]]
44
 
45
+ for j in books:
46
+ bookList.append(j)
47
 
48
+ return bookList, poster_url
49
 
50
+ selected_books = st.selectbox(
51
+ "Select a book",
52
+ book_names
53
+ )
54
 
55
+ if st.button("Show Recommendations"):
56
+ recommendations, posterUrls = recommend_book(selected_books)
57
 
58
+ st.subheader("Recommendations")
59
 
60
+ col1, col2, col3, col4 = st.columns(4)
61
 
62
+ for url in posterUrls:
63
+ print(url)
64
 
65
+ with col1:
66
+ st.text(recommendations[1])
67
+ st.image(posterUrls[1])
68
 
69
+ with col2:
70
+ st.text(recommendations[2])
71
+ st.image(posterUrls[2])
72
 
73
+ with col3:
74
+ st.text(recommendations[3])
75
+ st.image(posterUrls[3])
76
 
77
+ with col4:
78
+ st.text(recommendations[4])
79
+ st.image(posterUrls[4])
artifacts/book_names.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:794d08c3eb7ee65bf95008096b99fdcba5376e3794e313ade38d5781451e46a2
3
+ size 20131
artifacts/book_pivot.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a84ce7b17434dc9cf1740e636e9c2a795f5c87f19b7b7e7414b3e8b2da5c97c0
3
+ size 5298853
artifacts/final_ratings.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f47e068289bf4cc38a6240dda847abcb487c5ecd5f8ce1924fc2149692f56ab6
3
+ size 3811459
artifacts/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1aa3452bce5d5c6bee719a6aac3490a2cf39c0f417388a31893ffdc0df6f3317
3
+ size 183212