Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from utils import * | |
# Assuming data is loaded and matrices are prepared as discussed | |
def load_data(): | |
ratings = pd.read_csv('./collaborative_books_df.csv', index_col=0) | |
books = pd.read_csv('./collaborative_book_metadata.csv', index_col=0) | |
# book_titles=pd.read_csv('./data/book_titles.csv', index_col=0) | |
# book_titles = book_titles.reset_index() | |
# Merge data | |
ratings = ratings.merge(books, on='book_id') | |
book_titles = dict(zip(ratings['book_id'], ratings['title_x'])) | |
return ratings, books,book_titles | |
def initialize_session_state(): | |
if "ratings" not in st.session_state: | |
st.session_state.ratings, st.session_state.books, st.session_state.book_titles = load_data() | |
st.session_state.X, st.session_state.user_mapper, st.session_state.book_mapper, st.session_state.user_inv_mapper, st.session_state.book_inv_mapper = create_matrix(st.session_state.ratings) | |
st.session_state.book_id_mapping = pd.Series( st.session_state.books.book_id.values, index= st.session_state.books.title).to_dict() | |
initialize_session_state() | |
# Streamlit interface for book recommendation | |
st.title('Book Recommender System') | |
# User inputs | |
title_input = st.selectbox('Select or type a book title', st.session_state.books['title'].unique()) | |
k_input = st.number_input('How many recommendations do you want?', min_value=1, max_value=20, value=5) | |
if st.button('Find Similar Books'): | |
if title_input in st.session_state.book_id_mapping: | |
book_id = st.session_state.book_id_mapping[title_input] | |
distances, similar_ids = find_similar_books(book_id, st.session_state.X, k=k_input,book_mapper= st.session_state.book_mapper,book_inv_mapper= st.session_state.book_inv_mapper) | |
similar_books = pd.DataFrame({ | |
'Book Title': [ st.session_state.book_titles[ids] for ids in similar_ids], | |
'Distance': distances[0][1:] | |
}) | |
st.write(f"Books similar to {title_input}:") | |
st.dataframe(similar_books.sort_values(by='Distance', ascending=True)) | |
else: | |
st.error("Book title not found. Please check the spelling or try another title.") | |