File size: 898 Bytes
766b779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import pandas as pd
from fastai.learner import load_learner

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):
    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=ids_list), outputs="text")

iface.launch(share=True)