File size: 2,850 Bytes
6a8f68c
 
 
4b0ef19
6a8f68c
 
 
 
eb88a98
 
 
6a8f68c
 
 
 
eb88a98
 
 
 
6a8f68c
 
 
 
 
 
 
eb88a98
 
 
 
 
 
6a8f68c
 
 
 
 
 
 
 
eb88a98
 
 
 
 
 
0d26411
 
6a8f68c
eb88a98
 
 
6a8f68c
 
 
 
 
 
 
 
 
 
 
 
172834c
 
 
 
 
 
 
 
 
 
 
6a8f68c
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
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)