Make Machine Learning Powered Anime Recommender iOS App in 3 Steps
Who else is excited for the next episode of Attack on Titan or One Piece? 🙋♂️ And who else is also excited for the next season of My Hero Academia? 🙋♂️ While waiting for these awesome episodes, I’m thinking of watching an anime that I haven’t watched. But what should I watch?
Since Machine Learning (ML) and Artificial Intelligence (AI) have picked up a lot of buzz in the market these days, I figured it’d be fun to make an anime recommender app to tell me what anime to watch based on what others have also watched. This is called a collaborative filtering technique.
Apple has made it relatively easy to create a machine learning powered iOS and macOS appa with Core ML. So in this article I will share how to create an anime recommender app in three steps—Download, Train, and Use.
#1 Download the anime dataset
First, we need a dataset. After searching in the internet for the anime dataset to use, I ended up with the MyAnimeList dataset in Kaggle. It contains hundreds of thousands users and millions of ratings. I used the animelists_cleaned.csv
because it contains the ratings given by users. I’ve got to warn you, it’s 2GB with more than 30 million rows.
#2 Create and Train ML Model using Create ML.
Next let’s create the recommender model. Here’s the cool thing about what Apple has done with machine learning development in macOS. You don’t need to write any script or code to train an ML model. You can create and train an ML model using the Create ML app. You can find it by opening Xcode, click on Xcode menu, hover on “Open Developer Tool” menu, then click on Create ML.
In the New Project window, you can choose a template for the ML model you want to create. For the anime recommender app, I chose the Recommendation template.
Load the animelists_cleaned.csv
to the training data, then select username
column in Users, anime_id
column in Items, and my_score
column in Ratings.
Finally, start the training by clicking the Train button. Using the data from the three columns (username
, anime_id
, and my_score
), Create ML will then create a recommendation model by learning from millions of ratings in the dataset. The training only took around a minute, but it will depend on the machine you use.
Once the training completes, you can test it out in the Preview tab by giving it a User, and list of items which are the ids of anime and the rating. For example, since I like Attack on Titan, I entered 25777 as the id and 10 as the rating. You can find the id and the title of an anime in anime_cleaned.csv
file.
Last thing to do is to export the model by clicking the Get button in the Output tab.
#3 Use the Model in iOS App
The last step is to use the model in the app. Start by dragging the model into your Xcode project. When Xcode imports the model to a project, it automatically generates all the Swift classes so you can use it in the app. The name of the class will follow the name of the model. In my case, the class is called AnimeUserRating
.
To get the recommendations, first create an instance of the model (1), then create an input for the model (2), and finally get the predictions from the model (3). See the snippet below.
Using SwiftUI, I made a simple single view app that shows the popular anime and a search text field so user can search for the anime they like. When an anime in the popular list or in the search result is tapped, the app will instantly show a list of anime recommendation.
You can find the sample app in my Github repository. In the sample app, I simply set value “10” as the rating for the anime which is selected by the user. It is possible however to get better recommendation if we can let user to dislike an anime by setting lower rating in the anime that user dislikes, say 1. I’ll leave that as an exercise to the readers.
Hyperjump is an open-source-first company providing engineering excellence service. We aim to build and commercialize open-source tools to help companies streamline, simplify, and secure the most important aspects of its modern DevOps practices.