LyricsAnalyzerAgent / tools /analysis_tools.py
tonko22's picture
Remove serper
83c3279
raw
history blame
3.08 kB
"""
Analysis tools for understanding and interpreting song lyrics.
"""
from loguru import logger
from smolagents import Tool
from api_utils import make_api_call_with_retry
class AnalyzeLyricsTool(Tool):
"""Tool for analyzing song lyrics"""
name = "analyze_lyrics"
description = "Analyzes song lyrics and returns a detailed analysis"
inputs = {
"song_title": {"type": "string", "description": "Title of the song"},
"artist": {"type": "string", "description": "Name of the artist"},
"lyrics": {"type": "string", "description": "Lyrics of the song"}
}
output_type = "string"
def forward(self, song_title: str, artist: str, lyrics: str) -> str:
"""
Performs a deep analysis of the given lyrics and artist metadata.
Args:
song_title: Title of the song or music track.
artist: The name of the artist.
lyrics: The lyrics of the song.
format_output: Whether to format the output using rich formatting (default: True).
Returns:
A formatted full lyrics analysis and meaning in English.
"""
prompt = f"""You are an expert in songs and their meanings.
Analyze the song "{song_title}" by {artist}. Return a structured JSON with the following information:
1. Overall analysis of the song including themes, mood, meaning, and context.
2. Section-by-section meaning analysis of each part of the song (verses, chorus, bridge, etc.).
4. A conclusion about the vibes and concepts of the underlying meaning of the song. Try to get deeper into the meaning of the song if the text has the potential.
Format your response as a valid JSON object with the following structure:
```
{{
"summary": "Overall analysis of the song vibes, meaning and mood",
"main_themes": ["theme1", "theme2", ...],
"mood": "The overall mood/emotion of the song",
"sections_analysis": [
{{
"section_type": "verse/chorus/bridge/etc.",
"section_number": 1,
"lines": ["line1", "line2", ...],
"analysis": "Analysis of this section whith respect to the overall theme"
}},
...
],
"conclusion": "The song vibes and concepts of the underlying meaning, including ideas author may have intended to express"
}}
```
Make sure your response is a properly formatted JSON. Only provide the JSON object, no other text.
Here are the lyrics to analyze:
{lyrics}
"""
model_to_use = "gemini/gemini-2.0-flash"
logger.info("Using Gemini model: {} for lyrics analysis", model_to_use)
# Use the function with retry mechanism
logger.info("Analyzing lyrics for song: '{}' by '{}'", song_title, artist)
response = make_api_call_with_retry(model_to_use, prompt)
return response