Creating a Recommendation System for Songs with Turi Create

Arsh Anwar
4 min readOct 28, 2022

Everyone enjoys music. We can all agree that music is universal, regardless of which type we prefer. What is it about music that captivates us so much? Maybe it’s because music has the power to stir up strong emotional reactions in us. Consider this: We can all recall at least one song that evokes a certain emotion in us. A song that was performed at your high school graduation may be it. Or perhaps it’s a song that will always bring back a difficult breakup for you.

Listening to different types of music can have different types of effects on your brain. Therefore, finding the right music based on your likes and dislikes is a difficult task. Therefore apps like Spotify, Apple Music, Yt Music, etc. have something called a ‘Recommended for you’ section which consists of new songs based on your listening history.

This section relies on various types of Recommender System which analyzes your listening activity and give recommendations.

Image Source: Twitter

In Today’s blog, we are going to create a Recommendation System that gives recommendations based on the user’s listening history and based on the type of song.

After reading this blog:

  • You will understand the basics of Turi Create which is an ML Framework from Apple
  • You will know how to deal with SFrame Datasets
  • You will know how to use TrueFoundry for model Deployment

Let’s begin

Importing Libraries

import turicreate as tcimport os
os.environ["TFY_API_KEY"] = "<your-api-key>"
from servicefoundry import Build, PythonBuild, Service, Resources,DockerFileBuildimport mlfoundry
import servicefoundry.core as sfy

Loading Data

song_data = tc.SFrame('../input/songs-data/song_data.sframe')

Here, did you notice we used SFrames, not typical CSV or CSV?

SFrames are the primary data structure for extracting data from other sources for use in Turi Create. It is a scalable data format and is disk backed.

Exploratory Data Analysis

Creating Recommenders

  1. Popularity Based
train_data, test_data = song_data.random_split(.8, seed =0)popularity_model = tc.popularity_recommender.create(train_data, user_id='user_id',item_id='song')

Testing Popularity Recommender

Here, we recommended songs for user 1

2. Personalization Based

personalized_model = tc.item_similarity_recommender.create(
train_data,
user_id='user_id',
item_id='song'
)

Testing Recommender

Here, we are testing the Personalization based Recommender on User 1

We can even get similar songs using this type of recommender

personalized_model.get_similar_items(['Secrets - OneRepublic', 'Love Story - Taylor Swift','Marry Me - Train'])

Finding Best Out of 2

model_performance = tc.recommender.util.compare_models(test_data,[popularity_model,personalized_model],user_sample=0.05)

From this, we can conclude that our Population-Based Recommender and Personalized Recommender did well in recommending songs.
But, The table shows that the Personalized Recommender provides much better performance.

So the winner is the Personalized Recommender

Deployment

We are going to use TrueFoundry for our deployment

Logging into TrueFoundry and getting the API Key

Head to the Settings section and grab the API Keys.

1) Pre-Requisite

Before we Start deployment we will save our model and save it as an artifact in TrueFoundry

We aren’t going into detail about Experiment Tracking in this blog for more details refer to the Kaggle Nb.

personalized_model.save('my_model_file') #saving personalized model

2) Creating a Deployment file

3) Get Requirements

requirements = sfy.gather_requirements("deploy.py")
requirements['chardet'] = '3.0.4'
requirements['jinja2'] = '3.1.2'
requirements['turicreate'] = '6.4.1'
requirements['prettytable'] = '0.7.2'
requirements['servicefoundry'] = '0.2.15'
reqs = []
for i, j in enumerate(requirements):
reqs.append('{}=={}'.format(j, requirements[j]))
with open('requirements.txt', 'w') as f:
for line in reqs:
f.write(line)
f.write('\n')

4) Writing Dockerfile

%%writefile Dockerfile
FROM python:3.8
COPY ./requirements.txt /tmp/
RUN pip install -U pip && pip install -r /tmp/requirements.txt
COPY . ./app
WORKDIR app
ENTRYPOINT python deploy.py

5) Creating a Service

service = Service(
name="recommender-service-1",
image=Build(
build_spec=DockerFileBuild(),
),
ports=[{"port": 8080}],
resources=Resources(memory_limit=1500, memory_request=1000),
)
service.deploy(workspace_fqn='tfy-cluster-euwe1:demo-projects')

This will create a new deployment for you

6) Final Thoughts

After the deployment is done, you will be able to use the Gradio App.

The app is deployed here: https://recommender-service-1-demo-projects.tfy-ctl-euwe1-production.production.truefoundry.com/

Video

Code

The above code is also present in my Kaggle NB

References:

  1. TrueFoundry: https://truefoundry.com/
  2. TrueFoundry App: https://app.truefoundry.com/
  3. TrueFoundry Docs: http://docs.truefoundry.com/
  4. Code: https://www.kaggle.com/code/d4rklucif3r/songs-recommender-turi-create

--

--

Arsh Anwar

AI/ML expert. Built LuciferML (100k+ downloads). Co-founder @Revca, building smart solutions for a sustainable future.