-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
68 lines (54 loc) · 2.49 KB
/
app.py
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
from flask import Flask, render_template, request
import pandas as pd
import pickle
import numpy as np
popularity_df = pickle.load(open('popularity.pkl','rb'))
pt = pickle.load(open('pt.pkl','rb'))
books = pickle.load(open('books.pkl','rb'))
similarity_score = pickle.load(open('similarity_score.pkl','rb'))
book_list = pickle.load(open('book_list.pkl','rb'))
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html',
book_name=list(popularity_df['Book-Title'].values),
author=list(popularity_df['Book-Author'].values),
image=list(popularity_df['Image-URL-M'].values),
votes=list(popularity_df['num_ratings'].values),
rating=np.round(list(popularity_df['avg_ratings'].values), 2)
)
@app.route('/recommend')
def recommend_ui():
return render_template('recommend.html')
@app.route('/recommend_books', methods=['POST'])
def recommend():
user_input = request.form.get('user_input')
indi = np.where(pt.index == user_input)[0][0]
print('\n',indi)
print('\n')
# distances = similarity_score[index]
similar_items = sorted(list(enumerate(similarity_score[indi])), key=lambda x: x[1], reverse=True)[1:13]
data = []
# name = []
for i in similar_items:
item = []
# temp_df1 = book_list[book_list['Book-Title'] == pt.index[i[0]]]
temp_df = books[books['Book-Title'] == pt.index[i[0]]]
# name.extend(list(temp_df1.drop_duplicates('Book-Title')['Book-Title'].values))
item.extend(list(temp_df.drop_duplicates('Book-Title')['Book-Title'].values))
item.extend(list(temp_df.drop_duplicates('Book-Title')['Book-Author'].values))
item.extend(list(temp_df.drop_duplicates('Book-Title')['Image-URL-M'].values))
data.append(item)
print(data)
return render_template('recommend.html', data = data)
@app.route('/list')
def listt():
# name = []
# for i in book_list:
# temp_df1 = book_list[book_list['Book-Title'] == book_list.index[i[0]]]
# name.extend(list(temp_df1.drop_duplicates('Book-Title')['Book-Title'].values))
# return render_template('list.html')
return render_template('list.html', name=list(books['Book-Title'][1:201].values))
# return render_template('recommend.html', name = name)
if __name__ == '__main__':
app.run(debug=True)