FastAI Movie Recommender Step-by-Step

An app where you can type in your favorite movie and it will suggest 30 movies similar to i t using a collaborative filtering model trained on over 9million movie reviews.

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.

#!pip install -Uqq fastai

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 = Path('../models_and_dls')
learn = load_learner(path/'movie_predictor_large20mil_639.pkl')
titles = pd.read_csv(path/'movie_20mil_dls.csv')

Functions for Model Inference


source

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

source

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 our search_movies_n_recommend fuction as well as variables that dictate what will be shown once we publish it on gradio

intf = gr.Interface(fn=search_movies_n_recommend,
                    inputs=gr.Textbox(lines=1,placeholder="Put Your Favorite Movie Here To See Similar Movie Recommendations..."),
                    outputs=["text","text"],
                    examples=['Child\'s Play (1988)','Rushmore','brothers Bloom',
                              'call me by your name', 'Jumanji (1995)','The Three Amigos'],
                    title="Dave's Fast.AI Movie Recommender",
                    article='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!'                    )

Actually Try the Model HERE

Now we launch it so it can be used literally here in the jupyter notebook

intf.launch(inline=True, #when True publishes the page in notebook
    share=False) #when True automatically publishes the app to the gradio website for 72 hours.
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.

favorite_movie = 'Beasts of the Southern Wild (2012)'

get_movie_recs(full_title=favorite_movie, learn=learn, titles=titles)
['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)']