Spaces:
No application file
No application file
main file
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
import gzip
|
| 5 |
+
|
| 6 |
+
# Load models and data
|
| 7 |
+
with gzip.open('Model/finaldf_compressed.gz', 'rb') as f:
|
| 8 |
+
finaldf = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
with gzip.open('Model/book_compressed.gz', 'rb') as f:
|
| 11 |
+
book = pickle.load(f)
|
| 12 |
+
|
| 13 |
+
with gzip.open('Model/finalpopularitycompressed.gz', 'rb') as f:
|
| 14 |
+
finalpopularity = pickle.load(f)
|
| 15 |
+
|
| 16 |
+
with gzip.open('Model/similarity_score_compressed.gz', 'rb') as f:
|
| 17 |
+
similarityScore = pickle.load(f)
|
| 18 |
+
|
| 19 |
+
# Initialize Flask app
|
| 20 |
+
app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/')
|
| 21 |
+
|
| 22 |
+
@app.route("/")
|
| 23 |
+
def start():
|
| 24 |
+
return render_template(
|
| 25 |
+
"home.html",
|
| 26 |
+
bookName=list(finalpopularity["Book-Title"].values),
|
| 27 |
+
Image=list(finalpopularity["Image-URL-M"].values),
|
| 28 |
+
Isbn=list(finalpopularity["ISBN"].values),
|
| 29 |
+
bookauthor=list(finalpopularity["Book-Author"].values),
|
| 30 |
+
NoRating=list(finalpopularity["Book-Rating"].values),
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
@app.route("/recommend")
|
| 34 |
+
def recommend():
|
| 35 |
+
return render_template("recommend.html")
|
| 36 |
+
|
| 37 |
+
@app.route("/Userrecommend", methods=["POST"])
|
| 38 |
+
def Userrecommend():
|
| 39 |
+
Userinput = request.form.get("Userinput")
|
| 40 |
+
try:
|
| 41 |
+
index = np.where(finaldf.index == Userinput)[0][0]
|
| 42 |
+
similarBook = sorted(
|
| 43 |
+
list(enumerate(similarityScore[index])),
|
| 44 |
+
key=lambda x: x[1],
|
| 45 |
+
reverse=True
|
| 46 |
+
)[1:6]
|
| 47 |
+
|
| 48 |
+
data = []
|
| 49 |
+
for i in similarBook:
|
| 50 |
+
item = []
|
| 51 |
+
tempdf = book[book["Book-Title"] == finaldf.index[i[0]]]
|
| 52 |
+
item.extend(list(tempdf.drop_duplicates("Book-Title")["Book-Title"].values))
|
| 53 |
+
item.extend(list(tempdf.drop_duplicates("Book-Title")["Image-URL-M"].values))
|
| 54 |
+
item.extend(list(tempdf.drop_duplicates("Book-Title")["Book-Author"].values))
|
| 55 |
+
item.extend(list(tempdf.drop_duplicates("Book-Title")["ISBN"].values))
|
| 56 |
+
data.append(item)
|
| 57 |
+
|
| 58 |
+
return render_template("recommend.html", data=data, user_input=Userinput)
|
| 59 |
+
|
| 60 |
+
except IndexError:
|
| 61 |
+
return render_template("recommend.html", error="User input not found!", user_input=Userinput)
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
# Disable reloader to avoid threading issues
|
| 65 |
+
app.run(debug=True, use_reloader=False)
|