Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request | |
| import numpy as np | |
| import pickle | |
| import gzip | |
| # Load models and data | |
| with gzip.open('Model/finaldf_compressed.gz', 'rb') as f: | |
| finaldf = pickle.load(f) | |
| with gzip.open('Model/book_compressed.gz', 'rb') as f: | |
| book = pickle.load(f) | |
| with gzip.open('Model/finalpopularitycompressed.gz', 'rb') as f: | |
| finalpopularity = pickle.load(f) | |
| with gzip.open('Model/similarity_score_compressed.gz', 'rb') as f: | |
| similarityScore = pickle.load(f) | |
| # Initialize Flask app | |
| app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/') | |
| def start(): | |
| return render_template( | |
| "home.html", | |
| bookName=list(finalpopularity["Book-Title"].values), | |
| Image=list(finalpopularity["Image-URL-M"].values), | |
| Isbn=list(finalpopularity["ISBN"].values), | |
| bookauthor=list(finalpopularity["Book-Author"].values), | |
| NoRating=list(finalpopularity["Book-Rating"].values), | |
| ) | |
| def recommend(): | |
| return render_template("recommend.html") | |
| def Userrecommend(): | |
| Userinput = request.form.get("Userinput") | |
| try: | |
| index = np.where(finaldf.index == Userinput)[0][0] | |
| similarBook = sorted( | |
| list(enumerate(similarityScore[index])), | |
| key=lambda x: x[1], | |
| reverse=True | |
| )[1:6] | |
| data = [] | |
| for i in similarBook: | |
| item = [] | |
| tempdf = book[book["Book-Title"] == finaldf.index[i[0]]] | |
| item.extend(list(tempdf.drop_duplicates("Book-Title")["Book-Title"].values)) | |
| item.extend(list(tempdf.drop_duplicates("Book-Title")["Image-URL-M"].values)) | |
| item.extend(list(tempdf.drop_duplicates("Book-Title")["Book-Author"].values)) | |
| item.extend(list(tempdf.drop_duplicates("Book-Title")["ISBN"].values)) | |
| data.append(item) | |
| return render_template("recommend.html", data=data, user_input=Userinput) | |
| except IndexError: | |
| return render_template("recommend.html", error="User input not found!", user_input=Userinput) | |
| if __name__ == "__main__": | |
| # Disable reloader to avoid threading issues | |
| app.run(debug=True, use_reloader=False) | |