LyricsAnalyzerAgent / tools /analysis_tools.py
tonko22's picture
Recent updates (WIP)
033ead0
"""
Analysis tools for understanding and interpreting song lyrics.
"""
import os
from loguru import logger
from smolagents import Tool
import json
from typing import Dict
from config import get_model_id
from api_utils import make_api_call_with_retry
class AnalyzeLyricsTool(Tool):
"""Tool for analyzing song lyrics"""
name = "analyze_lyrics"
description = "Analyzes song lyrics, given its text as a string, 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 __init__(self, model_id=None):
super().__init__()
self.model_id = get_model_id(provider='openrouter')
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 = self.model_id
logger.info("Using {} for lyrics analysis", model_to_use)
# Use the function with retry mechanism
logger.info("Analyzing lyrics for song: '{}' by '{}'", song_title, artist)
response_text = make_api_call_with_retry(model_to_use, prompt)
# Parse the string response into a JSON object (dictionary)
logger.debug(f"Parsing JSON response for {song_title}")
response_json = json.loads(response_text)
return response_json