Spaces:
Sleeping
Sleeping
File size: 3,085 Bytes
988f727 9b5b26a 988f727 c19d193 6aae614 8fe992b c7acfba 65158de 9b5b26a c7acfba 988f727 f759625 988f727 65158de 9b5b26a 988f727 65158de 988f727 65158de 988f727 65158de 8c01ffb 988f727 6aae614 e121372 988f727 13d500a 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b 988f727 8c01ffb 861422e 8fe992b 988f727 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import requests
from bs4 import BeautifulSoup
import pandas as pd
import yaml
from tools.final_answer import FinalAnswerTool
@tool
def amazon_product_scraper(search_url: str) -> list:
"""
A tool that scrapes Amazon search results for product titles, prices, delivery fees, and links.
Args:
search_url (str): The URL of the Amazon search results page. This should be a valid Amazon search URL containing product listings.
Returns:
list: A list containing two elements:
- A list of dictionaries, each with keys "Title", "Price", "Delivery", "Link", sorted by price.
- A string containing a recommendation for the best deal.
"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
}
response = requests.get(search_url, headers=headers)
if response.status_code != 200:
return [], "Failed to retrieve Amazon results. Amazon may be blocking requests."
soup = BeautifulSoup(response.text, 'html.parser')
product_data = []
for item in soup.select("div[data-asin]"):
title_tag = item.select_one("h2 a")
price_tag = item.select_one("span.a-price-whole")
delivery_tag = item.select_one("span.s-align-children-center")
if title_tag and price_tag:
title = title_tag.text.strip()
price = price_tag.text.strip().replace(',', '') # Normalize prices
delivery = delivery_tag.text.strip() if delivery_tag else "Free"
link = "https://www.amazon.com" + title_tag["href"]
product_data.append({
"Title": title,
"Price": float(price) if price.isnumeric() else None,
"Delivery": delivery,
"Link": link
})
# Filter out products with no price and sort by price
product_data = [p for p in product_data if p["Price"] is not None]
product_data.sort(key=lambda x: x["Price"])
# Recommendation logic
best_deal = product_data[0] if product_data else None
recommendation = ""
if best_deal is not None:
recommendation = f"Best deal: {best_deal['Title']} at ${best_deal['Price']} with {best_deal['Delivery']} (Link: {best_deal['Link']})"
return product_data, recommendation
# Define the Agent
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, amazon_product_scraper], # Adding the scraper tool
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()
|