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