Skip to content

2. Back End

widepend edited this page Apr 22, 2021 · 29 revisions

Back-end Block Diagram

The diagram above shows the flow of information from the user interface to the back end operations. In order to avoid having to repeatedly send and parse multiple JSON objects between the front and back end, all of the recommendation computation happens in the back end. The main driving force is the recommendation engine which uses a k-nearest neighbor approach.

  1. Front End Displays the interface where the user can choose the song or playlist that will become the seed for the recommendation engine. The app does not require any login, and the user simply enters the URL to the song or playlist in question. The user also has the choice of using some pre-built genre playlists to get started with the app.

  2. Spotify Handler First checks that the API key authorization is valid, thereby allowing the user to make Spotify API calls even if they don't have an account. This module takes the URL provided in the front end, and then sends a get request also receives the song metadata from the Spotify API to be used in calculating scores for recommendations.

  3. Main Serves as a bridge between the Spotify Handler and the Recommendation Engine to allow the two parts to be tested separately and provide greater organization. Since the API handles tracks and playlists differently, there are two separate control flows.

  • Flow 1: If the user enters a single track, the getsong endpoint is called, which can be considered our 'main' for this case. All subsequent function calls return back to getsong, which ultimately returns the final recommendations back to the user. The getsong endpoint first calls the Spotify API, and the track metadata is returned back to 'getsong' in a dictionary called track_info. This data is then passed to the track_rec_helper function to get some baseline recommendation information for our nearest neighbors algorithm. The data from track_rec_helper is returned back to getsong in a dictionary called all_recs. Lastly, we pass both track_info and all_recs to our nearest_neighbor function, which returns the final recommendations back to getsong, which then returns it to the user.

  • Flow 2: If the user enters a playlist, the read_playlist endpoint is called, which will once again be considered our 'main' for this case. All subsequent function calls return back to read_playlist, which ultimately returns the final recommendations back to the user. The read_playlist endpoint first calls the Spotify API, and the track metadata (for each song in the playlist) is returned back to 'read_playlist' in a dictionary called playlist_info. This data is then passed to the playlist_rec_helper function to get some baseline recommendation information again. The data from playlist_rec_helper is returned back to 'read_playlist' in a dictionary called all_recs. Lastly, we pass both playlist_info and all_recs to our nearest_neighbor function, which returns the final recommendations back to read_playlist, which then returns it to the user.

Note: The Spotify API returns much more information than we need, so we have to clean up the data between every API call to keep it consistent. This is why a single track is handled differently than an entire playlist. However, the nearest neighbors function is the same for both cases as a result of our consistently formatted data.

  1. Spotify API The Spotify API allows for the retrieval of song metadata as well as the authentication of the API key. The API can return metadata about a single song or an entire playlist. We then do some data wrangling in the back-end to remove unnecessary information and make the API data easier to work with.

  2. Recommendation Engine Compares the key, BPM, and danceability of the seed with songs on Spotify and begins recommending like songs using k-nearest neighbor calculations. As the user approves recommendations, those songs also weigh into future recommendations.

KNN Recommender function takes in either a single song or a playlist of songs that was used to seed. It also takes in a list of parameters that we want to use to filter by. It computes the average BPM, energy, key, dance and valence of the input playlist/song and vectorizes that information. We then repeat that same process for each song in our recommendation list. In addition to those 5 factors we keep track of other meta data for the songs in our recommendation list. Then for all of our recommendations, we go through the vectors we made and take the cosine similarity between our baseline vector from our input song/playlist and all of our recommendations. From there we take a fraction of the original songs that were the most similar and group them together and in turn recommend them to the user in order from best to worst.

  1. Playlist Recommendations that are approved by the user are added to a list which is shown to the user at the end of their recommendation session. They can then select songs in the playlist to decide which to add to their Spotify library.

  2. Searching Spotify Uses Spotify API to search for a song or playlist passed in by the user. Returns up to 50 results that have a similar name to the search query passed in by the user.

Clone this wiki locally