This Python project utilizes the Spotify API and machine learning techniques to recommend songs based on user input. The recommendation system incorporates both a list of top songs and a clustering model to provide diverse and personalized suggestions.
Ensure you have the following libraries installed:
pip install pandas regex spotipy scikit-learn
- Clone the repository:
git clone https://github.com/laiagomezmessia/lab-web-scraping-single-page.git
-
Create a Spotify Developer account and obtain your client ID and client secret.
-
Create a file named secrets.txt in the project root:
clientid: YOUR_CLIENT_ID
clientsecret: YOUR_CLIENT_SECRET
-
Ensure you have the following CSV files in the project directory: top_songs.csv: List of top songs. playlist_cluster_df.csv: Playlist with relevant features.
-
Run the recommend_song function in SongRecommender.ipynb:
python song_recommender.py
Enter a song and its artist when prompted to receive a song recommendation.
The recommend_song function performs the following steps:
-
Checks if the user-inputted song is in the top songs list. If yes, it recommends a random song from the remaining top songs.
-
If the user-inputted song is not in the top songs list, it connects to the Spotify API, searches for the song, and retrieves its audio features.
-
Uses a pre-trained KMeans clustering model to predict the cluster of the input song.
-
Recommends a random song from the same cluster in the playlist DataFrame.
-
Introduces a nap to avoid hitting rate limits when interacting with the Spotify API.
-
The clustering model and scaler are loaded using pickle from files named kmeans.pkl and X_prep.pkl. Ensure these files are present in the project directory.
-
The script can be further extended to include additional features or improve the recommendation algorithm.
Feel free to explore, modify, and enhance the project according to your preferences!