-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
60 lines (47 loc) · 2.11 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
from flask import Flask, render_template, request
import numpy as np
import pandas as pd
import pickle
app = Flask(__name__)
@app.route('/')
@app.route('/homepage', methods=['GET', 'POST'])
def homepage():
recommend = 0
loaded_model = pickle.load(open("model/book_recommender.pkl", "rb"))
images = pd.read_csv('datasets/images.csv')
book_pivot = pd.read_csv('datasets/book_pivot.csv')
book_pivot.set_index('title', inplace=True)
book_names = list(book_pivot.index)
if request.method == 'POST':
Id = int(request.form['book'])
distances, suggestions = loaded_model.kneighbors(
book_pivot.iloc[Id, :].values.reshape(1, -1))
suggestions = suggestions[0]
authors = []
years = []
publishers = []
titles = []
isbn_no = []
recommend = 1
choice = []
name = book_names[Id]
choice.append(name)
choice.append(images[images['title'] == name]['author'].values[0])
choice.append(images[images['title'] == name]['year'].values[0])
choice.append(images[images['title'] == name]['publisher'].values[0])
choice.append(images[images['title'] == name]['ISBN'].values[0])
for i in range(len(suggestions)-1):
name = book_pivot.index[suggestions[i+1]]
author = images[images['title'] == name]['author'].values[0]
yr = images[images['title'] == name]['year'].values[0]
publish = images[images['title'] == name]['publisher'].values[0]
isbn = images[images['title'] == name]['ISBN'].values[0]
authors.append(author)
years.append(yr)
publishers.append(publish)
titles.append(name)
isbn_no.append(isbn)
return render_template('homepage.html', book_names=book_names,choice=choice, titles=titles, author=authors, year=years, publisher=publishers, recommend=recommend, isbn_no=isbn_no)
return render_template('homepage.html', book_names=book_names)
if __name__ == '__main__':
app.run(debug=True)