Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -75,71 +75,71 @@ def find_the_song(arg1: str) -> str:
|
|
75 |
search_tool = DuckDuckGoSearchTool()
|
76 |
search_results = search_tool.use({"query": search_term})
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
-
|
130 |
-
|
131 |
|
132 |
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
|
140 |
|
141 |
-
|
142 |
-
|
143 |
|
144 |
except Exception as e:
|
145 |
return f"An error occurred: {e}"
|
|
|
75 |
search_tool = DuckDuckGoSearchTool()
|
76 |
search_results = search_tool.use({"query": search_term})
|
77 |
|
78 |
+
|
79 |
+
if search_results:
|
80 |
+
#Try extracting information from Genius or AZLyrics (prioritized)
|
81 |
+
if "genius.com" in search_results.lower():
|
82 |
+
try:
|
83 |
+
url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the genius URL
|
84 |
+
response = requests.get(url)
|
85 |
+
soup = BeautifulSoup(response.content, 'html.parser') #html parser
|
86 |
+
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
|
87 |
+
artist = soup.find("a", class_="artist_name").text.strip() if soup.find("a", class_="artist_name") else None #same but for artist name
|
88 |
+
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)
|
89 |
+
if lyrics_div:
|
90 |
+
lyrics = lyrics_div.get_text(separator="\n").strip()
|
91 |
+
else:
|
92 |
+
lyrics = None
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
return f"Error scraping Genius: {e}. Raw results: {search_results}"
|
96 |
+
elif "azlyrics.com" in search_results.lower():
|
97 |
+
try:
|
98 |
+
url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the azlyrics URL
|
99 |
+
response = requests.get(url)
|
100 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
101 |
+
lyrics_div = soup.find("div", class_="ringtone") # Lyrics are inside a specific div
|
102 |
+
if lyrics_div:
|
103 |
+
lyrics = lyrics_div.find_next("div").get_text().strip() # Get the lyrics that are in the next div
|
104 |
+
artist_element = soup.find('div', class_='lyricsh') # Find the tag of the lyrics
|
105 |
+
artist = artist_element.find_next('b').text.split('lyrics')[0].strip() #parse the artist's name out
|
106 |
+
song_title = soup.find('title').text.split(' - ')[0].strip() #same for name
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
else:
|
111 |
+
lyrics = None
|
112 |
+
except Exception as e:
|
113 |
+
return f"Error scraping AZLyrics: {e}. Raw results: {search_results}"
|
114 |
+
|
115 |
+
else:
|
116 |
+
return f"Could not find Genius or AZLyrics page, so couldn't extract lyrics. Raw results: {search_results}"
|
117 |
|
118 |
|
119 |
+
if song_title is None or artist is None:
|
120 |
+
title_match = re.search(r"Title:\s*(.*)", search_results, re.IGNORECASE)
|
121 |
+
artist_match = re.search(r"Artist:\s*(.*)", search_results, re.IGNORECASE)
|
122 |
|
123 |
+
song_title = title_match.group(1).strip() if title_match else None
|
124 |
+
artist = artist_match.group(1).strip() if artist_match else None
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
+
if song_title and artist and lyrics and spotify_url:
|
130 |
+
return f"song name: {song_title} , by {artist} . the spotify url is : {spotify_url} \nthe lyrics are : {lyrics}" # return the song + lyrics
|
131 |
|
132 |
|
133 |
|
134 |
+
if song_title and artist:
|
135 |
+
spotify_message = f"\n(Could not reliably extract Spotify URL)" if spotify_url is None else ""
|
136 |
+
lyrics_message = f"\n(Could not reliably extract Lyrics)" if lyrics is None else ""
|
137 |
+
return f"Found song: {song_title} by {artist} . {spotify_message}{lyrics_message}. Raw results: {search_results}"
|
138 |
+
return f"Could not extract full information, check the search results:\n {search_results}" #in case it fails return the search result
|
139 |
|
140 |
|
141 |
+
else:
|
142 |
+
return "Could not find any songs matching the verse."
|
143 |
|
144 |
except Exception as e:
|
145 |
return f"An error occurred: {e}"
|