Spaces:
Sleeping
Sleeping
add last_fm currently listening api
Browse files
app.py
CHANGED
|
@@ -4,9 +4,17 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
@@ -33,6 +41,60 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -55,7 +117,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
lastfm_api_root = 'http://ws.audioscrobbler.com/2.0/'
|
| 12 |
+
cache = {
|
| 13 |
+
"current_track": None,
|
| 14 |
+
"last_fetched": 0,
|
| 15 |
+
"listening": False
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 19 |
@tool
|
| 20 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 43 |
|
| 44 |
+
@tool
|
| 45 |
+
def get_recent_tracks(user: str) -> str:
|
| 46 |
+
"""Retrieves the currently playing or most recently played track for a given user from Last.fm.
|
| 47 |
+
|
| 48 |
+
This function uses a cache to store the most recent track information and only fetches
|
| 49 |
+
new data from the Last.fm API if the cache is empty or older than 60 seconds.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
user (str): The Last.fm username for which to retrieve track information.
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
str: A string describing the user's current or recent listening activity.
|
| 56 |
+
|
| 57 |
+
The format of the returned string is:
|
| 58 |
+
- "Currently listening: {track_title} by {artist_name}" if currently playing.
|
| 59 |
+
- "Recently listened: {track_title} by {artist_name}" if recently played.
|
| 60 |
+
- "No recent track information available for {user}." if no track information is found.
|
| 61 |
+
|
| 62 |
+
Example:
|
| 63 |
+
>>> get_recent_tracks('rj')
|
| 64 |
+
'Currently listening: Bohemian Rhapsody by Queen'
|
| 65 |
+
|
| 66 |
+
Notes:
|
| 67 |
+
- Requires the environment variable `LASTFM_API_KEY` to be set with your Last.fm API key.
|
| 68 |
+
- Uses a cache to avoid unnecessary API calls. The cache is updated every 60 seconds.
|
| 69 |
+
- If the Last.fm API is unavailable or returns an error, a message indicating no track information is returned.
|
| 70 |
+
"""
|
| 71 |
+
global cache
|
| 72 |
+
|
| 73 |
+
current_time = time.time()
|
| 74 |
+
# Fetch new data if cache is empty or expired
|
| 75 |
+
if cache['current_track'] is None or current_time - cache['last_fetched'] > 60:
|
| 76 |
+
params = {"method": "user.getrecenttracks",
|
| 77 |
+
"user": USER,
|
| 78 |
+
"api_key": os.getenv('LASTFM_API_KEY'),
|
| 79 |
+
"limit": 1,
|
| 80 |
+
"format": "json"}
|
| 81 |
+
response = requests.get(lastfm_api_root, params=params)
|
| 82 |
+
if response.status_code == 200:
|
| 83 |
+
track = response.json()['recenttracks']['track'][0]
|
| 84 |
+
artist = track['artist']['#text']
|
| 85 |
+
title = track['name']
|
| 86 |
+
if track.get('@attr') and track.get('@attr').get('nowplaying') == 'true':
|
| 87 |
+
cache['current_track'] = f'{title} by {artist}'
|
| 88 |
+
cache['last_fetched'] = current_time
|
| 89 |
+
cache['listening'] = True
|
| 90 |
+
|
| 91 |
+
#If the cache current_track is still None it means that the user has no recent tracks
|
| 92 |
+
if cache['current_track'] is None:
|
| 93 |
+
return f"No recent track information available for {user}."
|
| 94 |
+
|
| 95 |
+
return_prompt = f'Currently listening: {cache["current_track"]}' if cache['listening'] else f'Recently listened: {cache["current_track"]}'
|
| 96 |
+
return return_prompt
|
| 97 |
+
|
| 98 |
|
| 99 |
final_answer = FinalAnswerTool()
|
| 100 |
|
|
|
|
| 117 |
|
| 118 |
agent = CodeAgent(
|
| 119 |
model=model,
|
| 120 |
+
tools=[final_answer, get_current_time_in_timezone, image_generation_tool, get_recent_tracks], ## add your tools here (don't remove final answer)
|
| 121 |
max_steps=6,
|
| 122 |
verbosity_level=1,
|
| 123 |
grammar=None,
|