voidKaustubh commited on
Commit
1f15940
·
verified ·
1 Parent(s): 9882eb9

music recommendation app

Browse files
Files changed (1) hide show
  1. app.py +41 -39
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 List
4
  import time
5
  import requests
6
  import pytz
@@ -15,50 +15,52 @@ from Gradio_UI import GradioUI
15
 
16
 
17
  @tool
18
- def search_internet(query: str, max_results: int = 3) -> str:
19
- """A tool that searches the internet for information about a given query.
20
 
21
  Args:
22
- query: The search query (e.g., 'latest AI news', 'climate change updates')
23
- max_results: Maximum number of results to return (default: 3)
 
24
  """
 
 
 
 
25
  try:
26
- # Initialize the DuckDuckGo search
27
- with DDGS() as ddgs:
28
- # Perform the search
29
- results = list(ddgs.text(query, max_results=max_results))
30
-
31
- if not results:
32
- return f"No results found for '{query}'"
33
-
34
- # Format the results
35
- formatted_results = f"Search results for '{query}':\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- for i, result in enumerate(results, 1):
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 fetching time for timezone '{timezone}': {str(e)}"
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, search_internet, get_current_time_in_timezone,], ## add your tools here (don't remove 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,