I asked ChatGPT to generate my Spotify playlists

I asked ChatGPT to generate my Spotify playlists

A command-line tool to generate Spotify playlists based on ChatGPT prompts.

Play this article

Github Project

Project description

The goal of this simple project was to learn how to interact with the OpenAI API and get useful information from ChatGPT in a format I can easily manipulate (JSON dictionaries of artists and songs). On the other side, it uses the Spotipy module to interact with Spotify and create playlists. Linked together, these two modules can help you create creative playlists from songs suggested by ChatGPT according to a given prompt (Example: "Peaceful songs to listen to when it's raining"). It's a great way to discover new tunes!

This project is built upon a tutorial by Colt Steele: Mastering OpenAI Python APIs that I highly recommend, as it covers the basics of how ChatGPT works and how to interact with it. On top of the automatic playlist creation mode covered by the tutorial, I created an interactive mode, which lets you decide which songs should be added and gives you the possibility to blacklist artists and songs. I completed the project with additional methods to play and print playlists.

Prerequisites

To use this tool, you need both an OpenAI and a Spotify Developer account.

OpenAI

It's the company that created and offers access to different ChatGPT models. Go to the OpenAI website and create an account. It comes with some free credit to start and test the API. See documentation to generate your own API-Key.

Spotify

Create a Spotify account if you don't already have one, it's free. After that, log in to Spotify for Developers and create a project. In the project settings, you can generate the client-ID and -Secret needed to connect and send requests to Spotify. See the documentation for more info.

Once you have your credentials to access both APIs, you can add them to playlist_generator.py. I am using the keyring module to store and retrieve mine, but you can use an .env file, store them in environment variables or just use them directly in the code. In this last case, be careful not to share your code with anyone!

# Set openai key
openai.api_key = keyring.get_password('openai_key', 'main_key')
# Set spotify client id and secret
client_id=keyring.get_password('spotify', 'client_ID')
client_secret=keyring.get_password('spotify', 'client_secret')

Spotify Desktop

If you want to be able to play songs during the playlist creation (in the interactive mode for instance), you will need Spotify Desktop installed on your machine. If you want to skip this part, comment all calls to the method play_song_in_spotify in playlist_generator.py, as it will not be able to ask your Spotify player to play the given songs.

Installation

Clone project and install required libraries (in a virtual environment for instance)

git clone https://github.com/alexdjulin/spotify-playlist-generator.git
python -m venv venv
venv\Scripts\activate # on windows
source venv/bin/activate # on macOS/linux
pip install -r requirements.txt

How to use it

The most convenient way to start the playlist generator is to call main.py.

python main.py

It will ask for the 4 required input variables:

  • Playlist Prompt (-p):
    A short description sent to ChatGPT to generate the playlist. Example: Best songs for a road trip with your best friend.

  • Playlist Length (-l):
    How many songs should be in the playlist. Default value is 10.

  • Playlist Name (-n):
    The playlist name on Spotify. Optional, leave blank to use the prompt as a name. Example: Road Trip Songs.

  • Interactive Mode (-i):
    Y to activate or N to deactivate the interactive mode. When active, each song will be played in Spotify and the user can choose to Add the song, blacklist it or blacklist the artist.

Playlist Prompt: Best songs for a road trip with your best friend
Playlist/Batch Length: 10
Playlist Name (leave blank to use prompt): Road Trip Songs
Interactive Mode? [y]es or [n]o: n
Creating playlist. Please wait...
>> end of playlist creation

Additionally, you can call playlist_generator.py and pass these arguments in the command line directly. Don't forget the quotes around the strings. The interactive mode flag is TRUE if you pass -i, FALSE if you don't pass it:

# interactive flag off
python playlist_generator.py -p "Best songs for a road trip with your best friend" -l 10 -n "Road Trip Songs"
# interactive flag on
python playlist_generator.py -p "Best songs for a road trip with your best friend" -l 10 -n "Road Trip Songs" -i

Automatic Playlist Creation

When the interactive mode is off, the playlist is created and filled automatically. It is great to create quick playlists, without worrying too much about the contents... Providing that you trust ChatGPT's suggestions, which can be sometimes surprising!

Output when the interactive mode is off:

----------------------------------------------------------------------------------------------------
------------------------------------ SPOTIFY PLAYLIST GENERATOR ------------------------------------
----------------------------------------------------------------------------------------------------
Prompt: Best songs for a road trip with your best friend
Length: 10
Name: Road Trip Songs
Interactive: False
----------------------------------------------------------------------------------------------------
1. Journey - Don't Stop Believin' | Escape (Bonus Track Version)
2. Smash Mouth - All Star | Astro Lounge
3. Bruce Springsteen - Born to Run | Born To Run
4. Guns N' Roses - Sweet Child O' Mine | Appetite For Destruction
5. John Denver - Take Me Home, Country Roads - Original Version | Poems, Prayers and Promises
6. ABBA - Dancing Queen | Arrival
7. Bon Jovi - Livin' On A Prayer | Slippery When Wet
8. Queen - Bohemian Rhapsody | Bohemian Rhapsody (The Original Soundtrack)
9. Lynyrd Skynyrd - Sweet Home Alabama | Second Helping (Expanded Edition)
10. Whitney Houston - I Wanna Dance with Somebody (Who Loves Me) | Whitney
----------------------------------------------------------------------------------------------------

Spotify should open automatically, the songs are added to the playlist and the first one from the list should play when done.

Interactive Playlist Creation

If the interactive flag is on, we get a first batch of suggestions from ChatGPT (using the input playlist length as batch length) and we play the songs one by one in Spotify. For each song, we have the choice to:

  • Add the current song to the Playlist

  • Blacklist this song (don't suggest it anymore)

  • Blacklist this artist (don't suggest any songs from this artist anymore)

  • Quit generating the playlist and keep it as it is

Gary Jules - Mad World   (1/5)
[1] Add to Playlist
[2] Not this song
[3] Not this artist
[q] Quit playlist generation
Your choice:

When the choice is done, we jump to the other song. When the batch is done, we get the following choice:

  • Ask ChatGPT for another batch of songs

  • Quit generating the playlist and keep it as it is

Do you want another batch of songs?
[1] Yes, give me more songs!
[2] No, I'm down with this playlist
Your choice:

When asking ChatGPT for a new batch, we provide it with the list of songs we already have, as well as the lists of blacklisted songs and artists, to take into account for the next suggestions.

Ouput when the interactive playlist is done:

----------------------------------------------------------------------------------------------------
------------------------------------ SPOTIFY PLAYLIST GENERATOR ------------------------------------
----------------------------------------------------------------------------------------------------
Prompt: Lonely songs to play when it's raining
Length: 10
Name: Rainy Mood
Interactive: True
Artists Blacklist: {'The Carpenters', 'Willie Nelson'}
Songs Blacklist: {'Adore', "I Can't Make You Love Me"}
----------------------------------------------------------------------------------------------------
1. System Of A Down - Lonely Day | Hypnotize
2. a-ha - Crying in the Rain | East of the Sun, West of the Moon
3. Brook Benton - Rainy Night in Georgia | Brook Benton Today
4. Green Day - Boulevard of Broken Dreams | Boulevard of Broken Dreams
5. The Doors - Riders on the Storm | L.A. Woman
6. R.E.M. - Everybody Hurts | Automatic For The People
----------------------------------------------------------------------------------------------------

See GitHub readme for additional notes and contributions.

Conclusion

This has been a fun little project and a good first experience playing around with the OpenAI API. The Spotipy library on the other side is very intuitive and you can quickly get around with it and interact with your Spotify account. Obviously, some prompts like "80's hits" will return you more accurate results than creative ones like "Sad songs to listen to when you just broke up and your fridge is empty". But I think switching the model to GPT4 and future models will probably improve on this.