Built a Spotify Playlist Organizer Using OpenAI and Python

It’s been such a long while! I recently was talking with my friend about Spotify playlist sharing and wondered if I could leverage OpenAI to analyze and organize my playlists based on language, vocal type, or even genre?

Since I’m not that busy before the holiday, I took on a fun little project to do just that — wrote some scripts that can analyze the songs in the playlist and sort them into new playlists based on their language, vocal type, and genre.

Let’s dive into how it works.

Overview

  • Take a playlist from my Spotify account.
  • Send each song’s details (name, artist) to OpenAI’s API.
  • Have OpenAI analyze the song and determine its language, genre, and vocal type (male, female, or mixed).
  • Automatically create new playlists for each unique category (like language-japanese, vocal-male, genre-pop), and then add the songs to the right playlist.

Setting Up

Two things:

  • OpenAI for analyzing the songs and figuring out their language, vocal type, and genre. Get more information here: https://openai.com/api/pricing
  • Spotify for Developers: Set up the app; Get your Client ID, Client Secret and Redirect URIs
  • Spotipy (a Python library for Spotify) to interact with my Spotify account and get playlist information.

The Process

  1. Authenticate with Spotify
    I used the spotipy library to authenticate with Spotify, using OAuth. This allows me to access playlists and create new ones without needing to input credentials manually every time. (Just make sure to set up your Spotify developer account and have client_id and client_secret ready.)
  2. Fetch Playlist Tracks
    Once authenticated, it grabs all the songs in the playlist you provide. It fetches the name and artist of each track.
  3. Analyze Tracks with OpenAI
    For each track, the app sends the track name and artist name to OpenAI. I crafted a prompt like:
    "Analyze the following song and provide details on language, genre, and vocal type (male, female, or mixed):\n\nSong: {track_name}\nArtist: {artist_name}"
    (you can always replace the prompt)
    OpenAI then sends back a response, and I extract details like:
    • Language: Is it in Japanese, English, Korean, or something else?
    • Vocal Type: Is it sung by a male or female voice? Or is it a mixed vocal?
    • Genre: What genre does it belong to (pop, rock, etc.)?
  4. Dynamic Playlist Creation
    Based on the analysis, I dynamically create playlists like language-japanese, vocal-male, genre-pop, etc. Each playlist is automatically populated with the tracks that fit those categories. For example:
    • All Japanese songs go into the language-japanese playlist.
    • All male-vocal tracks go into the vocal-male playlist.
    • All rock songs go into the genre-rock playlist.
  5. Run
    The app runs on a simple command-line interface.
    python sort_playlist.py YOUR_API_KEY PLAYLIST_ID
    Where:
    YOUR_API_KEY is your OpenAI API key.
    PLAYLIST_ID is the ID of the playlist you want to sort.

The OpenAI response Error

When I initially tried to call OpenAI’s create method for chat completions, I was getting an error that looked something like this:

'ChatCompletion' object is not subscriptable

Many thanks to this post. It turns out that the latest version of the OpenAI Python library made a change in how the response is structured. The .choices object, which used to be a list, is now wrapped in a more complex object, and you can’t access it directly with indexing like I tried. To access the message content, I needed to use .message.content instead of trying to index directly into the response object. So the fixed code looks like this

response_message = response.choices[0].message.content

Result

I used my release radar playlist to test. There are 30 songs in the playlist with a variety of languages, generally indie/ rock and I think all vocals are male. Not so suitable for a test but I’ll go with it.

The English playlist is not quite as expected:

But seems that the Japanese playlist is working well.

It definitely needs more fine-tuning and work on the OpenAI part for the analysis. Other things that need improvement:

  • Genre and language Detection: Make the genre and language detection more accurate by expanding the list of keywords checked for in OpenAI’s response.
  • Automate Playlist Sorting for Multiple Playlists: Run the script on multiple playlists and have it automatically categorize everything.
  • Improve the prompt: Make the process faster in case of long playlists.

But anyway it’s great fun to explore 🙂 See ya next time!

Leave a Reply

Your email address will not be published. Required fields are marked *