Spaces:
Sleeping
Sleeping
music recommendation app
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
-
from typing import
|
| 4 |
import time
|
| 5 |
import requests
|
| 6 |
import pytz
|
|
@@ -15,50 +15,52 @@ from Gradio_UI import GradioUI
|
|
| 15 |
|
| 16 |
|
| 17 |
@tool
|
| 18 |
-
def
|
| 19 |
-
"""A tool that
|
| 20 |
|
| 21 |
Args:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
formatted_results += f"{i}. {result['title']}\n"
|
| 39 |
-
formatted_results += f" {result['link']}\n"
|
| 40 |
-
formatted_results += f" {result['body']}\n\n"
|
| 41 |
-
|
| 42 |
-
return formatted_results
|
| 43 |
-
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Error performing search: {str(e)}"
|
| 46 |
|
| 47 |
-
|
| 48 |
-
@tool
|
| 49 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
| 50 |
-
"""A tool that fetches the current local time in a specified timezone.
|
| 51 |
-
Args:
|
| 52 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 53 |
-
"""
|
| 54 |
-
try:
|
| 55 |
-
# Create timezone object
|
| 56 |
-
tz = pytz.timezone(timezone)
|
| 57 |
-
# Get current time in that timezone
|
| 58 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 59 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 60 |
except Exception as e:
|
| 61 |
-
return f"Error
|
| 62 |
|
| 63 |
|
| 64 |
final_answer = FinalAnswerTool()
|
|
@@ -82,7 +84,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 82 |
|
| 83 |
agent = CodeAgent(
|
| 84 |
model=model,
|
| 85 |
-
tools=[final_answer,
|
| 86 |
max_steps=6,
|
| 87 |
verbosity_level=1,
|
| 88 |
grammar=None,
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
+
from typing import Optional
|
| 4 |
import time
|
| 5 |
import requests
|
| 6 |
import pytz
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
@tool
|
| 18 |
+
def get_similar_songs(song_title: str, artist_name: str, num_recommendations: int = 5) -> str:
|
| 19 |
+
"""A tool that suggests similar songs based on a given song and artist.
|
| 20 |
|
| 21 |
Args:
|
| 22 |
+
song_title: Name of the song
|
| 23 |
+
artist_name: Name of the artist
|
| 24 |
+
num_recommendations: Number of similar songs to return (default: 5)
|
| 25 |
"""
|
| 26 |
+
# LastFM API endpoint and parameters
|
| 27 |
+
API_KEY = "d17717e179b91347018a13452956c8a0" # Replace with your LastFM API key
|
| 28 |
+
BASE_URL = "http://ws.audioscrobbler.com/2.0/"
|
| 29 |
+
|
| 30 |
try:
|
| 31 |
+
# Get similar tracks
|
| 32 |
+
params = {
|
| 33 |
+
"method": "track.getsimilar",
|
| 34 |
+
"artist": artist_name,
|
| 35 |
+
"track": song_title,
|
| 36 |
+
"api_key": API_KEY,
|
| 37 |
+
"format": "json",
|
| 38 |
+
"limit": num_recommendations
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
response = requests.get(BASE_URL, params=params)
|
| 42 |
+
data = response.json()
|
| 43 |
+
|
| 44 |
+
# Check if we got valid results
|
| 45 |
+
if "similartracks" not in data or "track" not in data["similartracks"]:
|
| 46 |
+
return f"No similar songs found for '{song_title}' by {artist_name}. Please check the song and artist names."
|
| 47 |
+
|
| 48 |
+
# Format the recommendations
|
| 49 |
+
similar_tracks = data["similartracks"]["track"]
|
| 50 |
+
|
| 51 |
+
result = f"Similar songs to '{song_title}' by {artist_name}:\n\n"
|
| 52 |
+
|
| 53 |
+
for i, track in enumerate(similar_tracks, 1):
|
| 54 |
+
result += f"{i}. \"{track['name']}\" by {track['artist']['name']}\n"
|
| 55 |
+
result += f" Match score: {float(track['match'])*100:.1f}%\n"
|
| 56 |
+
if 'playcount' in track:
|
| 57 |
+
result += f" Playcount: {track['playcount']:,}\n"
|
| 58 |
+
result += "\n"
|
| 59 |
|
| 60 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"Error finding similar songs: {str(e)}"
|
| 64 |
|
| 65 |
|
| 66 |
final_answer = FinalAnswerTool()
|
|
|
|
| 84 |
|
| 85 |
agent = CodeAgent(
|
| 86 |
model=model,
|
| 87 |
+
tools=[final_answer, get_similar_songs], ## add your tools here (don't remove final answer)
|
| 88 |
max_steps=6,
|
| 89 |
verbosity_level=1,
|
| 90 |
grammar=None,
|