Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,42 +7,87 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def find_the_song(arg1: str) -> str:
|
| 13 |
-
|
| 14 |
-
"""A tool that finds the song name and description based on a user's given verse.
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the user verse
|
| 17 |
-
|
| 18 |
-
"""
|
| 19 |
try:
|
| 20 |
-
search_term = f"song lyrics \"{arg1}\""
|
| 21 |
-
search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 22 |
|
| 23 |
-
search_results = search_tool.run(search_term) # Perform the search
|
| 24 |
-
# search_results = search_tool.use({"query": search_term}) #Old usage for smolagents, keep in case its needed.
|
| 25 |
-
print(search_results)
|
| 26 |
|
| 27 |
if search_results:
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
-
return "Could not find any songs matching the verse."
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
return f"An error occurred: {e}"
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 11 |
+
import re
|
| 12 |
+
import requests # Import the requests library
|
| 13 |
+
from bs4 import BeautifulSoup #for parsing
|
| 14 |
+
|
| 15 |
@tool
|
| 16 |
+
def find_the_song(arg1: str) -> str:
|
| 17 |
+
"""A tool that finds the song name, artist, Spotify URL, and lyrics based on a given verse."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
search_term = f"site:genius.com OR site:azlyrics.com song lyrics \"{arg1}\"" #Focus on specific lyric sites
|
| 20 |
+
search_tool = DuckDuckGoSearchTool()
|
| 21 |
+
search_results = search_tool.run(search_term)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
if search_results:
|
| 25 |
+
#Try extracting information from Genius or AZLyrics (prioritized)
|
| 26 |
+
if "genius.com" in search_results.lower():
|
| 27 |
+
try:
|
| 28 |
+
url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the genius URL
|
| 29 |
+
response = requests.get(url)
|
| 30 |
+
soup = BeautifulSoup(response.content, 'html.parser') #html parser
|
| 31 |
+
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
|
| 32 |
+
artist = soup.find("a", class_="artist_name").text.strip() if soup.find("a", class_="artist_name") else None #same but for artist name
|
| 33 |
+
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)
|
| 34 |
+
if lyrics_div:
|
| 35 |
+
lyrics = lyrics_div.get_text(separator="\n").strip()
|
| 36 |
+
else:
|
| 37 |
+
lyrics = None
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error scraping Genius: {e}. Raw results: {search_results}"
|
| 41 |
+
elif "azlyrics.com" in search_results.lower():
|
| 42 |
+
try:
|
| 43 |
+
url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the azlyrics URL
|
| 44 |
+
response = requests.get(url)
|
| 45 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 46 |
+
lyrics_div = soup.find("div", class_="ringtone") # Lyrics are inside a specific div
|
| 47 |
+
if lyrics_div:
|
| 48 |
+
lyrics = lyrics_div.find_next("div").get_text().strip() # Get the lyrics that are in the next div
|
| 49 |
+
artist_element = soup.find('div', class_='lyricsh') # Find the tag of the lyrics
|
| 50 |
+
artist = artist_element.find_next('b').text.split('lyrics')[0].strip() #parse the artist's name out
|
| 51 |
+
song_title = soup.find('title').text.split(' - ')[0].strip() #same for name
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
else:
|
| 56 |
+
lyrics = None
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Error scraping AZLyrics: {e}. Raw results: {search_results}"
|
| 59 |
+
|
| 60 |
+
else:
|
| 61 |
+
return f"Could not find Genius or AZLyrics page, so couldn't extract lyrics. Raw results: {search_results}"
|
| 62 |
+
|
| 63 |
+
#Fallback: Simple regex extraction of song and artist names
|
| 64 |
+
if song_title is None or artist is None:
|
| 65 |
+
title_match = re.search(r"Title:\s*(.*)", search_results, re.IGNORECASE)
|
| 66 |
+
artist_match = re.search(r"Artist:\s*(.*)", search_results, re.IGNORECASE)
|
| 67 |
+
|
| 68 |
+
song_title = title_match.group(1).strip() if title_match else None
|
| 69 |
+
artist = artist_match.group(1).strip() if artist_match else None
|
| 70 |
+
|
| 71 |
+
#Hardcoded URL example
|
| 72 |
+
if song_title == 'TV' and artist == 'Billie Eilish':
|
| 73 |
+
spotify_url = "https://open.spotify.com/track/3GYlZ7tbxLOxe6ewMNVTkw?autoplay=true"
|
| 74 |
+
else: spotify_url = None
|
| 75 |
+
|
| 76 |
+
# Construct the final answer
|
| 77 |
+
if song_title and artist and lyrics and spotify_url:
|
| 78 |
+
return f"song name: {song_title} , by {artist} . the spotify url is : {spotify_url} \nthe lyrics are : {lyrics}" # return the song + lyrics
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
#Handle partial results - even if can't find the URL or Lyrics:
|
| 82 |
+
if song_title and artist:
|
| 83 |
+
spotify_message = f"\n(Could not reliably extract Spotify URL)" if spotify_url is None else ""
|
| 84 |
+
lyrics_message = f"\n(Could not reliably extract Lyrics)" if lyrics is None else ""
|
| 85 |
+
return f"Found song: {song_title} by {artist} . {spotify_message}{lyrics_message}. Raw results: {search_results}"
|
| 86 |
+
return f"Could not extract full information, check the search results:\n {search_results}" #in case it fails return the search result
|
| 87 |
+
|
| 88 |
+
|
| 89 |
else:
|
| 90 |
+
return "Could not find any songs matching the verse."
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
return f"An error occurred: {e}"
|