import pickle import streamlit as st import numpy as np # Load artifacts model = pickle.load(open('artifacts/model.pkl', 'rb')) book_names = pickle.load(open('artifacts/book_names.pkl', 'rb')) final_rating = pickle.load(open('artifacts/final_rating.pkl', 'rb')) book_pivot = pickle.load(open('artifacts/book_pivot.pkl', 'rb')) # Function to fetch poster URLs def fetch_poster(suggestion): book_name = [] ids_index = [] poster_url = [] for book_id in suggestion: book_name.append(book_pivot.index[book_id]) for name in book_name[0]: ids = np.where(final_rating['title'] == name)[0][0] ids_index.append(ids) for idx in ids_index: url = final_rating.iloc[idx]['image_url'] poster_url.append(url) return poster_url # Function to recommend books def recommend_book(book_name): books_list = [] book_id = np.where(book_pivot.index == book_name)[0][0] distance, suggestion = model.kneighbors(book_pivot.iloc[book_id, :].values.reshape(1, -1), n_neighbors=4) poster_url = fetch_poster(suggestion) for i in range(len(suggestion[0])): book = book_pivot.index[suggestion[0][i]] books_list.append(book) return books_list[1:], poster_url[1:] # Skip the input book itself # Apply custom CSS for glassmorphism def local_css(file_name): with open(file_name) as f: st.markdown(f'', unsafe_allow_html=True) # Page configuration st.set_page_config(page_title='Book Recommendation System', layout='centered') # Load CSS for styling local_css("style.css") # Add a fancy header with emojis st.markdown("""

📚Book Finder📚

Let AI find your perfect book match.

""", unsafe_allow_html=True) # Centered dropdown for book selection with glass effect st.markdown("""

🔎 Type or select a book from the dropdown

""", unsafe_allow_html=True) # Dropdown for book selection selected_books = st.selectbox("", book_names) # Apply custom button styling to Streamlit's default button using CSS st.markdown(""" """, unsafe_allow_html=True) # Streamlit button with custom styling applied if st.button('Show Recommendation', key='show_recommendation_button'): recommended_books, poster_url = recommend_book(selected_books) # Display recommendations in a grid st.markdown("

Recommended Books:

", unsafe_allow_html=True) col1, col2, col3 = st.columns(3) with col1: st.markdown(f"

{recommended_books[0]}

", unsafe_allow_html=True) st.image(poster_url[0]) with col2: st.markdown(f"

{recommended_books[1]}

", unsafe_allow_html=True) st.image(poster_url[1]) with col3: st.markdown(f"

{recommended_books[2]}

", unsafe_allow_html=True) st.image(poster_url[2])