This repository contains a Flask-based API for predicting the genre of a song based on its lyrics. The API utilizes a pre-trained machine learning model to perform genre predictions.
- Predict Genre: Send a POST request with song lyrics, and receive a predicted genre in response.
- Pre-trained Model: The API uses a pre-trained model to provide quick and accurate genre predictions.
Make sure you have the following installed:
- Python 3.6 or later
- Flask
- joblib
You can install the required Python packages using pip:
pip install Flask joblib- Clone the repository:
git clone https://github.com/yourusername/genre-prediction-api.git
cd genre-prediction-api- Place your trained model file (
trained_model.joblib) in the project directory. Ensure the model file path in the code matches the location where you place your model.
- Run the Flask application:
python app.py- The API will be accessible at
http://127.0.0.1:5000.
-
URL:
/predict -
Method:
POST -
Request Body: JSON object containing the song lyrics.
{ "lyrics": "Your song lyrics here" } -
Response: JSON object containing the predicted genre.
{ "predicted_genre": "The predicted genre" }
Use curl or any HTTP client to send a POST request to the API:
curl -X POST http://127.0.0.1:5000/predict -H "Content-Type: application/json" -d '{"lyrics": "your song lyrics here"}'{
"predicted_genre": "The predicted genre"
}-
If the lyrics are not provided in the request, the API responds with a
400 Bad Requeststatus and an error message:{ "error": "Lyrics not provided" } -
If the model file is not found, the API responds with a
500 Internal Server Errorstatus and an error message:{ "error": "Model file not found. Make sure to train the model first." }
To train your own model, you need to prepare a dataset of song lyrics with their corresponding genres. You can use various machine learning libraries such as scikit-learn to train your model and save it using joblib. Ensure the saved model is named trained_model.joblib and placed in the project directory.
Eminem Rap-God
Ice-T -6 'N the Mornin!