|
import streamlit as st |
|
import nltk |
|
st.header("New York Times Book recomendations") |
|
st.markdown("Takes a book that user has read as a input and gives recomendation based on top 70 NYT bestsellers.") |
|
book=st.text_input("Enter the book you have read") |
|
def gsearch(book): |
|
a=book.split() |
|
if len(a)==1: |
|
return(a[0]) |
|
else: |
|
abc="" |
|
for i in a: |
|
abc=abc+i+"+" |
|
return(abc[0:len(abc)-1]) |
|
|
|
gs=gsearch(book) |
|
import requests |
|
if st.button("Recommend"): |
|
breakk=0 |
|
data_id=requests.get("https://www.googleapis.com/books/v1/volumes?q="+gs).json() |
|
try: |
|
booka=str(data_id['items'][0]['volumeInfo']['authors'][0]) |
|
desc=str(data_id['items'][0]['volumeInfo']['description']) |
|
genres=str(data_id['items'][0]['volumeInfo']['categories'][0]) |
|
except: |
|
st.markdown("No data for this book. Sorry please try again with another one.") |
|
breakk=1 |
|
if(breakk==0): |
|
tag=genres+" "+booka+" "+desc |
|
tag=tag.lower() |
|
import pickle |
|
ps=pickle.load(open("ps.pkl","rb")) |
|
def stem(a): |
|
y=[] |
|
for i in a.split(): |
|
y.append(ps.stem(i)) |
|
return(" ".join(y)) |
|
tag=stem(tag) |
|
cv=pickle.load(open("cv.pkl","rb")) |
|
vec=pickle.load(open("vec.pkl","rb")) |
|
vec1=cv.transform([tag]) |
|
from scipy.sparse import vstack |
|
fvec=vstack([vec1, vec]) |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
similarity=cosine_similarity(fvec) |
|
distances=similarity[0] |
|
book_list_index=sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[1] |
|
import pandas as pd |
|
from PIL import Image |
|
from io import BytesIO |
|
data=pd.read_csv("streamlitdata.csv") |
|
if (str(data.iloc[book_list_index[0]-1]['title']).lower())==(book.lower()): |
|
st.text("**") |
|
book_list_index=sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1])[2] |
|
st.text("Book recomended-"+data.iloc[book_list_index[0]-1]['title']) |
|
response = requests.get(str(data.iloc[book_list_index[0]-1]['img'])) |
|
img = Image.open(BytesIO(response.content)) |
|
st.image(img) |
|
st.text("Similarity percentage="+str(round(book_list_index[1]*100,2))) |
|
st.text("-------------------------------------") |
|
st.text("SYNOPSIS") |
|
st.text(data.iloc[book_list_index[0]-1]['description']) |
|
else: |
|
st.text("Book recomended-"+data.iloc[book_list_index[0]-1]['title']) |
|
response = requests.get(str(data.iloc[book_list_index[0]-1]['img'])) |
|
img = Image.open(BytesIO(response.content)) |
|
st.image(img) |
|
st.text("Similarity percentage="+str(round(book_list_index[1]*100,2))) |
|
st.text("-------------------------------------") |
|
st.text("SYNOPSIS") |
|
st.markdown(data.iloc[book_list_index[0]-1]['description']) |