Jimmie commited on
Commit
7a839dd
1 Parent(s): 3c1ee2f

added the code

Browse files
Files changed (6) hide show
  1. .gitignore +3 -0
  2. app.py +56 -0
  3. books.csv +0 -0
  4. final_model.pkl +3 -0
  5. header.png +0 -0
  6. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ venv/
2
+
3
+ .python-version
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # imports
2
+ import streamlit as st
3
+ from fastai.learner import load_learner
4
+ from fastai.tabular.all import *
5
+ from PIL import Image
6
+
7
+ # Preprocessing of App
8
+ path = Path()
9
+ learn_inf = load_learner(path/'final_model.pkl', cpu=True)
10
+ book_factors = learn_inf.model.i_weight.weight
11
+ img = Image.open('header.png')
12
+ books = pd.read_csv('books.csv')
13
+
14
+
15
+ def selectbox_with_default(text, values, default, sidebar=False):
16
+ func = st.sidebar.selectbox if sidebar else st.selectbox
17
+ return func(text, np.insert(np.array(values, object), 0, default))
18
+
19
+ def get_similar_books(title, number):
20
+ idx = learn_inf.dls.classes['original_title'].o2i[title]
21
+ distances = nn.CosineSimilarity(dim=1)(book_factors, book_factors[idx][None])
22
+ idx = distances.argsort(descending=True)[1:number+1]
23
+ similar = [learn_inf.dls.classes['original_title'][i] for i in idx]
24
+ ids = [int(books.loc[books['original_title']==str(i)]['goodreads_book_id'].values[0]) for i in similar]
25
+ urls = [f'https://www.goodreads.com/book/show/{id}' for id in ids]
26
+ return similar, urls
27
+
28
+
29
+ # APP
30
+ st.image(img, width=200)
31
+ st.title('SIMILAR BOOKS')
32
+ st.subheader('A Book Recommendation System')
33
+ "Here's the [GitHub](https://github.com/jimmiemunyi/SimilarBooks) repo."
34
+
35
+ st.info("Start typing and you will get suggestions of Books we currently have. We Currently have support for 10, 000 Books!")
36
+ title = selectbox_with_default("Which Book Do you want Recommendations From:",
37
+ books['original_title'], default='Select A Book')
38
+ number = st.slider("How many Similar Books do you want?", 1, 10, value=5)
39
+
40
+ if(st.button("Suggest Similar Books")):
41
+ similar, urls = get_similar_books(title, number)
42
+ st.subheader('Here are your Book Recommendations. Enjoy!')
43
+ for book, url in zip(similar, urls):
44
+ st.write(f'{book}: {url}')
45
+
46
+ st.title('Developer Details')
47
+ '''
48
+
49
+ My name is Jimmie Munyi. You can connect with me on [Twitter](https://twitter.com/jimmie_munyi). You can check out other projects I have done from [My GitHub](https://github.com/jimmiemunyi) and from [My Blog](https://jimmiemunyi.github.io/blog/).
50
+
51
+ If you wish to see how Similar Books was created, read this [blog post](https://jimmiemunyi.github.io/blog/projects/tutorial/2021/02/15/Book-Recommendation-Model-Training.html).
52
+
53
+ '''
54
+ del books
55
+
56
+
books.csv ADDED
The diff for this file is too large to render. See raw diff
 
final_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:941a60c4dc13dc42e12f7620fefd1d496d60f022c6cb96bf75bf1261b89e953a
3
+ size 138427415
header.png ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastai>=2
2
+ packaging