This project predicts the acoustic features—acousticness, danceability, and energy—of songs based on their lyrics. Using machine learning models trained on a dataset of song lyrics and corresponding acoustic feature values, the project aims to uncover relationships between text data and audio characteristics. The pipeline also allows for real-time prediction of acoustic features based on user-provided lyrics.
- Introduction
- Project Structure
- Dataset
- Setup and Installation
- Feature Extraction
- Model Training
- Prediction
- Evaluation
- Usage
- Results
- Read More
This project explores the correlation between song lyrics and their acoustic properties. By using text processing techniques and various machine learning models, it aims to predict three acoustic features:
- Acousticness: The likelihood a song is acoustic.
- Danceability: How suitable a track is for dancing.
- Energy: The intensity and activity of a song.
The trained models can predict these values based solely on lyrics, providing insights into how lyrical content relates to a song's sound.
├── src/
│ ├── predictor.py # Script to predict acoustic features from input lyrics
│ ├── vectorizer.py # Script for lyrics preprocessing and vectorization
│ ├── model_trainer(Linear+Others).py # Model training script for each acoustic feature
├── models/
│ ├── acousticness_model.pkl # Trained model for acousticness prediction
│ ├── danceability_model.pkl # Trained model for danceability prediction
│ ├── energy_model.pkl # Trained model for energy prediction
├── .gitignore
├── requirements.txt
└── README.mdThe dataset used in this project includes:
- Song Metadata: Basic song information such as the song title and artist.
- Acoustic Features: Acousticness, danceability, and energy values.
- Lyrics: The primary feature used for prediction.
Ensure your dataset, SingerAndSongs.csv, is saved in the data/ folder with columns such as Song name, Singer, and acoustic features.
-
Clone the Repository:
git clone https://github.com/yourusername/lyrics-acoustic-feature-prediction.git cd lyrics-acoustic-feature-prediction -
Install Required Packages: Make sure to have Python installed, then run:
pip install -r requirements.txt
-
Set Up API Credentials:
- Register for Spotify and Musixmatch API keys.
- Save the credentials in lyrics_finder.py or use environment variables for secure storage.
Run the lyrics_finder.py script to fetch and add lyrics to the dataset. This script uses the Musixmatch API to retrieve lyrics for each song and saves them to a CSV file:
python lyrics_finder.pyThis will create a new dataset file SingerAndSongs_with_lyrics.csv with an additional lyrics column.
- Vectorize Lyrics: Run
vectorizer.pyto preprocess lyrics and convert them into TF-IDF vectors.
python vectorizer.pyThis generates:
tfidf_vectorizer.pkl: The TF-IDF vectorizer saved for later use.split_data.pkl: The vectorized dataset split into train and test sets.
- Train Models: Run
model_trainer.pyto train models for each target feature (acousticness, danceability, energy) using Linear Regression, Ridge Regression, and Gradient Boosting.
python model_trainer.pyEach model and its corresponding RMSE value will be saved in the models folder.
To predict acoustic features for new lyrics, run predictor.py. This script takes lyrics as input and outputs the predicted values for each feature:
python predictor.pyUpon execution, it will prompt for lyrics input and return predicted values for acousticness, danceability, and energy.
The model_trainer.py script prints RMSE values for each model, providing insights into model performance:
Root Mean Squared Error (RMSE) is used to evaluate model accuracy.
This project can be used to predict acoustic characteristics from song lyrics, allowing:
- Lyric-Based Feature Prediction: Using lyrics to estimate acoustic traits.
- Lyric Analysis: Exploring how lyrics impact a song’s audio profile.
- Music Recommendations: Filtering songs based on lyrical content to match desired acoustic profiles.
The models have been tested using the RMSE metric, achieving the following results(for Linear Regression):
- Acousticness RMSE: 0.5444
- Danceability RMSE: 0.3982
- Energy RMSE: 0.4611
The Linear, Ridge, and Gradient Boosting models were evaluated, with Ridge and Gradient Boosting showing improvement over Linear Regression.
Check out my blog on Hashnode - https://yoadvait.hashnode.dev/lyricsanalyzer-song-analysis-with-nlp