File size: 2,964 Bytes
a91126a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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'])