MorettiGS's picture
fix format
3e8662d
raw
history blame
1.07 kB
import gradio as gr
from fastai.learner import load_learner
import pandas as pd
import numpy as np
learn = load_learner('model.pkl')
dados = pd.read_csv('valid.csv')
ids = dados['user'].unique()
ids_list = list(map(str, ids.tolist()))
ratings = pd.read_csv('ratings.csv')
def top5(user):
user = int(user)
items = pd.Series(learn.dls.classes['title']).unique()
clas_items = ratings.loc[(ratings['user'] == user) & (ratings['rating'] > 0), 'title']
no_clas_items = np.setdiff1d(items, clas_items)
df = pd.DataFrame({'user': [user]*len(no_clas_items), 'title': no_clas_items})
preds,_ = learn.get_preds(dl=learn.dls.test_dl(df))
df['prediction'] = preds.numpy()
top_5 = df.nlargest(5, 'prediction')
return '\n'.join(top_5['title'].tolist())
iface = gr.Interface(
fn=top5,
inputs=gr.Dropdown(choices=id_list),
outputs="text",
title="Books Recommendation",
description="This model is responsible for a recommendation system involving books and their ratings.",
)
iface.launch(share=True)