Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,12 +42,13 @@ def get_full_poem(verse: str) -> str:
|
|
| 42 |
url = f"https://ganjoor.net/search?s={encoded_query}&es=1&author=0"
|
| 43 |
return url
|
| 44 |
# url = f"https://ganjoor.net/search?s=%D8%B3%D9%84%D8%A7%D9%85&es=1&author=0"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
@tool
|
| 53 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -63,8 +64,86 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 63 |
return f"The current local time in {timezone} is: {local_time}"
|
| 64 |
except Exception as e:
|
| 65 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
final_answer = FinalAnswerTool()
|
| 69 |
|
| 70 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
|
@@ -86,7 +165,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 86 |
|
| 87 |
agent = CodeAgent(
|
| 88 |
model=model,
|
| 89 |
-
tools=[final_answer, get_crypto_price,get_full_poem,get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
| 90 |
max_steps=6,
|
| 91 |
verbosity_level=1,
|
| 92 |
grammar=None,
|
|
|
|
| 42 |
url = f"https://ganjoor.net/search?s={encoded_query}&es=1&author=0"
|
| 43 |
return url
|
| 44 |
# url = f"https://ganjoor.net/search?s=%D8%B3%D9%84%D8%A7%D9%85&es=1&author=0"
|
| 45 |
+
try:
|
| 46 |
+
data = requests.get(url).json()
|
| 47 |
+
price = float(data[crypto][currency])
|
| 48 |
+
return f"${price:,.2f}"
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error: {e}"
|
| 51 |
+
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 64 |
return f"The current local time in {timezone} is: {local_time}"
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 67 |
+
@tool
|
| 68 |
+
def find_the_song(arg1: str) -> str:
|
| 69 |
+
"""A tool that gets the description of song from a verse provided by use.
|
| 70 |
+
Args:
|
| 71 |
+
arg1: A string representing a valid timezone (e.g., 'America/New_York').
|
| 72 |
+
"""
|
| 73 |
+
try:
|
| 74 |
+
search_term = f"site:genius.com OR site:azlyrics.com song lyrics \"{arg1}\"" #Focus on specific lyric sites
|
| 75 |
+
search_tool = DuckDuckGoSearchTool()
|
| 76 |
+
search_results = search_tool.use({"query": search_term})
|
| 77 |
+
|
| 78 |
+
return search_results
|
| 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}"
|
| 146 |
+
|
| 147 |
final_answer = FinalAnswerTool()
|
| 148 |
|
| 149 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
|
|
|
| 165 |
|
| 166 |
agent = CodeAgent(
|
| 167 |
model=model,
|
| 168 |
+
tools=[final_answer, find_the_song, get_crypto_price,get_full_poem,get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
| 169 |
max_steps=6,
|
| 170 |
verbosity_level=1,
|
| 171 |
grammar=None,
|