#!pip install -Uqq fastai
FastAI Movie Recommender Step-by-Step
Makes sure all your libraries and packages are installed and loaded.
Fastai is the library we use to train, load and run our model. NB: we already trained our model so we’ll only be loading it here.
Gradio allows you to easily publish your model onto the web for peope to use.
# !pip install gradio
nbdev is what we’re using to make this documentation easier and more presentable.
# !pip install nbdev
These are the libraries we need
# For modeling
from fastai.tabular.all import *
from fastai.collab import *
#for publishing the model
import gradio as gr
Load model and data loaders or movie title csv
The dataloaders we used to train the model from the 9 million reviews were 200MB so we use a csv instead. To convert from data loaders to pandas DataFrame use code hashtagged out just below
# titles = pd.DataFrame(dls.classes['title']).reset_index() #create df from data loaders(dls)
# titles = titles.rename(columns={'index':'midx',0:'title'}) # rename columns
#load the model and movie titles with indexes (from the previous data loaders)
= Path('../models_and_dls')
path = load_learner(path/'movie_predictor_large20mil_639.pkl')
learn = pd.read_csv(path/'movie_20mil_dls.csv') titles
Functions for Model Inference
get_movie_recs
get_movie_recs (full_title:str, learn, titles)
This function will use the nn.CosineSimilarity
on the 50 latent factors for each movie to find the 30 movies most similar to your favorite movie.
Type | Details | |
---|---|---|
full_title | str | String saved to favorite_movie variable |
learn | Trained Model | |
titles | DataFrame with all movie titles from dls |
search_movies_n_recommend
search_movies_n_recommend (favorite_movie:str, learn, titles)
This function returns 30 recommendations using get_movie_recs
after searching through all movie titles to find any titles that contain the words in the favorite_movie
variable.
Type | Details | |
---|---|---|
favorite_movie | str | The movie title typed into the gr.Textbox() that the user will see on the gradio app |
learn | Trained model | |
titles | Movie titles df |
Gradio For Publishing the Model
Now we use
gr.Interface()
to create a block that contains oursearch_movies_n_recommend
fuction as well as variables that dictate what will be shown once we publish it ongradio
= gr.Interface(fn=search_movies_n_recommend,
intf =gr.Textbox(lines=1,placeholder="Put Your Favorite Movie Here To See Similar Movie Recommendations..."),
inputs=["text","text"],
outputs=['Child\'s Play (1988)','Rushmore','brothers Bloom',
examples'call me by your name', 'Jumanji (1995)','The Three Amigos'],
="Dave's Fast.AI Movie Recommender",
title='This recommender uses a collaborative filtering model fashioned from the [Fast.AI](https://github.com/fastai/fastbook/blob/master/08_collab.ipynb) library and trained on the 9 million movie reviews from the [Movie Lense Dataset](https://grouplens.org/datasets/movielens/25m/). It trains Latent Factors to give movies and users descriptive weights that help predict thier reivews based on these factors. The model was trained completely WITHOUT titles, descriptions, or categories and amazingly these things can be added to imporove this model. Enjoy!' ) article
Actually Try the Model HERE
Now we launch it so it can be used literally here in the
jupyter notebook
=True, #when True publishes the page in notebook
intf.launch(inline=False) #when True automatically publishes the app to the gradio website for 72 hours. share
Running on local URL: http://127.0.0.1:7861
To create a public link, set `share=True` in `launch()`.
(<gradio.routes.App>, 'http://127.0.0.1:7861/', None)
Example Without Gradio:
Here is an example of the model being used straight from the
get_movie_recs
function, recommending movies similar to one of my favorites.
= 'Beasts of the Southern Wild (2012)'
favorite_movie
=favorite_movie, learn=learn, titles=titles) get_movie_recs(full_title
['Beasts of the Southern Wild (2012)',
'Mina Tannenbaum (1994)',
'Triplets of Belleville, The (Les triplettes de Belleville) (2003)',
'Chasing Mavericks (2012)',
'Bye Bye Brazil (Bye Bye Brasil) (1980)',
"Tale of Winter, A (a.k.a. A Winter's Tale) (Conte d'hiver) (1992)",
'Coonskin (1975)',
'Nanny, The (1965)',
'Trials of Henry Kissinger, The (2002)',
'Rain Fall (2009)',
'Hail, Caesar! (2016)',
'Freeway (1996)',
'Phantom Boy (2015)',
'The Attic (2008)',
'James and the Giant Peach (1996)',
'Song of the Sea (2014)',
'Bitter Tears of Petra von Kant, The (bitteren Tränen der Petra von Kant, Die) (1972)',
'The Ballad of Buster Scruggs (2018)',
'Bank Dick, The (1940)',
'Salvador (1986)',
'Go-Getter, The (2007)',
'Raining Stones (1993)',
'Easy Street (1917)',
'Scarlet Diva (2000)',
'Kings of Summer, The (2013)',
'My Side of the Mountain (1969)',
'Nashville (1975)',
'Fantastic Mr. Fox (2009)',
'After the Rehearsal (Efter repetitionen) (1984)',
'Touchy Feely (2013)']