File size: 1,537 Bytes
350eabd |
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 |
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.")
|