Spaces:
Build error
Build error
| import gradio as gr | |
| from fastai.learner import load_learner | |
| import pandas as pd | |
| import numpy as np | |
| learn = load_learner('movie_recommendation.pkl') | |
| ratings = pd.read_csv('ratings_test.csv') | |
| ids = ratings['userId'].unique() | |
| ids_list = list(map(str, ids.tolist())) | |
| def top_predictions(userId, recommendations = 5): | |
| userId = int(userId) | |
| items = pd.Series(learn.dls.classes['title']).unique() | |
| clas_items = ratings.loc[(ratings['userId'] == userId) & (ratings['rating'] > 0), 'title'] | |
| no_clas_items = np.setdiff1d(items, clas_items) | |
| df = pd.DataFrame({'userId': [userId]*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(recommendations, 'prediction') | |
| return '\n'.join(top_5['title'].tolist()) | |
| iface = gr.Interface( | |
| description="This model is a film recommender based on our user's ratings of other films .", | |
| fn=top_predictions, | |
| inputs=gr.Dropdown(choices=ids_list), | |
| outputs="text" | |
| ) | |
| iface.launch(share=True) |