Spaces:
Runtime error
Runtime error
File size: 2,247 Bytes
7a839dd |
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 |
# imports
import streamlit as st
from fastai.tabular.all import *
from PIL import Image
# Preprocessing of App
path = Path()
learn_inf = load_learner(path/'final_model.pkl', cpu=True)
book_factors = learn_inf.model.i_weight.weight
img = Image.open('header.png')
books = pd.read_csv('books.csv')
def selectbox_with_default(text, values, default, sidebar=False):
func = st.sidebar.selectbox if sidebar else st.selectbox
return func(text, np.insert(np.array(values, object), 0, default))
def get_similar_books(title, number):
idx = learn_inf.dls.classes['original_title'].o2i[title]
distances = nn.CosineSimilarity(dim=1)(book_factors, book_factors[idx][None])
idx = distances.argsort(descending=True)[1:number+1]
similar = [learn_inf.dls.classes['original_title'][i] for i in idx]
ids = [int(books.loc[books['original_title']==str(i)]['goodreads_book_id'].values[0]) for i in similar]
urls = [f'https://www.goodreads.com/book/show/{id}' for id in ids]
return similar, urls
# APP
st.image(img, width=200)
st.title('SIMILAR BOOKS')
st.subheader('A Book Recommendation System')
"Here's the [GitHub](https://github.com/jimmiemunyi/SimilarBooks) repo."
st.info("Start typing and you will get suggestions of Books we currently have. We Currently have support for 10, 000 Books!")
title = selectbox_with_default("Which Book Do you want Recommendations From:",
books['original_title'], default='Select A Book')
number = st.slider("How many Similar Books do you want?", 1, 10, value=5)
if(st.button("Suggest Similar Books")):
similar, urls = get_similar_books(title, number)
st.subheader('Here are your Book Recommendations. Enjoy!')
for book, url in zip(similar, urls):
st.write(f'{book}: {url}')
st.title('Developer Details')
'''
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/).
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).
'''
|