uncleMehrzad commited on
Commit
e3fe453
·
verified ·
1 Parent(s): 3ca0572

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -3
app.py CHANGED
@@ -13,14 +13,89 @@ import requests # Import the requests library
13
  from bs4 import BeautifulSoup #for parsing
14
 
15
  @tool
16
- def find_the_song(arg1:str, arg2:int)-> str: #it's import to specify the return type
17
  #Keep this format for the description / args / args description but feel free to modify the tool
18
  """A tool that does nothing yet
19
  Args:
20
  arg1: the first argument
21
- arg2: the second argument
22
  """
23
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  @tool
26
  def get_current_time_in_timezone(timezone: str) -> str:
 
13
  from bs4 import BeautifulSoup #for parsing
14
 
15
  @tool
16
+ def find_the_song(arg1:str)-> str: #it's import to specify the return type
17
  #Keep this format for the description / args / args description but feel free to modify the tool
18
  """A tool that does nothing yet
19
  Args:
20
  arg1: the first argument
21
+
22
  """
23
+ try:
24
+ search_term = f"site:genius.com OR site:azlyrics.com song lyrics \"{arg1}\"" #Focus on specific lyric sites
25
+ search_tool = DuckDuckGoSearchTool()
26
+ search_results = search_tool.run(search_term)
27
+
28
+
29
+ if search_results:
30
+ #Try extracting information from Genius or AZLyrics (prioritized)
31
+ if "genius.com" in search_results.lower():
32
+ try:
33
+ url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the genius URL
34
+ response = requests.get(url)
35
+ soup = BeautifulSoup(response.content, 'html.parser') #html parser
36
+ song_title = soup.find("h1", class_="song_title").text.strip() if soup.find("h1", class_="song_title") else None #search song's name from specific h1 tag
37
+ artist = soup.find("a", class_="artist_name").text.strip() if soup.find("a", class_="artist_name") else None #same but for artist name
38
+ lyrics_div = soup.find("div", class_="lyrics") if soup.find("div", class_="lyrics") else soup.find("div", class_="Lyrics__Container") #finding lyrics tags (check them both if one is not available)
39
+ if lyrics_div:
40
+ lyrics = lyrics_div.get_text(separator="\n").strip()
41
+ else:
42
+ lyrics = None
43
+
44
+ except Exception as e:
45
+ return f"Error scraping Genius: {e}. Raw results: {search_results}"
46
+ elif "azlyrics.com" in search_results.lower():
47
+ try:
48
+ url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the azlyrics URL
49
+ response = requests.get(url)
50
+ soup = BeautifulSoup(response.content, 'html.parser')
51
+ lyrics_div = soup.find("div", class_="ringtone") # Lyrics are inside a specific div
52
+ if lyrics_div:
53
+ lyrics = lyrics_div.find_next("div").get_text().strip() # Get the lyrics that are in the next div
54
+ artist_element = soup.find('div', class_='lyricsh') # Find the tag of the lyrics
55
+ artist = artist_element.find_next('b').text.split('lyrics')[0].strip() #parse the artist's name out
56
+ song_title = soup.find('title').text.split(' - ')[0].strip() #same for name
57
+
58
+
59
+
60
+ else:
61
+ lyrics = None
62
+ except Exception as e:
63
+ return f"Error scraping AZLyrics: {e}. Raw results: {search_results}"
64
+
65
+ else:
66
+ return f"Could not find Genius or AZLyrics page, so couldn't extract lyrics. Raw results: {search_results}"
67
+
68
+ #Fallback: Simple regex extraction of song and artist names
69
+ if song_title is None or artist is None:
70
+ title_match = re.search(r"Title:\s*(.*)", search_results, re.IGNORECASE)
71
+ artist_match = re.search(r"Artist:\s*(.*)", search_results, re.IGNORECASE)
72
+
73
+ song_title = title_match.group(1).strip() if title_match else None
74
+ artist = artist_match.group(1).strip() if artist_match else None
75
+
76
+ #Hardcoded URL example
77
+ if song_title == 'TV' and artist == 'Billie Eilish':
78
+ spotify_url = "https://open.spotify.com/track/3GYlZ7tbxLOxe6ewMNVTkw?autoplay=true"
79
+ else: spotify_url = None
80
+
81
+ # Construct the final answer
82
+ if song_title and artist and lyrics and spotify_url:
83
+ return f"song name: {song_title} , by {artist} . the spotify url is : {spotify_url} \nthe lyrics are : {lyrics}" # return the song + lyrics
84
+
85
+
86
+ #Handle partial results - even if can't find the URL or Lyrics:
87
+ if song_title and artist:
88
+ spotify_message = f"\n(Could not reliably extract Spotify URL)" if spotify_url is None else ""
89
+ lyrics_message = f"\n(Could not reliably extract Lyrics)" if lyrics is None else ""
90
+ return f"Found song: {song_title} by {artist} . {spotify_message}{lyrics_message}. Raw results: {search_results}"
91
+ return f"Could not extract full information, check the search results:\n {search_results}" #in case it fails return the search result
92
+
93
+
94
+ else:
95
+ return "Could not find any songs matching the verse."
96
+
97
+ except Exception as e:
98
+ return f"An error occurred: {e}"
99
 
100
  @tool
101
  def get_current_time_in_timezone(timezone: str) -> str: