|
import numpy as np |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
|
|
from model import build_model |
|
from utils import map_book_name_to_id |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
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. |
|
""" |
|
|
|
is_author = input_name.lower() in books["authors"].str.lower().values |
|
|
|
|
|
|
|
|
|
|
|
|
|
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.") |
|
|