Spaces:
Runtime error
Runtime error
File size: 4,422 Bytes
f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 11772dd f97b093 |
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import pickle
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
import streamlit as st
# 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"] = ratings["user_id"].astype(str)
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)
# 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
):
# Transform book author, title, and description into TF-IDF vectors
query_author_tfidf = tfidf_model_authors.transform([query])
query_title_tfidf = tfidf_model_titles.transform([query])
# Compute cosine similarity for authors and titles separately
similarity_scores_authors = cosine_similarity(
query_author_tfidf, tfidf_model_authors.transform(books["authors"])
)
similarity_scores_titles = cosine_similarity(
query_title_tfidf, tfidf_model_titles.transform(books["original_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):
# Get unrated books for the user
unrated_books = ratings[
~ratings["book_id"].isin(ratings[ratings["user_id"] == user_id]["book_id"])
]["book_id"].unique()
# 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
# Hybrid Recommendation
def hybrid_recommendation(
user_id,
query,
model_cf,
books,
ratings,
tfidf_model_authors,
tfidf_model_titles,
num_recommendations=10,
):
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
)
# Combine recommendations from different approaches
hybrid_rec = pd.concat([content_based_rec, collaborative_rec]).drop_duplicates(
subset="book_id", keep="first"
)
return hybrid_rec
# 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)
# Example user ID for collaborative recommendation
USER_ID = 0
st.write("Collaborative Recommendation:")
collaborative_rec = collaborative_recommendation(USER_ID, model_cf, ratings)
st.write(collaborative_rec)
st.write("Hybrid Recommendation:")
hybrid_rec = hybrid_recommendation(
USER_ID,
user_input,
model_cf,
books,
ratings,
tfidf_model_authors,
tfidf_model_titles,
)
st.write(hybrid_rec)
|