Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,91 +9,27 @@ from Gradio_UI import GradioUI
|
|
| 9 |
import re
|
| 10 |
import requests # Import the requests library
|
| 11 |
from bs4 import BeautifulSoup #for parsing
|
|
|
|
| 12 |
|
| 13 |
@tool
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
Args:
|
| 18 |
-
|
| 19 |
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the genius URL
|
| 32 |
-
response = requests.get(url)
|
| 33 |
-
soup = BeautifulSoup(response.content, 'html.parser') #html parser
|
| 34 |
-
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
|
| 35 |
-
artist = soup.find("a", class_="artist_name").text.strip() if soup.find("a", class_="artist_name") else None #same but for artist name
|
| 36 |
-
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)
|
| 37 |
-
if lyrics_div:
|
| 38 |
-
lyrics = lyrics_div.get_text(separator="\n").strip()
|
| 39 |
-
else:
|
| 40 |
-
lyrics = None
|
| 41 |
-
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return f"Error scraping Genius: {e}. Raw results: {search_results}"
|
| 44 |
-
elif "azlyrics.com" in search_results.lower():
|
| 45 |
-
try:
|
| 46 |
-
url = re.search(r'(https?://[^\s]+)', search_results).group(0) # Get the azlyrics URL
|
| 47 |
-
response = requests.get(url)
|
| 48 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
| 49 |
-
lyrics_div = soup.find("div", class_="ringtone") # Lyrics are inside a specific div
|
| 50 |
-
if lyrics_div:
|
| 51 |
-
lyrics = lyrics_div.find_next("div").get_text().strip() # Get the lyrics that are in the next div
|
| 52 |
-
artist_element = soup.find('div', class_='lyricsh') # Find the tag of the lyrics
|
| 53 |
-
artist = artist_element.find_next('b').text.split('lyrics')[0].strip() #parse the artist's name out
|
| 54 |
-
song_title = soup.find('title').text.split(' - ')[0].strip() #same for name
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
else:
|
| 59 |
-
lyrics = None
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return f"Error scraping AZLyrics: {e}. Raw results: {search_results}"
|
| 62 |
-
|
| 63 |
-
else:
|
| 64 |
-
return f"Could not find Genius or AZLyrics page, so couldn't extract lyrics. Raw results: {search_results}"
|
| 65 |
-
|
| 66 |
-
#Fallback: Simple regex extraction of song and artist names
|
| 67 |
-
if song_title is None or artist is None:
|
| 68 |
-
title_match = re.search(r"Title:\s*(.*)", search_results, re.IGNORECASE)
|
| 69 |
-
artist_match = re.search(r"Artist:\s*(.*)", search_results, re.IGNORECASE)
|
| 70 |
-
|
| 71 |
-
song_title = title_match.group(1).strip() if title_match else None
|
| 72 |
-
artist = artist_match.group(1).strip() if artist_match else None
|
| 73 |
-
|
| 74 |
-
#Hardcoded URL example
|
| 75 |
-
if song_title == 'TV' and artist == 'Billie Eilish':
|
| 76 |
-
spotify_url = "https://open.spotify.com/track/3GYlZ7tbxLOxe6ewMNVTkw?autoplay=true"
|
| 77 |
-
else: spotify_url = None
|
| 78 |
-
|
| 79 |
-
# Construct the final answer
|
| 80 |
-
if song_title and artist and lyrics and spotify_url:
|
| 81 |
-
return f"song name: {song_title} , by {artist} . the spotify url is : {spotify_url} \nthe lyrics are : {lyrics}" # return the song + lyrics
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
#Handle partial results - even if can't find the URL or Lyrics:
|
| 85 |
-
if song_title and artist:
|
| 86 |
-
spotify_message = f"\n(Could not reliably extract Spotify URL)" if spotify_url is None else ""
|
| 87 |
-
lyrics_message = f"\n(Could not reliably extract Lyrics)" if lyrics is None else ""
|
| 88 |
-
return f"Found song: {song_title} by {artist} . {spotify_message}{lyrics_message}. Raw results: {search_results}"
|
| 89 |
-
return f"Could not extract full information, check the search results:\n {search_results}" #in case it fails return the search result
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
else:
|
| 93 |
-
return "Could not find any songs matching the verse."
|
| 94 |
-
|
| 95 |
-
except Exception as e:
|
| 96 |
-
return f"An error occurred: {e}"
|
| 97 |
|
| 98 |
@tool
|
| 99 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -132,7 +68,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 132 |
|
| 133 |
agent = CodeAgent(
|
| 134 |
model=model,
|
| 135 |
-
tools=[final_answer,
|
| 136 |
max_steps=6,
|
| 137 |
verbosity_level=1,
|
| 138 |
grammar=None,
|
|
|
|
| 9 |
import re
|
| 10 |
import requests # Import the requests library
|
| 11 |
from bs4 import BeautifulSoup #for parsing
|
| 12 |
+
from urllib.parse import quote
|
| 13 |
|
| 14 |
@tool
|
| 15 |
+
def get_full_poem(verse: str) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Fetches and returns the song that includes the verse
|
| 18 |
+
|
| 19 |
Args:
|
| 20 |
+
verse: the verse that user wants to find the poem of it. (eg: چو ایران نباشد)
|
| 21 |
|
| 22 |
"""
|
| 23 |
+
encoded_query = quote(query)
|
| 24 |
+
url = f"https://ganjoor.net/search?s={encoded_query}&es=1&author=0"
|
| 25 |
+
return url
|
| 26 |
+
# url = f"https://ganjoor.net/search?s=%D8%B3%D9%84%D8%A7%D9%85&es=1&author=0"
|
| 27 |
+
# try:
|
| 28 |
+
# data = requests.get(url).json()
|
| 29 |
+
# price = float(data[crypto][currency])
|
| 30 |
+
# return f"${price:,.2f}"
|
| 31 |
+
# except Exception as e:
|
| 32 |
+
# return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@tool
|
| 35 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 68 |
|
| 69 |
agent = CodeAgent(
|
| 70 |
model=model,
|
| 71 |
+
tools=[final_answer, get_full_poem,get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
| 72 |
max_steps=6,
|
| 73 |
verbosity_level=1,
|
| 74 |
grammar=None,
|