File size: 3,487 Bytes
ce0ec3b
 
 
033ead0
ce0ec3b
0e9bb01
7539685
 
033ead0
ce0ec3b
 
 
0e9bb01
 
7c390d2
0e9bb01
f252842
0e9bb01
 
 
6933136
 
0e9bb01
7c390d2
7cc9db2
 
033ead0
7cc9db2
6933136
0e9bb01
6ebcb2e
ce0ec3b
0e9bb01
 
 
 
 
ce0ec3b
0e9bb01
6ebcb2e
0e9bb01
 
 
 
 
 
 
6ebcb2e
6740d85
0e9bb01
 
 
 
6ebcb2e
0e9bb01
 
 
 
 
 
 
6ebcb2e
0e9bb01
 
 
6740d85
0e9bb01
 
 
 
 
 
 
 
 
7cc9db2
7539685
0e9bb01
 
 
c3b461c
7539685
c3b461c
 
 
 
 
0e9bb01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
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