Building a Playlist Generator with Sentence Transformers

TL;DR

Hugging Face has detailed the construction of a playlist generator that uses Sentence Transformers to perform semantic search across song lyric embeddings, wrapped in a multi-step Gradio application. This project demonstrates how pre-trained embedding models can be used to match user text prompts to relevant song content without requiring custom model training.

Semantic Search with Sentence Transformers

The core functionality of the playlist generator relies on generating sentence embeddings to enable semantic search. Rather than matching keywords, the system identifies songs with meanings similar to the user's prompt.

Model Selection and Implementation

For this project, the sentence-transformers/msmarco-MiniLM-L-6-v3 model was selected. This model is part of the MS MARCO family, which is trained on Bing search engine queries and performs well across various domains.

Handling Long Text with Verse Chunking

Because embedding models have a maximum input sequence length (in this case, 512 word pieces), entire songs often exceed the model's capacity and are truncated. To solve this, the lyrics are split into smaller chunks—specifically verses—and each verse is embedded individually. This approach ensures that the search is more accurate and that the model can digest the full content of the songs.

Generating and Storing Embeddings

Embeddings are generated using the .encode() method of the Sentence Transformers model. In this implementation, the resulting embeddings are stored as pickle files and hosted on the Hugging Face Hub as a dataset for accessibility.

Executing the Search

To find relevant songs, the system uses util.semantic_search to compare the embedding of a user's prompt against the pre-generated corpus of verse embeddings. To ensure a sufficient number of distinct songs are returned, the top_k parameter is set to 20, which typically yields at least 9 unique songs after removing duplicate verses from the same song.

Building the Multi-Step Interface with Gradio

The application uses the Gradio Blocks API to create a non-linear, multi-step user experience where the output of one action informs the next.

Application Workflow

  1. Input: Users enter a text prompt into a gr.TextArea or select an example.
  2. Trigger: A gr.Button triggers the generate_playlist function.
  3. Processing: The function encodes the prompt and performs a semantic search against the verse embeddings.
  4. Output: The results are returned via a gr.Radio component, which is updated dynamically using the .update() method to display the names of the identified songs.
  5. Inspection: Users can then select a song from the radio options to view its lyrics.

Data Integration

The app loads necessary data—including verse embeddings, song mappings, and lyrics—directly from Hugging Face datasets using hf_hub_download upon startup.

Future Enhancements and Resources

The project author identifies several potential directions for expanding the tool's capabilities:

  • Spotify Integration: Automatically generating playlists and using an embedded player for immediate listening.
  • Visual Feedback: Using the HighlightedText Gradio component to show exactly which verse triggered the semantic match.
  • Embedding Visualization: Creating visual representations of the embedding space to better understand the relationships between songs.

Sources