This code pattern is an iOS Application that helps you find new movies to watch! It's been built to show you how you can use Turi Create, Apple's new deep learning framework, to build amazing recommendation systems very quickly, locally, on your machine - in under 2 minutes training time.
When the reader has completed this Code Pattern, they will understand how to:
- Create "Item Similarity" recommender models in Turi Create
- Use Flask to expose a REST API that provides recommendations based off of users' past movie ratings
- Call that REST API from Swift to create a GUI around the recommender
- Download the MovieLens latest dataset (at the time of writing, there are 26,000,000 rows of data).
- Train an item similarity recommender with turi create on MovieLens.
- Expose that model through a REST API with Flask.
- Open a tunnel to the REST API with ngrok.
- User searches for and rates movies on the iOS app.
- iOS app sends the movie rating data to the REST API.
- The REST API will provide the iOS App with a list of movies and their scores.
- The iOS App will display those movies and scores.
- User will watch & rate those movies, and go back to step 6, getting better recommendations every time.
- Turi Create: is a task-focused deep learning library - allowing you to build intelligent apps by focusing on the machine learning use case, not the algorithm.
- Flask: is a micro web framework.
- ngrok: allows you to open an HTTPS tunnel into your REST API.
- Artificial Intelligence: Artificial intelligence can be applied to disparate solution spaces to deliver disruptive technologies.
- Python: Python is a programming language that lets you work more quickly and integrate your systems more effectively.
The following are prerequisites to start developing this application:
- Download and organize the latest MovieLens dataset
- Train the Turi Create Recommender model
- Run the backend
- Deploy the iOS app
In order to get the MovieLens dataset setup, run the following script:
$ sh setup.sh
It'll download the data and move the files to their relevant locations.
In order for the recommendations to work, you need a trained model. Turi Create is a Python package that will train this model for you.
Go ahead and start your Jupyter Notebooks. In the local_model_training
folder, open the TrainRecommendationModel.ipynb
notebook. Run the notebook, and once it's done executing, you're going to have a folder called movie_rec
, which contains the trained Turi Create model. Unlike other machine learning libraries, Turi Create does not use files for models; instead, it uses folders.
Turi Create supports exporting recommender models to CoreML, so they can run on-device, and there's no need for a backend. However, it only works with iOS 12 and above (which is currently in Beta), and there's an issue with linking the custom model framework. Therefore, this code pattern will use a backend that takes requests from the iOS Application, and runs the users' preferences through the Turi Create model, to return predictions as to what the user would like to watch next.
First, copy the movie_rec
folder (your trained model) from local_model_training
, and paste it into the serverside_prediction_api
folder. Once that's done, run the following command from a terminal window within that folder:
FLASK_APP=backend.py flask run
This will run the backend.py
file, which is the flask application.
Next, open up a tunnel to the backend with ngrok, so your iOS App can access it:
ngrok http 5000
(change 5000 to whichever port you chose with Flask)
Finally, you can run the front-end of the application.
Start by pointing your iOS app to the backend. Go to the iOS_Frontend/MovieRecommender/MovieRecommender/MovieHandler.swift
file, and change the backend
constant to the link of the ngrok tunnel.
Then, in the iOS_frontend/MovieRecommender
folder, and open the xcworkspace
file. Once you're there, run the application by hitting ⌘ + R or clicking the run button beside the window controls on the top left of Xcode.
This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.