Music, Lyrics, and Agentic AI: Constructing a Smart Song Explainer using Python and OpenAI

-

is rap.

Rap music made me cry, sing, and dance, and it’s also how I learned the English language. I still remember the primary time I sat down and tried to know Biggie and Tupac. I remember learning about Brooklyn, about California, the slang, the struggles, and all the opposite powerful messages behind those songs.

Rap music is the most effective example of how lyrics alone could make music a bit of art. A few of the most effective hip-hop songs are actually a sample, some drums, and an individual talking poetry on a 4/4 tempo.

And while for somebody reading the lyrics won’t even be needed, as they know the language, for non-English speakers like myself, tools like Genius made life incredibly easier. Genius is a web based lyrics music collector: for those who’re in search of the lyrics of a song, Genius is your guy. Because of Genius, even when I didn’t understand what Biggie was saying, I could sit down and skim the lyrics, then Google them and translate them. Not only that: when a rapper (or a singer basically) makes a selected reference that is just too hard to know, Genius will make clear that for you thru some side-bar snippets.

Image from Genius [link]

But how does Genius try this? How do they produce such insightful, up-to-date, useful snippets?

Well, , when Genius was born, I might say they’d mainly produce these sorts of snippets manually: users would just add their comment, perhaps some moderator could review a few of them, and that’s it, closed deal.

Nonetheless, , with the powerful AI technologies that now we have, the pipeline may be made much smoother and efficient. While I don’t consider that an agentic AI would have the option to do the job of a music expert (for thus many reasons), I consider that an individual with such domain knowledge could possibly be helped by the agentic AI, which would supply them with the suitable tools to create the snippets.

And is what we’re doing today. 🙂

We are going to use Streamlit, Python, and OpenAI to construct an excellent easy web app that, given a song’s lyrics, provides clarification to what the piece of text means. More specifically, we’re going to allow the user to ask questions concerning the piece of text, making the Genius idea more “interactive”. We’re also going to offer our AI Agent the Web Search results to permit the LLM to have a look at other songs and resources when crafting the reply.

To spice things up (and for copyright purposes lol), we’re going to also create our own songs, using one other AI agent.

Exciting! Let’s start! 🚀

Should you are eager about the of this experiment, skip to the section. If you desire to craft the magic with me, start from next section. No judgments. 🙂

1. System Design

Let’s first design our system. That is the way it looks:

Image made by creator

More specifically:

  1. The User has the flexibility to create the song from scratch using an AI Agent. That’s optional; a batch of songs has already been generated and may be used as an alternative.
  2. The User can select an element of the text and ask a matter.
  3. The AI Agent can generate the response in a “Genius-like” style.

The AI Agent can be equipped with:

a. The “Internal Song Knowledge“, which consists of the extracted features/metadata of the song (e.g., vibe, title, theme, etc…).
b. The Web Search Tool, which allows the agent to surf the online to search for songs and add context to the query

This design has good modularity, meaning it is simple so as to add bits and pieces to extend the complexity of the system. For instance, if you desire to make the song generation more sophisticated, you may easily tweak the Song Generation Agent without going crazy over the opposite parts of the code.

Let’s construct this piece by piece. 🧱

2. Code

2.1 Setup

The entire code may be present in this Github folder

The structure of our work will probably be as follows:

  1. A lyrics generator, which is generate_madeup_lyrics.py
  2. The lyrics answer, which is qa.py
  3. The web app itself (the file we’ll run through Streamlit), which is lyricsgpt_app.py
  4. A bunch of helpers (resembling utils.py, constants.py, config.py, etc…)

The info will probably be stored in a data folder as well.

Image made by creator

I don’t need to bore you with all of the tiny details, so I’ll only describe the primary components of this structure. Let’s start with the core: the Streamlit app.

2.2 Streamlit App

Please note: you have to to have a OpenAI API key ready, for each the Streamlit app and whatever requires the LLM generation. Outside of the Streamlit app, the simplest way is to set it using OS: os.getenv(“OPENAI_API_KEY”) = “api_key”. Throughout the app, you will probably be prompted to copy-paste it. Don’t worry, it’s all local.

The entire thing runs with the next command:

streamlit run lyricsgpt_app.py

Where lyricsgpt_app.py is the next block of code:

This is kind of long, but pretty straightforward: every line represents a bit of the online app:

  1. The title of the online app
  2. The song selector, which allows the user to pick the lyrics from the info folder (more about this later)
  3. The box for the block of text, where the user can copy and paste the a part of the lyrics of interest
  4. The query box, where the user can ask a matter concerning the a part of the lyrics of interest chosen above
  5. The reply box, where the LLM can answer the query.

But that is “” the executor; the dirty work is finished by the lyricsgpt module and its objects/functions. Let’s see just a few!

2.3 Lyrics Generator

This part is optional and never included in the online app, which focuses on the Genius-like Lyrics Explainer. Be at liberty to skip this for those who are eager about that. But I actually have to say it: this part is pretty cool.

For the lyrics generation, the sport is straightforward:

  • give me a title, vibe, theme, and a few hidden message in a twist.
  • provide you with the lyrics.

For instance:

Produces:

Verse 1
Underneath town lights, whispers danced along the skyline, we were shining through the night, like diamonds in a midnight sky. But love was only a high-rise dream, built on fragile seams, now I’m pulling off the freeway, leaving echoes in between.

Chorus
And it’s all glitter within the rearview, fading into shades of blue, every laugh and each tear, drifting out of view. Because the road unrolls ahead, I let the memories stew, it’s just glitter within the rearview, letting go of me and also you.

[Some more LLM Generated Text]

Outro
So I drive into the silence, where the past can’t misconstrue, leaving glitter within the rearview, and the shards of me and also you. It’s just glitter within the rearview, a love story gone too soon.

Pretty cool, right? The code to try this is the next:

You’ll be able to play with it by modifying SONG_PROMPTS, which looks like this:

Each time you generate a song, it can go right into a JSON file. By default, that’s data/generated_lyrics.json. You don’t should necessarily generate a song; there are already some examples I made in there.

2.4 Lyrics Explainer

The good thing about this whole Agentic era is the period of time you save to construct these things. The Query-Answering logic, plus the flexibility of the AI to make use of online web search, is on this block of code:

This does all of it: answer the query, reads the lyrics metadata, and integrates information online if prompted/needed.

I didn’t need to get too fancy, but you may actually equip the agent with the web_search tool. On this case, I’m parsing the data directly; for those who give the LLM the tool, it might resolve when and if search online.

Okay, but does this work? How do the outcomes appear like? Let’s discover!

3. The magic!

That is an example of the online app.

  1. You copy-paste your OpenAI API key, and you choose a song. Say “Glitter within the Rearview”.
Image made by creator

2. Select the a part of interest. For instance, let’s say I’m an Italian guy (which I’m lol), and I exploit meters, so I don’t understand how far a mile is. I might also wish to know if there’s a reference to something specific when the singer says 13 miles (“Thirteen miles to freedom” is the primary sentence within the bridge)

3. See the magic!

Image made by creator

Let’s try something harder. Here, I pasted the entire second verse, and I asked the AI to offer me which singer would write something like this.

The AI points out Taylor Swift and Adele. Especially the Taylor Swift reference is amazingly accurate, because the songs about breakups and love stories are her biggest hits. She also on her popularity and the way her life is affected by it in songs like “I do know places”:

Lights flash and we’ll run for the fences, Allow them to say what they need, we won’t hear it

Taylor Swift – I do know places

I’ll admit I needed to Google that.

4. Some considering…

Now, it’s a plug-and-play weekend project, lower than an MVP. Nonetheless, it provides three widespread takeaways:

  1. When supplied with the suitable tools and metadata, the LLM really shines and provides insightful information (just like the Taylor Swift suggestion)
  2. Constructing an LLM wrapper is way easier than it was even 5 months ago. The way in which this technology is evolving permits you to be more productive than ever.
  3. Agentic AI can really be applied in every single place. Quite than getting scared about it, it’s best to embrace it and see what we are able to do with it.

5. Conclusions

Thanks for spending time with me; it means quite a bit ❤️. Here’s what now we have done together:

  • Designed a Genius-inspired system powered by Agentic AI that may explain song lyrics interactively.
  • Developed the backend components in Python and Streamlit, from the lyrics generator to the Q&A engine.
  • Built the AI agent with internal song knowledge and an internet search tool for contextual answers.
  • Built an app that may interpret lyrics intelligently.
  • Had some fun while doing that, I hope. 🙂

I would like to shut with my 2 cents. On my each day commute, I hearken to podcasts (often interviews) of artists who make music, and so they explain the songs, the references, and the lyrics. If someone replaces those musicians with an AI, I’m going to RIOT. Not () because I just like the people themselves, but because I consider that they’ll explain things with a passion, a depth, and an empathy that LLMs aren’t in a position to provide (and I feel they never will).

Nonetheless, if we offer music critics with these sorts of AI tools, their work becomes much easier, and so they can 10x their productivity.

7. Before you head out!

Thanks again on your time. It means quite a bit ❤️

My name is Piero Paialunga, and I’m this guy here:

Image made by creator

I’m originally from Italy, hold a Ph.D. from the University of Cincinnati, and work as a Data Scientist at The Trade Desk in Latest York City. I write about AI, Machine Learning, and the evolving role of knowledge scientists each here on TDS and on LinkedIn. Should you liked the article and need to know more about machine learning and follow my studies, you may:

A. Follow me on Linkedin, where I publish all my stories
B. Follow me on GitHub, where you may see all my code
C. For questions, you may send me an email at 

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x