File size: 4,533 Bytes
e653f5e
 
 
8caef53
 
 
 
e653f5e
8caef53
 
 
b251f08
 
f9dae53
 
8caef53
f9dae53
 
 
8caef53
 
 
f9dae53
8caef53
 
 
 
 
 
 
 
 
 
 
 
e653f5e
 
8caef53
 
 
 
 
 
e653f5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from flask import Flask,render_template,request, flash, redirect, url_for
import pickle
import numpy as np
import os
from dotenv import load_dotenv
import requests
from io import BytesIO

# Load environment variables
load_dotenv()

# Use /tmp directory for file storage
DATA_DIR = '/tmp/bookquest_data'
os.makedirs(DATA_DIR, exist_ok=True)

def load_pickle_from_url(url, local_filename):
    local_path = os.path.join(DATA_DIR, local_filename)
    if os.path.exists(local_path):
        with open(local_path, 'rb') as f:
            return pickle.load(f)
    response = requests.get(url)
    response.raise_for_status()
    with open(local_path, 'wb') as f:
        f.write(response.content)
    return pickle.load(BytesIO(response.content))

POPULAR_URL = "https://huggingface.co/zaiffi/BookQuest/resolve/main/popular.pkl"
PT_URL = "https://huggingface.co/zaiffi/BookQuest/resolve/main/pt.pkl"
BOOKS_URL = "https://huggingface.co/zaiffi/BookQuest/resolve/main/books.pkl"
SIMILARITY_URL = "https://huggingface.co/zaiffi/BookQuest/resolve/main/similarity_scores.pkl"

popular_df = load_pickle_from_url(POPULAR_URL, "popular.pkl")
pt = load_pickle_from_url(PT_URL, "pt.pkl")
books = load_pickle_from_url(BOOKS_URL, "books.pkl")
similarity_scores = load_pickle_from_url(SIMILARITY_URL, "similarity_scores.pkl")

app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY', 'your-secret-key-here')

# EmailJS Configuration
app.config['EMAILJS_PUBLIC_KEY'] = os.getenv('VITE_EMAILJS_PUBLIC_KEY')
app.config['EMAILJS_SERVICE_ID'] = os.getenv('VITE_EMAILJS_SERVICE_ID')
app.config['EMAILJS_TEMPLATE_ID'] = os.getenv('VITE_EMAILJS_TEMPLATE_ID')

@app.route("/")
def index():
    return render_template('index.html',
                           ISBN = list(popular_df['ISBN'].values),
                           book_name = list(popular_df['Book-Title'].values),
                           author = list(popular_df['Book-Author'].values),
                           release_year = list(popular_df['Year-Of-Publication'].values),
                           publisher = list(popular_df['Publisher'].values),
                           image = list(popular_df['Image-URL-M'].values),
                           votes = list(popular_df['num_ratings'].values),
                           rating = list(popular_df['avg_rating'].values)
                           )

@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')
    try:
        # fetching index
        index = np.where(pt.index == user_input)[0][0]
    except IndexError:
        # Book not found, show a friendly error
        return render_template('recommend.html', error=f'No recommendations found for "{user_input}". Please try another book title.')

    similar_items = sorted(list(enumerate(similarity_scores[index])), key=lambda x: x[1], reverse=True)[1:11]

    data = []
    for i in similar_items:
        item = []
        temp_df = books[books['Book-Title'] == pt.index[i[0]]]
        item.extend(list(temp_df.drop_duplicates('Book-Title')['ISBN'].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')['Year-Of-Publication'].values))
        item.extend(list(temp_df.drop_duplicates('Book-Title')['Publisher'].values))
        item.extend(list(temp_df.drop_duplicates('Book-Title')['Image-URL-M'].values))

        data.append(item)
    return render_template('recommend.html', data=data, user_input=user_input)

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    if request.method == 'POST':
        name = request.form.get('name')
        email = request.form.get('email')
        subject = request.form.get('subject')
        message = request.form.get('message')
        
        # Here you would typically handle the form submission
        # For example, sending an email or storing in a database
        # For now, we'll just show a success message
        
        flash('Thank you for your message! We will get back to you soon.', 'success')
        return redirect(url_for('contact'))
    
    return render_template('contact.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)