Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,71 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
character: The character to feature in the meme (e.g., "cat", "superhero").
|
| 18 |
|
| 19 |
Returns:
|
| 20 |
-
A string
|
| 21 |
"""
|
| 22 |
-
prompt = f"Create a meme with a {theme} theme featuring a {character}. The meme should be humorous and visually appealing."
|
| 23 |
-
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
return f"Error
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
@tool
|
|
@@ -65,7 +108,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 65 |
|
| 66 |
agent = CodeAgent(
|
| 67 |
model=model,
|
| 68 |
-
tools=[final_answer,
|
| 69 |
max_steps=6,
|
| 70 |
verbosity_level=1,
|
| 71 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
@tool
|
| 13 |
+
from bs4 import BeautifulSoup
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def get_amazon_product_price(product: str) -> str:
|
| 17 |
+
"""A tool that fetches the current price of an Amazon product given its product name.
|
| 18 |
|
| 19 |
Args:
|
| 20 |
+
product: The name of the product to search on Amazon.
|
|
|
|
| 21 |
|
| 22 |
Returns:
|
| 23 |
+
A string indicating the current price of the product or an error message.
|
| 24 |
"""
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
+
# Use the DuckDuckGo search tool to find an Amazon page for the product
|
| 27 |
+
search_query = f"site:amazon.com {product}"
|
| 28 |
+
search_results = search_tool(query=search_query)
|
| 29 |
+
|
| 30 |
+
# Assume search_results is a list of URLs; if not, wrap it into a list.
|
| 31 |
+
if not isinstance(search_results, list):
|
| 32 |
+
search_results = [search_results]
|
| 33 |
+
|
| 34 |
+
amazon_url = None
|
| 35 |
+
for result in search_results:
|
| 36 |
+
if "amazon.com" in result:
|
| 37 |
+
amazon_url = result
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
if not amazon_url:
|
| 41 |
+
return "Could not find an Amazon product page for the given product."
|
| 42 |
+
|
| 43 |
+
headers = {
|
| 44 |
+
"User-Agent": (
|
| 45 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 46 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 47 |
+
"Chrome/90.0.4430.93 Safari/537.36"
|
| 48 |
+
)
|
| 49 |
+
}
|
| 50 |
+
response = requests.get(amazon_url, headers=headers, timeout=10)
|
| 51 |
+
if response.status_code != 200:
|
| 52 |
+
return f"Error fetching the page: Received status code {response.status_code}"
|
| 53 |
+
|
| 54 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 55 |
+
|
| 56 |
+
price_tag = soup.find(id="priceblock_ourprice")
|
| 57 |
+
if price_tag is None:
|
| 58 |
+
price_tag = soup.find(id="priceblock_dealprice")
|
| 59 |
+
if price_tag is None:
|
| 60 |
+
price_container = soup.find("span", class_="a-price")
|
| 61 |
+
if price_container:
|
| 62 |
+
price_tag = price_container.find("span", class_="a-offscreen")
|
| 63 |
+
|
| 64 |
+
if price_tag:
|
| 65 |
+
price = price_tag.get_text().strip()
|
| 66 |
+
return f"The current price for '{product}' is: {price}"
|
| 67 |
+
else:
|
| 68 |
+
return "Price not found on the product page."
|
| 69 |
except Exception as e:
|
| 70 |
+
return f"Error fetching product price: {str(e)}"
|
| 71 |
+
|
| 72 |
|
| 73 |
|
| 74 |
@tool
|
|
|
|
| 108 |
|
| 109 |
agent = CodeAgent(
|
| 110 |
model=model,
|
| 111 |
+
tools=[final_answer, get_amazon_product_price], ## add your tools here (don't remove final answer)
|
| 112 |
max_steps=6,
|
| 113 |
verbosity_level=1,
|
| 114 |
grammar=None,
|