Spaces:
Runtime error
Runtime error
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, 3] | |
intf = gr.Interface(fn=gl, inputs=text, outputs=label, examples=example) | |
intf.launch(inline=False) |