Spaces:
Build error
Build error
| import streamlit as st | |
| import numpy as np | |
| from joblib import load | |
| st.title("Book Recommendation System") | |
| # Load models and data | |
| model = load("model.pkl") | |
| books_name = load("book_names.pkl") | |
| final_rating = load("final_rating.pkl") | |
| book_pivot_tale = load("book_pivot_tale.pkl") | |
| # Book selection | |
| select_book = st.selectbox("Select a book", options=books_name) | |
| # Recommendation function | |
| def recommended_book(book_name): | |
| book_id = np.where(book_pivot_tale.index == book_name)[0][0] | |
| distances, suggestions = model.kneighbors( | |
| book_pivot_tale.iloc[book_id, :].values.reshape(1, -1), n_neighbors=6 | |
| ) | |
| recommended_books = [] | |
| for i in suggestions[0][1:]: # Skipping the first as it's the input book itself | |
| recommended_books.append(book_pivot_tale.index[i]) | |
| return recommended_books | |
| # On button click | |
| if st.button("Recommend"): | |
| books_list = recommended_book(select_book) | |
| st.success("Recommended books are:") | |
| for i, book in enumerate(books_list): | |
| st.write(f"{i+1}. {book}") | |