nirajandhakal's picture
initial commit
f97b093 verified
raw
history blame
9.06 kB
"""
This is a book recommendation system.
"""
import pickle
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from tensorflow.keras.models import load_model
# Load datasets
books = pd.read_csv("./dataset/books.csv")
ratings = pd.read_csv("./dataset/ratings.csv")
# Preprocess data
user_encoder = LabelEncoder()
book_encoder = LabelEncoder()
ratings["user_id"] = user_encoder.fit_transform(ratings["user_id"])
ratings["book_id"] = book_encoder.fit_transform(ratings["book_id"])
# Load TF-IDF models
with open("tfidf_model_authors.pkl", "rb") as f:
tfidf_model_authors = pickle.load(f)
with open("tfidf_model_titles.pkl", "rb") as f:
tfidf_model_titles = pickle.load(f)
# Define TF-IDF vectorizer
tfidf_vectorizer = TfidfVectorizer(stop_words="english")
# Fit and transform the book descriptions
tfidf_matrix = tfidf_vectorizer.fit_transform(books["original_title"].fillna(""))
# Load collaborative filtering model
model_cf = load_model("recommendation_model.keras")
# Content-Based Recommendation
def content_based_recommendation(
query, books, tfidf_model_authors, tfidf_model_titles, num_recommendations=10
):
"""
Recommend books based on content similarity.
Args:
query (str): The name of the book or author.
books (DataFrame): DataFrame containing book information.
tfidf_model_authors: Pre-trained TF-IDF model for authors.
tfidf_model_titles: Pre-trained TF-IDF model for titles.
num_recommendations (int): The number of books to recommend.
Returns:
DataFrame: A DataFrame containing recommended books with details.
"""
# Check if the query corresponds to an author or a book
if query in books["authors"].values:
book_name = books.loc[books["authors"] == query, "original_title"].values[0]
elif query in books["original_title"].values:
book_name = query
else:
print("Query not found in authors or titles.")
return None
book_author = books.loc[books["original_title"] == book_name, "authors"].values[0]
book_title = books.loc[books["title"] == book_name, "title"].values[0]
# Transform book author, title, and description into TF-IDF vectors
book_author_tfidf = tfidf_model_authors.transform([book_author])
book_title_tfidf = tfidf_model_titles.transform([book_title])
# Compute cosine similarity for authors and titles separately
similarity_scores_authors = cosine_similarity(
book_author_tfidf, tfidf_model_authors.transform(books["authors"])
)
similarity_scores_titles = cosine_similarity(
book_title_tfidf, tfidf_model_titles.transform(books["title"])
)
# Combine similarity scores for authors and titles
similarity_scores_combined = (
similarity_scores_authors + similarity_scores_titles
) / 2
# Get indices of recommended books
recommended_indices = np.argsort(similarity_scores_combined.flatten())[
-num_recommendations:
][::-1]
# Get recommended books
recommended_books = books.iloc[recommended_indices]
return recommended_books
# Collaborative Recommendation
def collaborative_recommendation(user_id, model_cf, ratings, num_recommendations=10):
"""
Recommend books based on collaborative filtering.
Args:
user_id (int): The user ID.
model_cf: The trained collaborative filtering model.
ratings (DataFrame): DataFrame containing user ratings.
num_recommendations (int): The number of books to recommend.
Returns:
DataFrame: A DataFrame containing recommended books with details.
"""
# Check if the user ID exists in the ratings dataset
if user_id not in ratings["user_id"].unique():
print("User ID not found in ratings dataset.")
return None
# Get unrated books for the user
unrated_books = ratings[
~ratings["book_id"].isin(ratings[ratings["user_id"] == user_id]["book_id"])
]["book_id"].unique()
# Check if there are unrated books
if len(unrated_books) == 0:
print("No unrated books found for the user.")
return None
# Predict ratings for unrated books
predictions = model_cf.predict(
[np.full_like(unrated_books, user_id), unrated_books]
).flatten()
# Get top indices based on predictions
top_indices = np.argsort(predictions)[-num_recommendations:][::-1]
# Get recommended books
recommended_books = books.iloc[top_indices][["original_title", "authors"]]
return recommended_books
# History-Based Recommendation
def history_based_recommendation(user_id, ratings, num_recommendations=10):
"""
Recommend books based on user's historical ratings.
Args:
user_id (int): The user ID.
ratings (DataFrame): DataFrame containing user ratings.
num_recommendations (int): The number of books to recommend.
Returns:
DataFrame: A DataFrame containing recommended books with details.
"""
user_ratings = ratings[ratings["user_id"] == user_id]
top_books = user_ratings.sort_values(by="rating", ascending=False).head(
num_recommendations
)["book_id"]
recommended_books = books[books["book_id"].isin(top_books)]
return recommended_books
# Hybrid Recommendation
def hybrid_recommendation(
user_id,
query,
model_cf,
books,
ratings,
tfidf_model_authors,
tfidf_model_titles,
num_recommendations=10,
):
"""
Recommend books using hybrid recommendation approach.
Args:
user_id (int): The user ID.
query (str): The name of the book or author.
model_cf: The collaborative filtering model.
books (DataFrame): DataFrame containing book information.
ratings (DataFrame): DataFrame containing user ratings.
tfidf_model_authors: Pre-trained TF-IDF model for authors.
tfidf_model_titles: Pre-trained TF-IDF model for titles.
num_recommendations (int): The number of books to recommend.
Returns:
DataFrame: A DataFrame containing recommended books with details.
"""
content_based_rec = content_based_recommendation(
query,
books,
tfidf_model_authors,
tfidf_model_titles,
num_recommendations=num_recommendations,
)
collaborative_rec = collaborative_recommendation(
user_id, model_cf, ratings, num_recommendations=num_recommendations
)
history_based_rec = history_based_recommendation(
user_id, ratings, num_recommendations=num_recommendations
)
# Combine recommendations from different approaches
hybrid_rec = pd.concat(
[content_based_rec, collaborative_rec, history_based_rec]
).drop_duplicates(subset="book_id", keep="first")
return hybrid_rec
# Top Recommendations (most popular books)
def top_recommendations(books, num_recommendations=10):
"""
Recommend top books based on popularity (highest ratings count).
Args:
books (DataFrame): DataFrame containing book information.
num_recommendations (int): The number of books to recommend.
Returns:
DataFrame: A DataFrame containing recommended books with details.
"""
top_books = books.sort_values(by="ratings_count", ascending=False).head(
num_recommendations
)
return top_books
# Test the recommendation functions
query = input("Enter book name or author: ")
USER_ID = 0 # Example user ID for collaborative and history-based recommendations
print("Content-Based Recommendation:")
print(
content_based_recommendation(query, books, tfidf_model_authors, tfidf_model_titles)
)
print("\nCollaborative Recommendation:")
print(collaborative_recommendation(USER_ID, model_cf, ratings))
print("\nHistory-Based Recommendation:")
print(history_based_recommendation(USER_ID, ratings))
print("\nHybrid Recommendation:")
print(
hybrid_recommendation(
user_id,
query,
model_cf,
books,
ratings,
tfidf_model_authors,
tfidf_model_titles,
)
)
print("\nTop Recommendations:")
print(top_recommendations(books))
# Streamlit App
st.title("Book Recommendation System")
# Sidebar for user input
user_input = st.text_input("Enter book name or author:", "")
# Get recommendations on button click
if st.button("Get Recommendations"):
st.write("Content-Based Recommendation:")
content_based_rec = content_based_recommendation(
user_input, books, tfidf_model_authors, tfidf_model_titles
)
st.write(content_based_rec)
st.write("Collaborative Recommendation:")
collaborative_rec = collaborative_recommendation(0, model_cf, ratings)
st.write(collaborative_rec)
st.write("Hybrid Recommendation:")
hybrid_rec = hybrid_recommendation(
0, user_input, model_cf, books, ratings, tfidf_model_authors, tfidf_model_titles
)
st.write(hybrid_rec)