import gradio as gr import random import pandas as pd import operator from surprise import Dataset, Reader from surprise import KNNBasic def opendata(a, nrows): """ opens the data and returns a dataframe """ df = pd.read_csv(a, nrows=nrows, sep=';', encoding='ISO-8859-1') return df def red(df): """ Builds the trainset and the anti testset, Returns the trainset and the anti testset as items """ reader = Reader(rating_scale=(1,10)) # rating scale range trainset = Dataset.load_from_df(df[['User-ID','ISBN','Book-Rating']],reader).build_full_trainset() items = trainset.build_anti_testset() return trainset, items def mod(df, user, items): """ Builds the model using KNNBasic, Fits the model to the trainset, Takes the given user and creates a recommendation for them based on the model returns the recommendations """ algo = KNNBasic() algo.fit(df) user_items = list(filter(lambda x: x[0] == user, items)) recommendations = algo.test(user_items) recommendations.sort(key=operator.itemgetter(3), reverse=True) return recommendations def gl(num): """ Opens the 2 datasets Creates a mapping dictionary for the ISBN and Book-Title, Creates a list of users and accepts a user input, Builds the model and returns the top 5 recommendations for the user """ data = opendata('BX-Book-Ratings.csv', nrows=20_000) books = opendata('BX_Books.csv', nrows=None) mapping_dict = books.set_index("ISBN")["Book-Title"].to_dict() users=data['User-ID'].tolist() trainset, items = red(data) user = users[int(num)] recommendations = mod(trainset, user, items) op = [] for r in recommendations[0:5]: try: op.append(f"{mapping_dict[r[1]]} with Estimated Rating {round(r[3],3)}") except: continue return ('\n\n'.join(map(str, op))) text = gr.components.Number(label="pick a number between 1 and 1000") label = gr.components.Text(label="Picked User Top 5 Recommendations:") example = [2, 20, 200, 1000] des = """ This model is meant to build a recommendation system for a user who's already made some ratings for some of the books in the Library. This model then uses those ratings with the other user ratings are then used to make a prediction on what other kinds of books the user might like. This dataset comes from a the kaggle website https://www.kaggle.com/datasets/ruchi798/bookcrossing-dataset The model is built with an inspiration from this notebook https://www.kaggle.com/code/stpeteishii/surprise-recommend-books-for-users/notebook """ Title= "Book Recommedation System With Surprise Library" intf = gr.Interface(fn=gl, inputs=text, outputs=label, examples=example, description=des, title=Title) intf.launch(inline=False)