saayedalam commited on
Commit
001e0a8
·
verified ·
1 Parent(s): 6a4aaa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -4,44 +4,45 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
- import lyricsgenius
8
- import os
9
- import time
10
  from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
 
14
- genius_token = os.getenv("Genius_Lyrics")
15
- genius = lyricsgenius.Genius(genius_token)
16
-
17
- # Add headers to avoid 403 errors
18
- headers = {
19
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"}
20
-
21
- genius._headers = headers # Set the headers in the API client
22
 
23
- # Optional settings
24
- genius.remove_section_headers = True # Removes [Chorus], [Verse] headers
25
- genius.excluded_terms = ["(Remix)", "(Live)"]
26
 
27
  @tool
28
- def get_song_lyrics(song_name: str, artist_name: str) -> str:
29
- """A tool that fetches lyrics from Genius based on the song title and artist name.
30
  Args:
31
- song_name: A string representing a valid name of a song.
32
  artist_name: A string representing a valid name of a song.
33
  """
 
34
 
35
- try:
36
- # Search for the song by title and artist
37
- song = genius.search_song(song_name, artist_name)
 
 
38
 
39
- if song:
40
- return f"The lyrics of the song '{song_name}' by {artist_name} are ...\n{song.lyrics[:300]}..."
 
 
 
 
41
  else:
42
- return "Song not found."
43
- except Exception as e:
44
- return f"Error fetching song lyrics of '{song_name}' by {artist_name}: {str(e)}"
45
 
46
 
47
 
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ import spotipy
8
+ from spotipy.oauth2 import SpotifyClientCredentials
9
+
10
  from Gradio_UI import GradioUI
11
 
12
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
 
14
+ # Set your Spotify API credentials
15
+ client_id = 'f4b7863fa1504190b0d49939fc31a05f'
16
+ client_secret = 'db2c21e185404604a02a5b8d683a6c03'
 
 
 
 
 
17
 
18
+ # Set up Spotify authentication
19
+ client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
20
+ sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
21
 
22
  @tool
23
+ def get_song_lyrics(artist_name: str) -> str:
24
+ """A tool that fetches the most recent album name of a given artist.
25
  Args:
 
26
  artist_name: A string representing a valid name of a song.
27
  """
28
+ result = sp.search(q=artist_name, type='artist', limit=1)
29
 
30
+ if result['artists']['items']:
31
+ artist_id = result['artists']['items'][0]['id']
32
+
33
+ # Get the artist's albums (in reverse chronological order)
34
+ albums = sp.artist_albums(artist_id, album_type='album', limit=1, offset=0)
35
 
36
+ if albums['items']:
37
+ # Get the most recent album
38
+ most_recent_album = albums['items'][0]
39
+
40
+ # Return the name of the most recent album
41
+ return f"The most recent album of the {artist_name} is {most_recent_album['name']}
42
  else:
43
+ return "No albums found for this artist."
44
+ else:
45
+ return "Artist not found."
46
 
47
 
48