book-recommender / inference.py
nirajandhakal's picture
initial commit
350eabd verified
raw
history blame
1.54 kB
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from model import build_model
from utils import map_book_name_to_id
# Load the saved model for inference
loaded_model = build_model(
num_users=len(ratings["user_id"].unique()),
num_books=len(ratings["book_id"].unique()),
)
loaded_model.load_weights("recommendation_model.h5")
# Function to recommend books for a user based on input book name or author name
def recommend_books_for_user(input_name, model, num_recommendations=10):
"""
Recommend books for a user based on input book name or author name.
Args:
input_name (str): The input book name or author name.
model: The trained recommendation model.
num_recommendations (int): The number of books to recommend.
Returns:
tuple: A tuple containing the recommended book names and their similarity scores.
"""
# Check if input_name is a book name or author name
is_author = input_name.lower() in books["authors"].str.lower().values
# Rest of the code...
# Recommend books for a user based on input name along with similarity score.
input_name = "Harry Potter and the Sorcerer's Stone"
recommended_books, similarity_scores = recommend_books_for_user(
input_name, loaded_model
)
if recommended_books is not None:
print("Recommended Books:")
print("------------------")
for book, score in zip(recommended_books, similarity_scores):
print(f"{book:<60} {score:.4f}")
else:
print("No recommendations found.")